【发布时间】:2014-11-10 19:29:22
【问题描述】:
是否可以定义一个 XSD 结构,使得元素上的属性只有在父(直接/间接)元素上的属性具有特定值时才能具有特定值?
示例:
<root>
<child1 myAttr="true">
<child2>
<child3 otherAttr="false" /> <!-- 'otherAttr' can only be 'false' if 'myAttr' is 'true' -->
</child2>
</child1>
</root>
伪解:
将<rule condition="@otherAttr == true && //child1/@myAttr != false" /> 之类的内容添加到“child3”复杂类型的定义中...
示例:
<?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:sequence>
<xs:element name="child1" type="child1Type" />
</xs:sequence>
</xs:element>
<xs:complexType name="child1Type">
<xs:sequence>
<xs:element name="child2" type="child2Type" />
</xs:sequence>
<xs:attribute name="myAttr" type="xs:boolean" use="optional" default="false" />
</xs:complexType>
<xs:complexType name="child2Type">
<xs:sequence>
<xs:element name="child3" type="child3Type" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="child3Type">
<xs:attribute name="otherAttr" type="xs:boolean" use="optional" default="true" />
<rule condition="@otherAttr == true && //child1/@myAttr != false" />
</xs:complexType>
【问题讨论】:
-
没有该问题的基本原理阻止提供此问题的答案。类似的问题不同的要求和细节。问题足够独特,足以支持 IMO
标签: xml xsd xsd-validation