XPath 和多个条件

发布于 2024-11-15 09:17:29 字数 1240 浏览 4 评论 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>

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾北清歌寒 2024-11-22 09:17:29

哪个是根节点 (/) 的子节点?如果您的文档格式良好,则不应是 document 并且您的 XPath 评估结果为零。例如,以下 XPath:

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

应用于此输入:

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

仅返回第二个文档节点。请注意,root 的子级是 docs

如果您不想选择从根开始,您应该使用 //

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

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')]]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文