XSD 1.1:使用断言根据父属性限制元素

时间:2015-03-30 13:59:27

标签: xml xpath xsd schema assertion

鉴于XML

<test type="a">
    <foo>1</foo>
    <bar>2</bar>
</test>

如果@type不是“a”,我想做一些简单的事情,比如使用XSD断言来禁止使用元素“foo”。我在以下架构中尝试过断言:

<xs:element name="test">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="foo"/>
            <xs:element name="bar"/>
        </xs:sequence>
        <xs:attribute name="type">
            <xs:simpleType>
                <xs:restriction base="xs:Name">
                    <xs:enumeration value="a"/>
                    <xs:enumeration value="b"/>
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>
        <xs:assert test="not(@type='b' and exists(foo))"/>
    </xs:complexType>
</xs:element>

但这不起作用。我正在使用Oxygen,而Oxygen又使用了Saxon验证器,并且断言总是失败。否则断言工作(声明test="@type='b'"正确拒绝上述XML片段)但由于某种原因,我无法测试test是否存在子元素。我也试过exists(./foo)count(foo) gt 0无济于事。

我是否误解了有关xpath语法的内容?

0 个答案:

没有答案
相关问题