选择一个 XML 元素,而不考虑 XPATH 的级别 [英] Select an XML element regardless of level with XPATH

查看:22
本文介绍了选择一个 XML 元素,而不考虑 XPATH 的级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个:

<a>
  <b>
    <t>text</t>
  </b>
</a>
<a>
  <t>text</t>
</a>

所以我想选择文本,不管它在哪里.(请注意,它可以位于不只是 1/2 级以下的任何位置,例如它可以没有父级)

So I want to select the text regardless of where is it. (note that it can be anywhere not just 1/2 levels down, it can have no parents for instance)

这可能吗?

推荐答案

您正在寻找 后代:

You are looking for the descendant axis:

descendant 轴包含上下文节点的后代;一种后代是一个孩子或一个孩子的孩子孩子等;因此后代轴从不包含属性或命名空间节点

the descendant axis contains the descendants of the context node; a descendant is a child or a child of a child and so on; thus the descendant axis never contains attribute or namespace nodes

在你的情况下:/descendant:t

当然,正如其他人所回答的那样,对此有一个缩写语法:

Of course, as others have answered, there is an abbreviated syntax for this:

///descendant-or-self::node()/ 的缩写.为了例如,//para/descendant-or-self::node()/child::para因此将选择任何 para 元素在文档中(即使是 para 元素那是一个文档元素将是由 //para 选择,因为文档元素节点是根节点)

// is short for /descendant-or-self::node()/. For example, //para is short for /descendant-or-self::node()/child::para and so will select any para element in the document (even a para element that is a document element will be selected by //para since the document element node is a child of the root node)

这篇关于选择一个 XML 元素,而不考虑 XPATH 的级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆