简体   繁体   English

用于选择具有公共属性的所有节点的XPath表达式

[英]XPath expression for selecting all nodes with a common attribute

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: 我正在阅读XML的一本书说,要选择XML文件中具有特定属性的所有节点,请使用以下语法:

//*/@_attribute_

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"? 那么,不会// @ lang,例如,选择具有名为“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. 如果有人可以为我分解,我真的很感激。

Thanks 谢谢

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: 嗨,我正在读XML的书说要选择XML文件中具有特定属性的所有节点,请使用以下语法:

//*/@attribute

That's wrong. 那是错的。 It will be expanded to: 它将扩展到:

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

Meaning: All attribute attributes of any element child of a node being the root document itself or one of its descendats 含义: 节点的任何元素子节点的所有attribute属性都是根文档本身或其后代之一

You need: 你需要:

/descendant::*[attribute::attribute]

Or the abbreviated form 或缩写形式

//*[@attribute]

About the * : formaly is a name test not a node type test . 关于* :formaly是名称测试而不是节点类型测试 In XPath 1.0 there is no element type test. 在XPath 1.0中,没有元素类型测试。 In XPath 2.0 you have element() . 在XPath 2.0中,您有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 : 轴具有主节点类型,来自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, 从而,

  • For the attribute axis, the principal node type is attribute. 对于属性轴,主节点类型是属性。
  • For the namespace axis, the principal node type is namespace. 对于名称空间轴,主节点类型是名称空间。
  • For other axes, the principal node type is element. 对于其他轴,主节点类型是元素。

That's why * , child::* , self::* , descendant::* , etc. selects elements, but @* or attribute::* or namespace::* selects attributes or in scope namespaces. 这就是为什么*child::*self::*descendant::*等选择元素,但@*attribute::*namespace::*选择属性或在范围名称空间中。

About predicate (the [@attribute] part): this expression is evaluate with each of the nodes selects by last step. 关于谓词( [@attribute]部分):评估此表达式,每个节点按最后一步选择。 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. 对于空节点集,节点集的布尔值(这是attribute::attribute的结果)为false,否则为true。

The title of this question is : 这个问题的标题是

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

However nowhere does the text of the question discuss how tho find all nodes that have a common attribute -- so the title may be incorrect. 然而,问题的文本没有讨论如何找到具有共同属性的所有节点 - 因此标题可能不正确。

To find all nodes that have a common attribute named x (BTW, only element-nodes can have attributes), use : 要查找具有名为x的公共属性的所有节点 (BTW,只有元素节点可以具有属性),请使用

//*[@x]

Use : 用途

//@x

to select all attributes named x in the XML document. 选择XML文档中名为x所有属性。 This is probably the shortest expression to do so. 这可能是最短的表达方式。

There is nothing wrong with : 没有错

//*/@x

except that it is slightly longer. 除了它稍长。

It is a shorthand for : 这是一个简写

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

and also selects all x attributes in the XML document. 并选择XML文档中的所有x属性。

Someone may think that this expression doesn't select the x attribute of the top element in the document. 有人可能认为这个表达式没有选择文档中top元素的x属性。 This is a wrong conclusion, because the first location step: 这是一个错误的结论,因为第一个位置步骤:

/descendant-or-self::node()

selects every node in the document, including the root ( / ) itself. 选择文档中的每个节点 ,包括/ )本身。

This means that: 这意味着:

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

selects every element, including the top element (which is the only child of the root node in a well-formed XML document). 选择每个元素,包括top元素(在格式良好的XML文档中是根节点的唯一子元素)。

So, when the last location step /@x is finally added, this will select all the x attributes of all nodes selected so far by the first two location steps -- that is all x attributes of all element-nodes in the XML document. 所以,当最后一个位置一步/@x最后加入,这将选择所有x由前两个位置到此为止选择的所有节点的属性-这是所有x的XML文档中的所有元素节点的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM