Reputation: 5372
Based on help from other questions I've asked, I've got this groovy snippet:
NodeList nodes = (NodeList)xpath.evaluate( xpathQuery, records, XPathConstants.NODESET );
return nodes.collect { node -> node.getTextContent() }
Which allows me to perform xpathQuery
on records
and get the result.
What I want to do now is just return (as a string) the raw xml of the result (rather than the text content) (I realise this will not result in a valid xml document).
Such that:
xml = "<root><apple><color>RED</color></apple>…</root>"
xpathQuery = "/root/apple[1]"
will return:
"<apple><color>RED</color></apple>"
(Without the enclosing <apple>
tags would also be fine). Is there a simple way to do this?
Or failing that, is there another way to achieve this?
Upvotes: 2
Views: 1808
Reputation: 3219
This isn't very elegant, but could possibly be useful:
Use this:
concat('<apple><color>', //root/apple[1]/color, '</color></apple>')
If you know the structure won't change, or for a more complicated unknown number of child elements within <apple>
, use:
//root/apple[1]/node()
to give you all children nodes, and use groovy's .each function to iterate through the nodes, and create your string paddings, and combine them with the original //root/apple[1]/color
result.
Upvotes: 1