XSD 允许两个元素的任意组合
XSD that allows any combination of two elements
我有巨大的 XML 文件,而且所有文件都只有 2 种元素类型。但是,元素的层次结构根据 xml 不断变化。是否可以创建具有 2 种元素类型的 XSD,并且 XML 文档的所有元素都将根据此 XSD 进行验证?
例如
假设 a
和 b
是唯一可能的元素类型
1.xml
<b attr1="hello">
<b attr1="Hello">
<a></a>
<a></a>
</b>
<a></a>
</b>
2.xml
<b attr1="hello">
<b attr1="hello">
<a></a>
<a></a>
<b attr1="hello">
<a></a>
</b>
</b>
<a></a>
</b>
我可以使用定义元素 a
和 b
应该如何显示的相同 XSD 来验证两个 XML 文档吗?
更新:为'b'节点添加了属性。
以下 XSD 将允许 a
和 b
元素的任意组合:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="a" type="ab"/>
<xs:element name="b" type="ab"/>
<xs:complexType name="ab">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="a"/>
<xs:element ref="b"/>
</xs:choice>
</xs:complexType>
</xs:schema>
有属性
对原始问题的每次 OP 编辑更新:
Update: Added attributes to 'b' node.
要指定属性 attr1
必须出现在 a
和 b
上,照常在 xs:complexType
中添加一个 xs:attribute
声明:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="a" type="ab"/>
<xs:element name="b" type="ab"/>
<xs:complexType name="ab">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="a"/>
<xs:element ref="b"/>
</xs:choice>
<xs:attribute name="attr1" use="required"/>
</xs:complexType>
</xs:schema>
要指定属性 attr1
必须出现在 b
上而不要求它出现在 a
上,请按上述操作,但拆分 a
和 a
的定义b
:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="a" type="aType"/>
<xs:element name="b" type="bType"/>
<xs:complexType name="aType">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="a"/>
<xs:element ref="b"/>
</xs:choice>
</xs:complexType>
<xs:complexType name="bType">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="a"/>
<xs:element ref="b"/>
</xs:choice>
<xs:attribute name="attr1" use="required"/>
</xs:complexType>
</xs:schema>
我有巨大的 XML 文件,而且所有文件都只有 2 种元素类型。但是,元素的层次结构根据 xml 不断变化。是否可以创建具有 2 种元素类型的 XSD,并且 XML 文档的所有元素都将根据此 XSD 进行验证?
例如
假设 a
和 b
是唯一可能的元素类型
1.xml
<b attr1="hello">
<b attr1="Hello">
<a></a>
<a></a>
</b>
<a></a>
</b>
2.xml
<b attr1="hello">
<b attr1="hello">
<a></a>
<a></a>
<b attr1="hello">
<a></a>
</b>
</b>
<a></a>
</b>
我可以使用定义元素 a
和 b
应该如何显示的相同 XSD 来验证两个 XML 文档吗?
更新:为'b'节点添加了属性。
以下 XSD 将允许 a
和 b
元素的任意组合:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="a" type="ab"/>
<xs:element name="b" type="ab"/>
<xs:complexType name="ab">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="a"/>
<xs:element ref="b"/>
</xs:choice>
</xs:complexType>
</xs:schema>
有属性
对原始问题的每次 OP 编辑更新:
Update: Added attributes to 'b' node.
要指定属性 attr1
必须出现在 a
和 b
上,照常在 xs:complexType
中添加一个 xs:attribute
声明:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="a" type="ab"/>
<xs:element name="b" type="ab"/>
<xs:complexType name="ab">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="a"/>
<xs:element ref="b"/>
</xs:choice>
<xs:attribute name="attr1" use="required"/>
</xs:complexType>
</xs:schema>
要指定属性 attr1
必须出现在 b
上而不要求它出现在 a
上,请按上述操作,但拆分 a
和 a
的定义b
:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="a" type="aType"/>
<xs:element name="b" type="bType"/>
<xs:complexType name="aType">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="a"/>
<xs:element ref="b"/>
</xs:choice>
</xs:complexType>
<xs:complexType name="bType">
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="a"/>
<xs:element ref="b"/>
</xs:choice>
<xs:attribute name="attr1" use="required"/>
</xs:complexType>
</xs:schema>