简体   繁体   English

text()和string()之间的区别

[英]Difference between text() and string()

Can someone explain the difference between text() and string() functions. 有人可以解释text()和string()函数之间的区别。 I often use one with other, but it does not make any difference, both will get the string value of the xml node. 我经常使用其中一个,但它没有任何区别,两者都将获得xml节点的字符串值。

Can someone explain the difference between text() and string() functions. 有人可以解释text()和string()函数之间的区别。

I. text() isn't a function but a node test . I. text()不是函数,而是节点测试

It is used to select all text-node children of the context node. 它用于选择上下文节点的所有文本节点子节点。

So, if the context node is an element named x , then text() selects all text-node children of x . 因此,如果上下文节点是名为x的元素,则text()选择x所有文本节点子节点。

Other examples : 其他例子

/a/b/c/text()

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 . 选择任何c元素的所有文本节点子元素,该元素是作为顶部元素a的子元素的任何b元素的子元素。

II. II。 The string() function string()函数

By definition string(exprSelectingASingleNode) returns the string value of the node. 根据定义, string(exprSelectingASingleNode)返回节点的字符串值

The string value of an element is the concatenation of all of its text-node descendents -- in document order. 元素的字符串值是所有文本节点后代的串联 - 按文档顺序排列。

Therefore, if in the following XML document : 因此,如果在以下XML文档中

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

string(/a) returns (without the surrounding quotes): 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. 正如我们所看到的,字符串值反映了三个仅限空格的文本节点,我们通常无法注意到这些节点。

Some XML parsers have the option of stripping-off white-space-only text nodes. 一些XML解析器可以选择剥离仅限空白的文本节点。 If the above document was parsed with the white-space-only text nodes stripped off, then the same function: 如果上面的文档是使用剥离的仅空白文本节点解析的,那么相同的函数:

string(/a)

now returns: 现在返回:

"23
    4
  5
"

Most of the time, if you want the content of an element node X, you can refer to it as ".", if it's the context node, or as "X" if it's a child of the context node. 大多数情况下,如果您想要元素节点X的内容,您可以将其称为“。”,如果它是上下文节点,或者如果它是上下文节点的子节点则称为“X”。 For example: 例如:

<xsl:if test="X = 'abcd'">...

or 要么

<xsl:value-of select="."/>

In both cases, because the context demands a string, the string() function is applied automatically. 在这两种情况下,由于上下文需要字符串,因此会自动应用string()函数。 (That's a slight simplification, if you're running schema-aware XSLT 2.0 the rules are a little more complicated). (这是一个小小的简化,如果你正在运行模式感知的XSLT 2.0,规则会稍微复杂一些)。

Using " string() " here is unnecessary, because it's done automatically; 这里使用“ string() ”是不必要的,因为它是自动完成的; and using text() is a mistake (one that seems to be increasingly common, encouraged by some bad tutorials on the web). 并且使用text()是一个错误(一个似乎越来越普遍的错误,受到网络上一些不好的教程的鼓励)。 Using ./text() or X/text() in this situation gives you all the text node children of the element. 在这种情况下使用./text()X/text()会为您提供元素的所有文本节点子节点。 Often the element has one text node child whose string value happens to be the same as the string value of the element, but your code fails if someone adds a comment or processing instruction, because the value is then split into multiple text nodes. 元素通常有一个文本节点子节点,其字符串值恰好与元素的字符串值相同,但如果有人添加注释或处理指令,则代码会失败,因为该值会被分割为多个文本节点。 It also fails if the element is one (say " title ") that allows mixed content: string(title) and title/text() are going to give the same answer until you hit an article with the title 如果元素是一个(例如“ title ”)允许混合内容,它也会失败: string(title)title/text()将给出相同的答案,直到您点击带有标题的文章

<title>On the wetness of H<sub>2</sub>O</title>

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

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