使用Schema在XML文档中定义根元素是否可行?

9

这是可能的吗?我无法弄清如何做到。

1个回答

6
以下内容应该可行,我还建议您查看W3 Schools关于模式的部分。
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="rootElement" type="RootElementType"/>

  <xs:complexType name="RootElementType">
    <xs:sequence>
      <xs:element name="child1" type="xs:string" minOccurs="1" maxOccurs="unbounded"/>
      <xs:element name="child2" type="xs:string" minOccurs="0" maxOccurs="1"/>
    </xs:sequence>
    <xs:attribute name="user" type="xs:string" use="required"/>
  </xs:complexType>
</xs:schema>

这应该是一个类似于以下 XML 结构的模式:

<rootElement user="Bob">
  <child1>Hello</child1>
  <child1>World</child1>
  <child2>Optional</child2>
</rootElement>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接