1

I need a one-line XPath 1.0 expression to calculate the length of a node, but excluding text in any child nodes (if they are present).

For instance, the string-length() of

<node>This is text</node>

is 12 (ok), but the string-length() of

<node>This is<any>any text</any> text</node>
<node>This <any>any text</any>is text</node>

is more than 12 (not ok).

I cannot use string-length(//node/text()), because it takes the first chunk of text inside node, if there are any child nodes inside node.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Cuder
  • 163
  • 2
  • 8

2 Answers2

1

XPath 1.0

XPath 1.0 alone cannot provide a character count of only the text node children of an element. You'll also have to use facilities of the language hosting the XPath library.

XPath 2.0 and up

This XPath (first posted by @MartinHonnen in comments),

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

will provide a character count of all text node children of /node, as requested.

See also

kjhughes
  • 106,133
  • 27
  • 181
  • 240
0

Use this hope will run for 1.0:

string-length(//node)
Amrendra Kumar
  • 1,806
  • 1
  • 7
  • 17