如果存在其中一个属性,则应该要求另一个属性,否则两者都必须是可选的

时间:2014-11-04 13:38:57

标签: xml xsd

让我们说我关注XML中的内容:

<shape type="rectangle" fontSize="14.11" width="10" height="5" />
<shape type="roundedrectangle" fontSize="16" />
<shape type="circle" fontSize="12" />
<shape type="roundedrectangle" fontSize="11" width="10" height="5" />

我想创建一个XSD来验证&#34; width&#34;作为属性出现然后&#34;身高&#34;必须存在于该元素中,反之亦然。

1 个答案:

答案 0 :(得分:1)

XSD 1.0

必须在代码中完成。

XSD 1.1

可以通过assertion

完成
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
           vc:minVersion="1.1">
    <xs:element name="shape">
        <xs:complexType>
            <xs:attribute name="height" type="xs:int"/>
            <xs:attribute name="width" type="xs:int"/>
            <xs:assert test="(@height and @width) or (not(@height) and not(@width))"/>
        </xs:complexType>
    </xs:element>
</xs:schema>
相关问题