XPath - node() 和 text() 之间的区别 [英] XPath - Difference between node() and text()

查看:38
本文介绍了XPath - node() 和 text() 之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解 text()node() 之间的区别.据我了解, text() 将是标签 apple</item> 之间的任何内容,即 apple这个案例.节点将是该节点实际是什么,这将是 item

但后来我被分配了一些工作,它要求我选择生产下所有项目的文本",一个单独的问题要求选择所有部门的所有管理节点"

假设输出看起来如何 text() 而不是 node()

XML 片段:

<item>苹果</item><item>香蕉</item><item>胡椒</item></生产><部门><电话>123-456-7891</电话><经理>约翰</经理></部门>

当然,还有更多的部门和更多的经理,但这只是一小段代码.

任何帮助将不胜感激!

解决方案

text()node()节点测试,在XPath 术语(比较).

节点测试在一个集合上运行(在一个a>,确切地说)节点并返回特定类型的节点.如果未提及轴,则默认采用 child 轴.

有各种节点测试:

  • node() 匹配任何节点(最不具体的节点测试)
  • text() 仅匹配 text 节点
  • comment() 匹配 comment 节点
  • * 匹配任何元素节点
  • foo 匹配任何名为 "foo"
  • 的元素节点
  • processing-instruction() 匹配 PI 节点(它们看起来像 ).
  • 旁注: * 也匹配属性节点,但仅沿 attribute 轴匹配.@*attribute::* 的简写.属性不是 child 轴的一部分,这就是普通 * 不会选择它们的原因.

这个 XML 文档:

<item>苹果</item><item>香蕉</item><item>胡椒</item></生产>

代表以下DOM(简化):

<前>根节点元素节点(名称=生产")文本节点(值= ")元素节点(名称=项目")文本节点(值=苹果")文本节点(值= ")元素节点(名称=项目")文本节点(值=香蕉")文本节点(值= ")元素节点(名称=项目")文本节点(值=胡椒")文本节点(值= ")

XPath 也是如此:

  • / 选择根节点
  • /produce 选择根节点的子元素,如果它的名字是"produce"(这被称为文档元素;它代表文档本身.文档元素和根节点经常被混淆,但它们不是一回事.)
  • /produce/node() 选择 /produce/ 下的任何类型的子节点(即所有 7 个子节点)
  • /produce/text() 选择 4 个 (!) 纯空白文本节点
  • /produce/item[1] 选择名为 "item"
  • 的第一个子元素
  • /produce/item[1]/text() 选择所有 子文本节点(只有一个 - apple" - 在这种情况下)

等等.

那么,你的问题

  • 选择produce下所有item的文本" /produce/item/text()(选择3个节点)
  • 选择所有部门的所有管理节点" //department/manager(选择1个节点)

注意事项

  • XPath 中的默认 axischild 轴.您可以通过为不同的轴名称添加前缀来更改轴.例如://item/ancestor::produce
  • 元素节点具有文​​本值.当您评估元素节点时,将返回其文本内容.在本例中,/produce/item[1]/text()string(/produce/item[1]) 将相同.
  • 另请参阅此答案,其中我以图形方式概述了 XPath 表达式的各个部分.

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"

How is the output suppose to look text() as opposed to node()

Snippet 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 snippet of code.

Any help would be much appreciated!

解决方案

text() and node() are node tests, in XPath terminology (compare).

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)
  • text() matches text nodes only
  • comment() matches comment nodes
  • * matches any element node
  • foo 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 the attribute axis. @* is a shorthand for attribute::*. Attributes are not part of the child axis, that's why a normal * does not select them.

This XML document:

<produce>
    <item>apple</item>
    <item>banana</item>
    <item>pepper</item>
</produce>

represents the following DOM (simplified):

root node
   element node (name="produce")
      text node (value="
    ")
      element node (name="item")
         text node (value="apple")
      text node (value="
    ")
      element node (name="item")
         text node (value="banana")
      text node (value="
    ")
      element node (name="item")
         text node (value="pepper")
      text node (value="
")

So with 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/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)

And so on.

So, your questions

  • "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

  • 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() and string(/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屋!

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