对错误消息感到困惑 "invalid XPath for selector or field"

Confusion over error message "invalid XPath for selector or field"

我试图更好地理解 key 和 keyref 以及如何在模式中使用它们。我想将键应用于 y 类型的部分,而不是 z 类型的部分。我很困惑为什么会看到以下错误。

'r:B/r:part[@type='y']' is an invalid XPath for selector or field

我几乎可以肯定 XPath 是有效的,但是当我在 XPath 表达式中输入谓词过滤器时,我从 Visual Studio 收到错误消息。

这是XML

<root xmlns="namespace1">
  <A>
    <!-- if the ref-number is not equal to one of the key-number, the validation will give error -->
    <part ref-number="1"/>
  </A>
  <A>
    <!-- if the ref-number is not equal to one of the key-number, the validation will give error -->
    <part ref-number="2"/>
  </A>
  <B>
    <part type="y" key-number="1"/>
    <part type="y" key-number="2"/>
    <part type="z" key-number="3"/>
  </B>
</root>

这是架构

    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="namespace1" xmlns:r="namespace1" elementFormDefault="qualified">
      <xs:element name="root">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="A" type="r:aType" maxOccurs="unbounded">
              <xs:keyref name="dummy" refer="r:partNumberKey">
                <xs:selector xpath="r:part"/>
                <xs:field xpath="@ref-number"/>
              </xs:keyref>
            </xs:element>
            <xs:element name="B" type="r:bType"/>
          </xs:sequence>
        </xs:complexType>
        <xs:key name="partNumberKey">
<!-- I get an error here -->
          <xs:selector xpath="r:B/r:part[@type='y']"/>
          <xs:field xpath="@key-number"/>
        </xs:key>
      </xs:element>
      <xs:complexType name="aType">
        <xs:sequence>
          <xs:element name="part" maxOccurs="unbounded">
            <xs:complexType>
              <xs:simpleContent>
                <xs:extension base="xs:string">
                  <xs:attribute name="ref-number" type="xs:integer"/>
                </xs:extension>
              </xs:simpleContent>
            </xs:complexType>
          </xs:element>
        </xs:sequence>
      </xs:complexType>
      <xs:complexType name="bType">
        <xs:sequence>
          <xs:element name="part" maxOccurs="unbounded">
            <xs:complexType>
              <xs:simpleContent>
                <xs:extension base="xs:string">
                  <xs:attribute name="key-number" type="xs:integer"/>
                  <xs:attribute name="type" type="xs:string"/>
                </xs:extension>
              </xs:simpleContent>
            </xs:complexType>
          </xs:element>
        </xs:sequence>
      </xs:complexType>
    </xs:schema>

您从 Xerces/Oxygen 收到更有意义的错误消息:

[Xerces] c-general-xpath: The expression 'r:B/r:part[@type='y']' is not valid with respect to the XPath subset supported by XML Schema.

XML Schema 只支持 subset of XPath。以下规则列出了允许作为 xs:selectorxpath 属性值的内容:

[1]     Selector       ::=      Path ( '|' Path )*
[2]     Path       ::=      ('.//')? Step ( '/' Step )*
[3]     Step       ::=      '.' | NameTest
[4]     NameTest       ::=      QName | '*' | NCName ':' '*'

这是一种神秘的说法,表示在此上下文中不支持带有谓词的表达式(介于 [] 之间)。

如果这个 XML 是您自己设计的,您可以为 part 元素指定不同的名称,而不是不同的 type 属性。


另一种方法是在执行此检查的 XML 模式中包含 Schematron 规则。

已修改 XML 具有嵌入式 Schematron 的模式

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:r="namespace1"
    xmlns:sch="http://purl.oclc.org/dsdl/schematron"
    targetNamespace="namespace1"
    elementFormDefault="qualified">

    <xs:annotation>
        <xs:appinfo>
            <sch:ns uri="namespace1"
                prefix="r" />
            <sch:pattern>
                <sch:rule context="r:A/r:part">
                    <sch:assert test="@ref-number = /r:root/r:B/r:part/@key-number">Key Error!</sch:assert>
                </sch:rule>
            </sch:pattern>
        </xs:appinfo>
    </xs:annotation>

    <xs:element name="root">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="A" type="r:aType" maxOccurs="unbounded"/>
                <xs:element name="B" type="r:bType"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:complexType name="aType">
        <xs:sequence>
            <xs:element name="part" maxOccurs="unbounded">
                <xs:complexType>
                    <xs:simpleContent>
                        <xs:extension base="xs:string">
                            <xs:attribute name="ref-number" type="xs:integer"/>
                        </xs:extension>
                    </xs:simpleContent>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="bType">
        <xs:sequence>
            <xs:element name="part" maxOccurs="unbounded">
                <xs:complexType>
                    <xs:simpleContent>
                        <xs:extension base="xs:string">
                            <xs:attribute name="key-number" type="xs:integer"/>
                            <xs:attribute name="type" type="xs:string"/>
                        </xs:extension>
                    </xs:simpleContent>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

我不确定这是否正是您想要的,因为我不完全理解为什么 part 元素的 type 属性很重要。

如果您在 XML 实例文档中引用 XSD,您现在还需要按照

的行包括对 Schematron 规则的引用
<?xml-model href="../Desktop/key.xsd" type="application/xml" schematypens="http://purl.oclc.org/dsdl/schematron"?>