加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 22:46:01 所属栏目:百科 来源:网络整理
导读:我试图构建一个XPath查询,基本上选择一切但排除某些节点. 这是我正在经历的XML: ?xml version="1.0" encoding="UTF-8"?task title id="30014"Instructions/title taskbody context pYour box has a document./p p audience="print"To get the document:/p p
我试图构建一个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'])]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读