2

Using XPath 1.0 want to get list of text nodes applying XPath 'substring' function on every text node

substring(//p/text(), 10) 

gives only one first text's sub-string, when

//p/text()

gives all of them, but want all sub-strings as set

EDIT:

Tried

//p/substring(text(), 10)

Says invalid XPath expression

How can I achieve this ?

Thanks in advance

JLRishe
  • 99,490
  • 19
  • 131
  • 169
  • What you're describing is not possible in a single XPath 1.0 expression without the use of custom functions. In what kind of context are you using this XPath (XSLT, .NET, Java, etc.)? Please show us the code where you are invoking this XPath. – JLRishe Jan 19 '17 at 08:26
  • Of course i can do this from code, but my current code is general and want to do this using XPath, thanks for answer, will work on the solution you described :) – Ioseb Khutsishvili Jan 19 '17 at 08:30
  • EXSLT defines a function that would allow you to do this: [dyn:map(//p/text, 'substring(., 10)')](http://exslt.org/dyn/functions/map/index.html). That page says there are no known XSLT processors or 3rd party libraries that support that function, but if your XPath engine supports it or something similar, you could use that. – JLRishe Jan 19 '17 at 08:33
  • Thanks for answer, will try now – Ioseb Khutsishvili Jan 19 '17 at 08:41

1 Answers1

2

If you want a set of strings as the result of an XPath 1.0 expression, then you're out of luck, because there is no such data type in XPath 1.0: the only collections available are collections of nodes, and you can only select nodes that already exist, you can't create new ones.

With XPath 2.0 this is a piece of cake:

//p/text()/substring(., 10)

So if you possibly can, find yourself an XPath 2.0 processor.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164