text() 和 string() 的区别 [英] Difference between text() and string()

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

问题描述

谁能解释一下 text() 和 string() 函数之间的区别.我经常和另一个一起使用,但没有任何区别,两者都会得到xml节点的字符串值.

Can someone explain the difference between text() and string() functions. I often use one with other, but it does not make any difference, both will get the string value of the xml node.

推荐答案

谁能解释一下 text() 和 string() 的区别功能.

Can someone explain the difference between text() and string() functions.

我.text() 不是一个函数,而是一个 节点测试.

I. text() isn't a function but a node test.

用于选择上下文节点的所有文本节点子节点.

It is used to select all text-node children of the context node.

因此,如果上下文节点是名为 x 的元素,则 text() 选择 x 的所有文本节点子节点.

So, if the context node is an element named x, then text() selects all text-node children of x.

其他示例:

/a/b/c/text()

选择作为任何 b 元素的子元素的任何 c 元素的所有文本节点子元素,该元素是顶部元素 a 的子元素>.

selects all text-node children of any c element that is a child of any b element that is a child of the top element a.

二.string() 函数强>

根据定义 string(exprSelectingASingleNode) 返回 节点的字符串值.

By definition string(exprSelectingASingleNode) returns the string value of the node.

元素的字符串值是其所有文本节点后代的串联——按文档顺序.

The string value of an element is the concatenation of all of its text-node descendents -- in document order.

因此,如果在以下 XML 文档中:

<a>
  <b>2</b>
  <c>3
    <d>4</d>
  </c>
  5
</a>

string(/a) 返回(不带引号):

"
  2
  3
    4

  5
"

如我们所见,字符串值反映了三个纯空白文本节点,我们通常没有注意到和考虑.

As we see, the string value reflects three white-space-only text-nodes, which we typically fail to notice and account for.

一些 XML 解析器可以选择去除纯空白文本节点.如果上面的文档是在去除了纯空白文本节点的情况下解析的,那么相同的功能:

Some XML parsers have the option of stripping-off white-space-only text nodes. If the above document was parsed with the white-space-only text nodes stripped off, then the same function:

string(/a)

现在返回:

"23
    4
  5
"

这篇关于text() 和 string() 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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