如何在XSD断言XPath中访问父元素?

时间:2016-05-12 02:43:15

标签: xml xpath xsd assertions xsd-1.1

我正在尝试编写一个断言,使@row@column的值小于或等于父元素中@rows@columns的值<structure>

<xs:element name="structure">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="cell" maxOccurs="unbounded">
                <xs:complexType>
                    <xs:attribute name="row" type="xs:positiveInteger"/>   
                    <xs:attribute name="column" type="xs:positiveInteger"/>
                    <xs:assert test="@row le @rows"/>
                    <xs:assert test="@column le @columns"/>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
        <xs:attribute name="rows" type="xs:positiveInteger" use="optional"/>
        <xs:attribute name="columns" type="xs:positiveInteger" use="optional"/>
    </xs:complexType>
</xs:element>

我的断言是在错误的地方吗?我用什么XPath表达式来指定父节点?我的编辑不是让我写..@rows

1 个答案:

答案 0 :(得分:3)

断言XPath无法到达其上下文之外。

因此,将您的断言移至structure元素,并使用every ... satisfies断言测试:

<?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"
           elementFormDefault="qualified"
           vc:minVersion="1.1">
    <xs:element name="structure">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="cell" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:attribute name="row" type="xs:positiveInteger"/>   
                        <xs:attribute name="column" type="xs:positiveInteger"/>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
            <xs:attribute name="rows" type="xs:positiveInteger" use="optional"/>
            <xs:attribute name="columns" type="xs:positiveInteger" use="optional"/>
            <xs:assert test="every $r in cell/@row satisfies @rows >= $r"/>
            <xs:assert test="every $c in cell/@column satisfies @columns >= $c"/>
        </xs:complexType>
    </xs:element>
</xs:schema>