基于属性值的条件(XML Schema)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8801256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Condition based on attribute value (XML Schema)
提问by Pol
Is it possible to define in XML Schema an condition based on attribute value? For example, when test@attrib="one", I want one-elementto be allowed and mandatory or when test@attrib="two", I want two-elementto be allowed and mandatory.
是否可以在 XML Schema 中定义基于属性值的条件?例如, when test@attrib="one",我想要one-element被允许和强制,或者 when test@attrib="two",我想要two-element被允许和强制。
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 中可能吗?
采纳答案by tom redfern
Not within the same type. You would need to define a different type for each of the different options.
不在同一类型内。您需要为每个不同的选项定义不同的类型。
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>

