Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the "translate" Xpath function on a node-set

Tags:

xpath

I have an XML document that contains items with dashes I'd like to strip

e.g.

<xmlDoc>
   <items>
      <item>a-b-c</item>
      <item>c-d-e</item>
   <items>
</xmlDoc>

I know I can find-replace a single item using this xpath

/xmldoc/items/item[1]/translate(text(),'-','')

Which will return

"abc"

however, how do I do this for the entire set?

This doesn't work

/xmldoc/items/item/translate(text(),'-','')

Nor this

translate(/xmldoc/items/item/text(),'-','')

Is there a way at all to achieve that?

like image 721
Eran Medan Avatar asked Jun 30 '11 18:06

Eran Medan


1 Answers

I know I can find-replace a single item using this xpath

/xmldoc/items/item[1]/translate(text(),'-','')

Which will return

"abc"

however, how do I do this for the entire set?

This cannot be done with a single XPath 1.0 expression.

Use the following XPath 2.0 expression to produce a sequence of strings, each being the result of the application of the translate() function on the string value of the corresponding node:

/xmlDoc/items/item/translate(.,'-', '')
like image 64
Dimitre Novatchev Avatar answered Sep 23 '22 03:09

Dimitre Novatchev