用于选择具有公共属性的所有节点的 XPath 表达式 [英] XPath expression for selecting all nodes with a common attribute

查看:21
本文介绍了用于选择具有公共属性的所有节点的 XPath 表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读的一本关于 XML 的书说要选择 XML 文件中具有特定属性的所有节点,请使用以下语法:

a book I'm reading on XML says that to select all nodes in an XML file that have a specific attribute, use the syntax:

//*/@_attribute_

我不明白为什么需要星号.据我了解,表达式//选择根节点的所有后代.那么,例如,//@lang 不会选择具有名为lang"的属性的根节点的所有后代吗?我什至无法解释上面表达式中星号的含义(我知道星号通常意味着全部").如果有人能为我分解它,我会非常感激.

What I don't understand is why the asterisk is needed. As I understand it, the expression // selects all descendants of the root node. So, wouldn't //@lang, for example, select all descendants of the root node that have an attribute called "lang"? I can't even interpret what the asterisk even means in the above expression (I know the asterisk in general means "all"). If someone could break it down for me I'd really appreciate it.

谢谢

推荐答案

我正在读的一本关于 XML 的书说选择 XML 中的所有节点具有特定属性的文件,使用语法:

Hi, a book I'm reading on XML says that to select all nodes in an XML file that have a specific attribute, use the syntax:

//*/@attribute

这是错误的.它将扩展为:

That's wrong. It will be expanded to:

/descendant-or-self::node()/child::*/attribute::attribute

含义:作为根文档本身或其后代之一的节点的任何子元素的所有attribute属性

您需要:

/descendant::*[attribute::attribute]

或缩写形式

//*[@attribute]

关于*:正式的是名称测试,而不是节点类型测试.在 XPath 1.0 中没有元素类型测试.在 XPath 2.0 中,您有 element().那么,为什么只选择元素呢?好吧,它没有.轴有一个主要节点类型,来自 http://www.w3.org/TR/xpath/#node-tests :

About the *: formaly is a name test not a node type test. In XPath 1.0 there is no element type test. In XPath 2.0 you have element(). So, why select only elements? Well, it doesn't. The axis have a principal node type, from http://www.w3.org/TR/xpath/#node-tests :

每个轴都有一个主节点类型.如果一个轴可以包含元素,那么主节点类型为元素;否则,它是节点的类型轴可以包含的.因此,

Every axis has a principal node type. If an axis can contain elements, then the principal node type is element; otherwise, it is the type of the nodes that the axis can contain. Thus,

  • 对于属性轴,主要节点类型为属性.
  • 对于命名空间轴,主要节点类型是命名空间.
  • 对于其他轴,主要节点类型为元素.

这就是为什么 *,child::*,self::*,descendant::*,etc. 选择元素,但 @*attribute::*namespace::* 选择属性或作用域命名空间.

That's why *,child::*,self::*,descendant::*, etc. selects elements, but @* or attribute::* or namespace::* selects attributes or in scope namespaces.

关于谓词([@attribute] 部分):这个表达式是用最后一步选择的每个节点计算的.它需要一个布尔值进行过滤.节点集的布尔值(这是 attribute::attribute 的结果)对于空节点集为 false,否则为 true.

About predicate (the [@attribute] part): this expression is evaluate with each of the nodes selects by last step. It expects a boolean value for filtering. The boolean value for a node set (this is the result for attribute::attribute) is false for an empty node set, and true otherwise.

这篇关于用于选择具有公共属性的所有节点的 XPath 表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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