开发者

XPath and multiple conditions

My problem is my XPATH rule doesn't work correctly. I want it to select all the XML's where DataFile element attribute Filename is not "thing.xml". I also added the sample XML's for easier understanding.

This is my XPATH rule:

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

Here's the XML I want my XPATH rule to ignore:

<document>
  <transport>
    <sender>
      <code>12345678</code>
    </sender>
  </transport>
  <metaxml>
    <LetterMetaData>
      <Type>invoice</Type>
    </LetterMetaData>
  </metaxml>
  <SignedDoc>
    <DataFile Filename="thing.xml">...</DataFile>
  </开发者_StackOverflow社区SignedDoc>
</document>

Here's the XML I want my XPATH rule not to ignore:

<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>


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')]]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜