XPath - node()和text()之间的区别 [英] XPath - Difference between node() and text()
问题描述
我无法理解 text()
和 node()
之间的区别。根据我的理解, text()
将是标签之间的任何东西< item> apple< / item>
这是苹果。节点将是实际上的节点,这将是项目
I'm having trouble understanding the difference between text()
and node()
. From what I understand, text()
would be whatever is in between the tags <item>apple</item>
which is apple in this case. Node would be whatever that node actually is, which would be item
但是,我已经分配了一些工作,要求我选择产品下的所有项目的文本,单独的问题询问选择所有部门的所有经理节点
But then I've been assigned some work where it asks me to "Select the text of all items under produce" and a separate question asks "Select all the manager nodes in all departments"
输出如何看起来 text()
而不是 node()
How is the output suppose to look text()
as opposed to node()
/ p>
snip of XML
<produce>
<item>apple</item>
<item>banana</item>
<item>pepper</item>
</produce>
<department>
<phone>123-456-7891</phone>
<manager>John</manager>
</department>
当然,有更多的部门和更多的经理人,但这只是一段代码。
Of course, there are more departments and more managers, but this was just a snip of code.
任何帮助将不胜感激!
Any help would be much appreciated!
推荐答案
text()
和 )
是节点测试,在XPath术语(比较)中。
text()
and node()
are node tests, in XPath terminology (compare).
节点测试操作在一组(在 轴 ,并且返回一些类型的节点。当没有提到轴时,默认情况下假定 child
轴。
Node tests operate on a set (on an axis, to be exact) of nodes and return the ones that are of a certain type. When no axis is mentioned, the child
axis is assumed by default.
有各种各样的节点测试:
-
node()
匹配任何节点(它们的最小特定节点测试) -
text()
仅匹配文本节点 -
comment()
匹配评论节点 -
*
匹配任何元素节点 -
foo
匹配任何名为的元素节点foo
/ li>
-
processing-instruction()
匹配PI节点(它们看起来像<?name value?> / code>)。
- 旁注:
*
也匹配属性节点,但只能沿属性
轴。@ *
是attribute :: *
的缩写。属性不是child
轴的一部分,这就是为什么一般的*
不会选择它们。
node()
matches any node (the least specific node test of them all)text()
matches text nodes onlycomment()
matches comment nodes*
matches any element nodefoo
matches any element node named"foo"
processing-instruction()
matches PI nodes (they look like<?name value?>
).- Side note: The
*
also matches attribute nodes, but only along theattribute
axis.@*
is a shorthand forattribute::*
. Attributes are not part of thechild
axis, that's why a normal*
does not select them.
此XML文档:
<produce>
<item>apple</item>
<item>banana</item>
<item>pepper</item>
</produce>
代表以下DOM(简化):
represents the following DOM (simplified):
root node
element node (name="produce")
text node (value="\n ")
element node (name="item")
text node (value="apple")
text node (value="\n ")
element node (name="item")
text node (value="banana")
text node (value="\n ")
element node (name="item")
text node (value="pepper")
text node (value="\n")
所以使用XPath:
-
/
选择根节点 -
/ produce
选择根节点的子元素,如果它具有名称produce
(这称为文档元素;它代表文档本身,文档元素和根节点常常被困惑,但它们不一样。) -
/ produce / node()
选择 所有7个孩子) -
/ produce / text()
选择4(!)空白的文本节点 -
/ produce / item [1]
选择名为的第一个子元素item
li>
-
/ produce / item [1] / text()
选择所有子文本节点(只有一个 - 苹果 - 在这种情况下)
/
selects the root node/produce
selects a child element of the root node if it has the name"produce"
(This is called the document element; it represents the document itself. Document element and root node are often confused, but they are not the same thing.)/produce/node()
selects any type of child node beneath/produce/
(i.e. all 7 children)/produce/text()
selects the 4 (!) whitespace-only text nodes/produce/item[1]
selects the first child element named"item"
/produce/item[1]/text()
selects all child text nodes (there's only one - "apple" - in this case)
等等。
所以,您的问题
- 选择产品下的所有项目的文本
/生成/ item / text()
(选择3个节点) - 选择所有部门的所有经理节点
//部门/经理
(选择1个节点)
- "Select the text of all items under produce"
/produce/item/text()
(3 nodes selected) - "Select all the manager nodes in all departments"
//department/manager
(1 node selected)
Notes
- XPath中的默认轴是
子
轴。您可以通过以不同的轴名称前缀来更改轴。例如:// item / ancestor :: produce
- 元素节点具有文本值。评估元素节点时,将返回其文本内容。在这个例子中,
/ produce / item [1] / text()
和string(/ produce / item [1])
将是一样的。 - 另请参阅这个答案,其中我以图形方式概述XPath表达式的各个部分。
- The default axis in XPath is the
child
axis. You can change the axis by prefixing a different axis name. For example://item/ancestor::produce
- Element nodes have text values. When you evaluate an element node, its textual contents will be returned. In case of this example,
/produce/item[1]/text()
andstring(/produce/item[1])
will be the same. - Also see this answer where I outline the individual parts of an XPath expression graphically.
这篇关于XPath - node()和text()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!