基于其他字段值的XSD条件限制



我有以下XML:

<entity>
<category>Foo</category>
<subcategory>Foo1</subcategory>
</entity>

现在我想只允许特定的子类别。我将从以下XSD开始:

<xs:element name="entity">
<xs:complexType>
<xs:sequence>
<xs:element name="category" type="categories"/>
<xs:element name="subcategory" type="???"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="categories">
<xs:restriction base="xs:string">
<xs:enumeration value="Foo"/>
<xs:enumeration value="Bar"/>
<xs:enumeration value="many other categories"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="fooSubCategories">
<xs:restriction base="xs:string">
<xs:enumeration value="Foo1"/>
<xs:enumeration value="Foo2"/>
<xs:enumeration value="FooN"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="barSubCategories">
<xs:restriction base="xs:string">
<xs:enumeration value="Bar1"/>
<xs:enumeration value="Bar2"/>
<xs:enumeration value="BarN"/>
</xs:restriction>
</xs:simpleType>

现在我阅读了XSD 1.1中的assert元素:基于另一个元素值的XMLRestrict。XSD 1.1在我阅读时也有一些规则。

我该怎么做?我试图扩展子类别元素并插入xs:choice元素,但这在规范中是不允许的。

经过一段时间的思考,我将使用一个类别字段:

<xs:enumeration value="Foo"/>
<xs:enumeration value="Foo/Bar1"/>
<xs:enumeration value="Foo/Bar2"/>
<xs:enumeration value="Foo/Bar3"/>
<xs:enumeration value="Foo/Bar3"/>
<xs:enumeration value="Foo/Bar4"/>
<xs:enumeration value="Foo/Bar4/Baz1"/>

从语义上来说不是最好的,但这在深度上没有限制。

尽管如此:如果你有时间,一个解决方案,并想在未来帮助某人,请随时发布你的解决方案。

最新更新