获取没有特定祖先 xml xpath 的节点

发布于 2024-11-07 02:28:53 字数 787 浏览 0 评论 0原文

我想要 xpath,它获取没有祖先的节点,它是特定节点的第一个后代。

假设我们有这样的 xml 文档:

<a>
  <b>This node</b>
  <c>
    <a>
      <b>not this</b>
      <g>
        <b>not this</b>
      </g>
    </a>
    <a>
      <b>This node</b>
      <c/>
    </a>
  </c>
</a>


<a>
  <c>
    <a>
      <b>not this</b>
    </a>
    <a>
      <b>This node</b> 
    </a>
    <a>
      <b>This node</b> 
    </a>
    <a>
      <b>This node</b> 
    </a>
  </c>
</a>


<d>
  <b>This node</b>
</d>

我想选择文档中没有 //a/c/a[1] 作为祖先节点的所有 b 节点。

I would like to have xpath, that obtains nodes, that don't have ancestor, which is first descendant of specific node.

Let's assume we have xml document like this:

<a>
  <b>This node</b>
  <c>
    <a>
      <b>not this</b>
      <g>
        <b>not this</b>
      </g>
    </a>
    <a>
      <b>This node</b>
      <c/>
    </a>
  </c>
</a>


<a>
  <c>
    <a>
      <b>not this</b>
    </a>
    <a>
      <b>This node</b> 
    </a>
    <a>
      <b>This node</b> 
    </a>
    <a>
      <b>This node</b> 
    </a>
  </c>
</a>


<d>
  <b>This node</b>
</d>

I would like to select all b nodes in document that don't have as their ancestor node //a/c/a[1].

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

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

发布评论

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

评论(1

农村范ル 2024-11-14 02:28:53

我想选择所有b节点
没有作为他们的文件
祖先节点 //a/c/a[1]

使用此 XPath 表达式

//b[not(ancestor::a
             [parent::c[parent::a]
            and
              not(preceding-sibling::a)
             ]
       )
   ]

这会选择文档中不存在的所有 b 元素具有祖先 a,其父级 c 具有父级 a a具有父代 c 的祖先不是其父代的第一个 a 子代。

给定以下 XML 文档(基于所提供的,但格式良好,并且还在应选择的节点中放置了标识文本):

<t>
    <a>
        <b>This node 1</b>
        <c>
            <a>
                <b>not this</b>
                <g>
                    <b>not this</b>
                </g>
            </a>
            <a>
                <b>This node 2</b>
                <c/>
            </a>
        </c>
    </a>
    <a>
        <c>
            <a>
                <b>not this</b>
            </a>
            <a>
                <b>This node 3</b>
            </a>
            <a>
                <b>This node 4</b>
            </a>
            <a>
                <b>This node 5</b>
            </a>
        </c>
    </a>
    <d>
        <b>This node 6</b>
    </d>
</t>

正是想要的 6 b 元素。

使用 XSLT 进行验证

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
 <xsl:copy-of select=
 "//b[not(ancestor::a
             [parent::c[parent::a]
            and
              not(preceding-sibling::a)
             ]
         )
     ]

 "/>
 </xsl:template>
</xsl:stylesheet>

当此转换应用于上述 XML 文档时,将选择所需的 b 元素,并且复制到输出。产生了想要的正确结果

<b>This node 1</b>
<b>This node 2</b>
<b>This node 3</b>
<b>This node 4</b>
<b>This node 5</b>
<b>This node 6</b>

I would like to select all b nodes in
document that don't have as their
ancestor node //a/c/a[1]

Use this XPath expression:

//b[not(ancestor::a
             [parent::c[parent::a]
            and
              not(preceding-sibling::a)
             ]
       )
   ]

This selects all b elements in the document that don't have ancestor a that has a parent c that has parent a and the a ancestor that has parent c is not the first a child of its parent.

Given the following XML document (based on the provided, but made well-formed and also put identifying text in the nodes that should be selected):

<t>
    <a>
        <b>This node 1</b>
        <c>
            <a>
                <b>not this</b>
                <g>
                    <b>not this</b>
                </g>
            </a>
            <a>
                <b>This node 2</b>
                <c/>
            </a>
        </c>
    </a>
    <a>
        <c>
            <a>
                <b>not this</b>
            </a>
            <a>
                <b>This node 3</b>
            </a>
            <a>
                <b>This node 4</b>
            </a>
            <a>
                <b>This node 5</b>
            </a>
        </c>
    </a>
    <d>
        <b>This node 6</b>
    </d>
</t>

exactly the wanted 6 b elements are selected.

Verification using XSLT:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
 <xsl:copy-of select=
 "//b[not(ancestor::a
             [parent::c[parent::a]
            and
              not(preceding-sibling::a)
             ]
         )
     ]

 "/>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the above XML document, exactly the wanted b elements are selected and copied to the output. The wanted, correct result is produced:

<b>This node 1</b>
<b>This node 2</b>
<b>This node 3</b>
<b>This node 4</b>
<b>This node 5</b>
<b>This node 6</b>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文