0

我的问题是我的 XPATH 规则无法正常工作。我希望它选择 DataFile 元素属性 Filename 不是“thing.xml”的所有 XML。我还添加了示例 XML,以便于理解。

这是我的 XPATH 规则:

/document[transport/sender[code='12345678'] and metaxml/LetterMetaData[Type='invoice'] and SignedDoc/DataFile[@Filename!='thing.xml']]

这是我希望我的XPATH 规则忽略的 XML :

<document>
  <transport>
    <sender>
      <code>12345678</code>
    </sender>
  </transport>
  <metaxml>
    <LetterMetaData>
      <Type>invoice</Type>
    </LetterMetaData>
  </metaxml>
  <SignedDoc>
    <DataFile Filename="thing.xml">...</DataFile>
  </SignedDoc>
</document>

这是我希望我的 XPATH 规则不要忽略的 XML :

<document>
  <transport>
    <sender>
      <code>12345678</code>
    </sender>
  </transport>
  <metaxml>
    <LetterMetaData>
      <Type>invoice</Type>
    </LetterMetaData>
  </metaxml>
  <SignedDoc>
    <DataFile Filename="file_with_other_name_than_thing.xml">...</DataFile>
  </SignedDoc>
</document>
4

1 回答 1

0

Which is the child of the root node (/)? If your document is well formed should not be document and your XPath is evaluating to nothing. For instance, the following XPath:

 /docs/document[SignedDoc/DataFile[not(@Filename!='thing.xml')]]

applied on this input:

<docs>
    <document>
        <transport>
            <sender>
                <code>12345678</code>
            </sender>
        </transport>
        <metaxml>
            <LetterMetaData>
                <Type>invoice</Type>
            </LetterMetaData>
        </metaxml>
        <SignedDoc>
            <DataFile Filename="thing.xml">...</DataFile>
        </SignedDoc>
    </document>
    <document>
        <transport>
            <sender>
                <code>12345678</code>
            </sender>
        </transport>
        <metaxml>
            <LetterMetaData>
                <Type>invoice</Type>
            </LetterMetaData>
        </metaxml>
        <SignedDoc>
            <DataFile Filename="file_with_other_name_than_thing.xml">...</DataFile>
        </SignedDoc>
    </document>
</docs>

returns the second document node only. Notice that root's child is docs.

if you don't want to select starting from the root you should use //:

 //document[SignedDoc/DataFile[not(@Filename!='thing.xml')]]
于 2011-06-15T06:53:36.033 回答