我有一个xml,它可以有两种类型的节点,如下所述:
类型1:
<node attr1="false" attr2="attr2">
<node attr1="true" attr2="attr2" attr3="attr3" attr4=""></node>
</node>
<node attr1="false" attr2="attr2">
<node attr1="false" attr2="attr2">
<node attr1="true" attr2="attr2" attr3="attr3" attr4="">
</node>
</node>
<node attr1="true" attr2="attr2" attr3="attr3" attr4=""></node>
类型2:
<node attr1="true" attr2="attr2" attr3="attr3" attr4=""></node>
如果attr1值为false,那么它将有子节点,并且没有attr3,attr4将在那里
如果attr1值为true,那么它没有任何子节点,并且attr3,attr4将在那里。
答案 0 :(得分:0)
你可以在XSD 1.1中使用断言来做到这一点,但我看不到在XSD 1.0中做到这一点的方法
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML 2017 Developer Bundle Edition 15.0.0.0 (https://www.liquid-technologies.com)-->
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="node">
<xs:complexType>
<xs:sequence>
<xs:element ref="node" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
<xs:attribute name="attr1" type="xs:boolean" use="required" />
<xs:attribute name="attr2" type="xs:string" use="required" />
<xs:attribute name="attr3" type="xs:string" />
<xs:attribute name="attr4" type="xs:string" />
<xs:assert test="string(@attr1) = ('false', '0') or boolean(@attr3)" />
<xs:assert test="string(@attr1) = ('false', '0') or boolean(@attr4)" />
<xs:assert test="string(@attr1) = ('true', '1') or not(boolean(@attr3))" />
<xs:assert test="string(@attr1) = ('true', '1') or not(boolean(@attr4))" />
</xs:complexType>
</xs:element>
</xs:schema>