Reputation: 23
I have a xml with several attributes like this:
<results value="1">
<result value="111"></result>
<result value="222"></result>
<result value="333"></result>
<result value="444"></result>
</results>
Are there any way to get all the values of the attributes and concat to every value a constant string? Output like that:
Value: 111
Value: 222
Value: 333
Value: 444
Thank you very much
Upvotes: 2
Views: 300
Reputation: 14723
Yes, that sounds feasible if you relies on XPath 2.0's functions library. You could write something like:
concat('Value: ',string-join((//result/@value), '\nValue: '))
or:
concat('Value: ',string-join((//result/@value), ' Value: '))
depending on how you encode the newline character.
(tested using https://www.freeformatter.com/xpath-tester.html)
Another solution − if you only have a XPath 1.0 parser at hand − would consist in just evaluating //result/@value
, then post-processing the node-list result so obtained in the programming language that you use.
EDIT: if you still need to get a nodelist as a result and only rely on XPath, you should prefer @JackFleeting's answer over my first suggestion.
(BTW I had also thought about the same solution as Jack's first, and tested it on http://www.xpathtester.com/xpath but this didn't work, probably because that online parser is buggy actually).
Upvotes: 1
Reputation: 24930
Using only pure xpath:
//results//result/concat("Value: ", @value)
Output:
Value: 111
Value: 222
Value: 333
Value: 444
Upvotes: 1