没有子节点的节点的 XPath string-length()

XPath's string-length() of a node without its children

我需要一个单行 XPath 1.0 表达式来计算节点的长度,但不包括任何子节点中的文本(如果它们存在)。

例如

的string-length()
<node>This is text</node>

是12(ok),但是

的string-length()
<node>This is<any>any text</any> text</node>
<node>This <any>any text</any>is text</node>

大于 12(不正常)。

我不能使用 string-length(//node/text()),因为如果 node.

中有任何子节点,它会占用 node 中的第一个文本块

使用此希望将 运行 for 1.0:

string-length(//node)

XPath 1.0

XPath 1.0 无法单独提供元素的文本节点子元素的字符数。您还必须使用托管 XPath 库的语言的功能。

XPath 2.0 及更高版本

这个 XPath(@MartinHonnen 最先发布 ),

sum(/node/text()/string-length())

将根据要求提供 /node 的所有文本节点子节点的字符数。

另见