微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

xml – 用于选择没有特定属性的特定值的任何后代的XPath查询

我试图构建一个XPath查询,基本上选择一切但排除某些节点.

这是我正在经历的XML

<?xml version="1.0" encoding="UTF-8"?>

<task>
  <title id="30014">Instructions</title>
  <taskbody>
    <context>
      <p>Your Box has a document.</p>
      <p audience="print">To get the document:</p>
      <p audience="web">
        <xref href="/node/6308" scope="external">Click here</xref> to get the document.
      </p>
    </context>
    <steps audience="print">
      <step>
        <cmd>Go to 
          <u>www.google.com</u>.
        </cmd>
      </step>
      <step>
        <cmd>Click on the “Resource” button.</cmd>
        <info>
          <fig frame="all">
            <image href="resource.ai" height="1.650in" width="4.500in"/>
          </fig>
        </info>
      </step>
      <step>
        <cmd>Click on “Manuals”.</cmd>
      </step>
      <step>
        <cmd>Click on “Shipping”.</cmd>
      </step>
      <step>
        <cmd>You can save or print it from your browser window.</cmd>
      </step>
    </steps>
  </taskbody>
</task>

我需要选择观众不等于“打印”的所有内容.

我一直在尝试各种各样的方式,但我似乎都没有按照我的方式工作.

这是最近的一个,但几乎没有:

task/taskbody//*[not(@audience = "print")]

问题是,它可以很好地剥离具有“打印”值的节点1级.然而,第一个< p>具有“print”值的内容位于< context>内.该节点似乎永远不会被选中.

以下是查询的结果:

<?xml version="1.0" encoding="UTF-8"?>
<result>
<context>
      <p>Your Box has a document.</p>
      <p audience="print">To get the document:</p>
      <p audience="web">
        <xref href="/node/6308" scope="external">Click here</xref> to get the document.
      </p>
    </context>

<p>Your Box has a document.</p>

<p audience="web">
        <xref href="/node/6308" scope="external">Click here</xref> to get the document.
      </p>

<xref href="/node/6308" scope="external">Click here</xref>

<step>
        <cmd>Go to 
          <u>www.google.com</u>.
        </cmd>
      </step>

<cmd>Go to 
          <u>www.google.com</u>.
        </cmd>

<u>www.google.com</u>

<step>
        <cmd>Click on the “Resource” button.</cmd>
        <info>
          <fig frame="all">
            <image height="1.650in" href="resource.ai" width="4.500in"/>
          </fig>
        </info>
      </step>

<cmd>Click on the “Resource” button.</cmd>

<info>
          <fig frame="all">
            <image height="1.650in" href="resource.ai" width="4.500in"/>
          </fig>
        </info>

<fig frame="all">
            <image height="1.650in" href="resource.ai" width="4.500in"/>
          </fig>

<image height="1.650in" href="resource.ai" width="4.500in"/>

<step>
        <cmd>Click on “Manuals”.</cmd>
      </step>

<cmd>Click on “Manuals”.</cmd>

<step>
        <cmd>Click on “Shipping”.</cmd>
      </step>

<cmd>Click on “Shipping”.</cmd>

<step>
        <cmd>You can save or print it from your browser window.</cmd>
      </step>

<cmd>You can save or print it from your browser window.</cmd>

</result>

它抓取没有属性的节点,它用“web”抓取节点,大多数节点用“print”除了那个节点.

有什么建议?

解决方法

此表达式将选择所有不具有@audience属性的元素,以及包含不是字符串print的值的元素:

//*[not(descendant::*[@audience='print']) and not(ancestor-or-self::*[@audience='print'])]

在它上面写的方式将选择< title>,第一个和第三个< p> < context>的孩子.它不会选择< steps>或者第二个< p>因为他们有一个包含print的audience属性.

要排除标题(将上下文缩减为taskbody),请使用:

//task/taskbody//*[not(descendant::*[@audience='print']) and not(ancestor-or-self::*[@audience='print'])]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。