Is it possible to define in XML Schema an condition based on attribute value? For example, when test@attrib="one"
, I want one-element
to be allowed and mandatory or when test@attrib="two"
, I want two-element
to be allowed and mandatory.
是否可以在XML Schema中定义基于属性值的条件?例如,当test @ attrib =“one”时,我希望允许一个元素和强制性,或者当test @ attrib =“two”时,我希望允许两个元素并且是强制性的。
For example, valid documents are:
例如,有效文件是:
<root>
<test attrib="one"/>
<some-element-1/>
<some-element-2/>
...
<some-element-n/>
<one-element>
</one-element>
</root>
or
要么
<root>
<test attrib="two"/>
<some-element-1/>
<some-element-2/>
...
<some-element-n/>
<two-element>
</two-element>
</root>
Wrong documents:
错误的文件:
<root>
<test attrib="one"/>
<some-element-1/>
<some-element-2/>
...
<some-element-n/>
</root>
or
要么
<root>
<test attrib="two"/>
<some-element-1/>
<some-element-2/>
...
<some-element-n/>
<one-element>
</one-element>
</root>
Is it possible in XSD?
在XSD中可以吗?
1 个解决方案
#1
10
Not within the same type. You would need to define a different type for each of the different options.
不属于同一类型。您需要为每个不同的选项定义不同的类型。
UPDATE
UPDATE
To re-use type definitions in your schema:
要在模式中重用类型定义,请执行以下操作:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://My.Schema.Namespace"
targetNamespace="http://My.Schema.Namespace">
<xs:element name="root">
<xs:complexType>
<xs:choice>
<xs:element name="test1" type="test1Type" />
<xs:element name="test2" type="test2Type" />
</xs:choice>
</xs:complexType>
</xs:element>
<!-- define the two root types -->
<xs:complexType name="test1Type">
<xs:all>
<xs:element name="some-element-1" type="some-element-1Type" />
<xs:element name="some-element-2" type="some-element-2Type" />
<xs:element name="some-element-3" type="some-element-3Type" />
<xs:element name="one-element" type="one-elementType" />
</xs:all>
<xs:attribute name="attrib" type="xs:string" fixed="one" />
</xs:complexType>
<xs:complexType name="test2Type">
<xs:all>
<xs:element name="some-element-1" type="some-element-1Type" />
<xs:element name="some-element-2" type="some-element-2Type" />
<xs:element name="some-element-3" type="some-element-3Type" />
<xs:element name="two-element" type="two-elementType" />
</xs:all>
<xs:attribute name="attrib" type="xs:string" fixed="two" />
</xs:complexType>
<!-- Define re-usable types-->
<xs:complexType mixed="true" name="some-element-1Type"/>
<xs:complexType mixed="true" name="some-element-2Type"/>
<xs:complexType mixed="true" name="some-element-3Type"/>
<xs:complexType mixed="true" name="one-elementType"/>
<xs:complexType mixed="true" name="two-elementType"/>
</xs:schema>
This will validate:
这将验证:
<?xml version="1.0" encoding="utf-8" ?>
<root xmlns="http://My.Schema.Namespace">
<test1 attrib="one">
<some-element-1>sadas</some-element-1>
<some-element-2>sadas</some-element-2>
<some-element-3>sadas</some-element-3>
<one-element>sadas</one-element>
</test1>
</root>
and
和
<?xml version="1.0" encoding="utf-8" ?>
<root xmlns="http://My.Schema.Namespace">
<test2 attrib="two">
<some-element-1>sadas</some-element-1>
<some-element-2>sadas</some-element-2>
<some-element-3>sadas</some-element-3>
<two-element>sadas</two-element>
</test2>
</root>
#1
10
Not within the same type. You would need to define a different type for each of the different options.
不属于同一类型。您需要为每个不同的选项定义不同的类型。
UPDATE
UPDATE
To re-use type definitions in your schema:
要在模式中重用类型定义,请执行以下操作:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://My.Schema.Namespace"
targetNamespace="http://My.Schema.Namespace">
<xs:element name="root">
<xs:complexType>
<xs:choice>
<xs:element name="test1" type="test1Type" />
<xs:element name="test2" type="test2Type" />
</xs:choice>
</xs:complexType>
</xs:element>
<!-- define the two root types -->
<xs:complexType name="test1Type">
<xs:all>
<xs:element name="some-element-1" type="some-element-1Type" />
<xs:element name="some-element-2" type="some-element-2Type" />
<xs:element name="some-element-3" type="some-element-3Type" />
<xs:element name="one-element" type="one-elementType" />
</xs:all>
<xs:attribute name="attrib" type="xs:string" fixed="one" />
</xs:complexType>
<xs:complexType name="test2Type">
<xs:all>
<xs:element name="some-element-1" type="some-element-1Type" />
<xs:element name="some-element-2" type="some-element-2Type" />
<xs:element name="some-element-3" type="some-element-3Type" />
<xs:element name="two-element" type="two-elementType" />
</xs:all>
<xs:attribute name="attrib" type="xs:string" fixed="two" />
</xs:complexType>
<!-- Define re-usable types-->
<xs:complexType mixed="true" name="some-element-1Type"/>
<xs:complexType mixed="true" name="some-element-2Type"/>
<xs:complexType mixed="true" name="some-element-3Type"/>
<xs:complexType mixed="true" name="one-elementType"/>
<xs:complexType mixed="true" name="two-elementType"/>
</xs:schema>
This will validate:
这将验证:
<?xml version="1.0" encoding="utf-8" ?>
<root xmlns="http://My.Schema.Namespace">
<test1 attrib="one">
<some-element-1>sadas</some-element-1>
<some-element-2>sadas</some-element-2>
<some-element-3>sadas</some-element-3>
<one-element>sadas</one-element>
</test1>
</root>
and
和
<?xml version="1.0" encoding="utf-8" ?>
<root xmlns="http://My.Schema.Namespace">
<test2 attrib="two">
<some-element-1>sadas</some-element-1>
<some-element-2>sadas</some-element-2>
<some-element-3>sadas</some-element-3>
<two-element>sadas</two-element>
</test2>
</root>