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
我无法理解text()和node()之间的区别。根据我的理解,text()将是标签之间的任何东西
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"
但后来我被分配了一些工作,要求我“选择生成的所有项目的文本”,另一个问题是“选择所有部门的所有管理节点”
How is the output suppose to look text()
as opposed to node()
与node()相比,输出应该如何显示text()
Snippet of XML:
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 snippet of code.
当然,有更多的部门和管理人员,但这只是一个代码片段。
Any help would be much appreciated!
如有任何帮助,我们将不胜感激!
2 个解决方案
#1
112
text()
and node()
are node tests, in XPath terminology (compare).
text()和node()是XPath术语中的节点测试。
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.
节点测试在节点的集合(确切地说,在轴上)上操作,并返回特定类型的节点。当没有提到轴时,默认使用子轴。
There are all kinds of node tests:
有各种节点测试:
-
node()
matches any node (the least specific node test of them all) - node()匹配任何节点(所有节点中最不特定的节点测试)
-
text()
matches text nodes only - 文本()只匹配文本节点。
-
comment()
matches comment nodes - 评论()匹配注释节点
-
*
matches any element node - *匹配任何元素节点
-
foo
matches any element node named"foo"
- foo匹配任何名为“foo”的元素节点
-
processing-instruction()
matches PI nodes (they look like<?name value?>
). - 处理指令()匹配PI节点(它们看起来像 )。
-
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. - 边注:*也匹配属性节点,但仅沿着属性轴。@*是属性的缩写::*。属性不是子轴的一部分,这就是为什么普通*不选择它们。
This XML document:
这个XML文档:
<produce>
<item>apple</item>
<item>banana</item>
<item>pepper</item>
</produce>
represents the following DOM (simplified):
表示以下DOM(简化):
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")
So with XPath:
所以与XPath:
-
/
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/node()选择/produce/下的任何类型的子节点(即所有7个子节点)
-
/produce/text()
selects the 4 (!) whitespace-only text nodes - /produce/text()选择4(!)纯白色文本节点
-
/produce/item[1]
selects the first child element named"item"
- 选择第一个名为“item”的子元素
-
/produce/item[1]/text()
selects all child text nodes (there's only one - "apple" - in this case) - /produce/item[1]/text()选择所有子文本节点(在本例中只有一个——“apple”)
And so on.
等等。
So, your questions
所以,你的问题
-
"Select the text of all items under produce"
/produce/item/text()
(3 nodes selected) - “选择生成项下的所有项的文本”/produce/item/text()()(选择3个节点)
-
"Select all the manager nodes in all departments"
//department/manager
(1 node selected) - “选择所有部门的所有经理节点”//部门/经理(选择1个节点)
Notes
笔记
- 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
- XPath中的默认轴是子轴。可以通过在不同的轴名前加上前缀来更改轴名。例如:/ /项目/祖先::生产
- 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. - 元素节点具有文本值。当您评估一个元素节点时,它的文本内容将被返回。在本例中,/produce/item[1]/text()和string(/produce/item[1])将是相同的。
- Also see this answer where I outline the individual parts of an XPath expression graphically.
- 还可以看到这个答案,其中我以图形化的方式概述了XPath表达式的各个部分。
#2
1
Select the text of all items under produce:
选择产品下所有项目的文本:
//produce/item/text()
Select all the manager nodes in all departments:
选择各部门的管理节点:
//department/*
#1
112
text()
and node()
are node tests, in XPath terminology (compare).
text()和node()是XPath术语中的节点测试。
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.
节点测试在节点的集合(确切地说,在轴上)上操作,并返回特定类型的节点。当没有提到轴时,默认使用子轴。
There are all kinds of node tests:
有各种节点测试:
-
node()
matches any node (the least specific node test of them all) - node()匹配任何节点(所有节点中最不特定的节点测试)
-
text()
matches text nodes only - 文本()只匹配文本节点。
-
comment()
matches comment nodes - 评论()匹配注释节点
-
*
matches any element node - *匹配任何元素节点
-
foo
matches any element node named"foo"
- foo匹配任何名为“foo”的元素节点
-
processing-instruction()
matches PI nodes (they look like<?name value?>
). - 处理指令()匹配PI节点(它们看起来像 )。
-
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. - 边注:*也匹配属性节点,但仅沿着属性轴。@*是属性的缩写::*。属性不是子轴的一部分,这就是为什么普通*不选择它们。
This XML document:
这个XML文档:
<produce>
<item>apple</item>
<item>banana</item>
<item>pepper</item>
</produce>
represents the following DOM (simplified):
表示以下DOM(简化):
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")
So with XPath:
所以与XPath:
-
/
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/node()选择/produce/下的任何类型的子节点(即所有7个子节点)
-
/produce/text()
selects the 4 (!) whitespace-only text nodes - /produce/text()选择4(!)纯白色文本节点
-
/produce/item[1]
selects the first child element named"item"
- 选择第一个名为“item”的子元素
-
/produce/item[1]/text()
selects all child text nodes (there's only one - "apple" - in this case) - /produce/item[1]/text()选择所有子文本节点(在本例中只有一个——“apple”)
And so on.
等等。
So, your questions
所以,你的问题
-
"Select the text of all items under produce"
/produce/item/text()
(3 nodes selected) - “选择生成项下的所有项的文本”/produce/item/text()()(选择3个节点)
-
"Select all the manager nodes in all departments"
//department/manager
(1 node selected) - “选择所有部门的所有经理节点”//部门/经理(选择1个节点)
Notes
笔记
- 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
- XPath中的默认轴是子轴。可以通过在不同的轴名前加上前缀来更改轴名。例如:/ /项目/祖先::生产
- 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. - 元素节点具有文本值。当您评估一个元素节点时,它的文本内容将被返回。在本例中,/produce/item[1]/text()和string(/produce/item[1])将是相同的。
- Also see this answer where I outline the individual parts of an XPath expression graphically.
- 还可以看到这个答案,其中我以图形化的方式概述了XPath表达式的各个部分。
#2
1
Select the text of all items under produce:
选择产品下所有项目的文本:
//produce/item/text()
Select all the manager nodes in all departments:
选择各部门的管理节点:
//department/*