How do I return a comma-separated list of the ids, please?
<nodes>
<node>
<id>1</id>
<name>idbread</name>
</node>
<node>
<id>2</id>
<name>idbutter</name>
</node>
</nodes>
expected output
1,2
I attempted to use XPath, but concat returns only the first value.
xpath node.xml "//nodes/node/id/text()" 2>/dev/null
returns
12
You can use xmlstarlet for this:
xmlstarlet sel -t -v "/nodes/node[1]/id" -m "/nodes/node[position()>1]" -v "concat(',',id)" input.xml
This outputs the value of the first node/id node and then outputs the following node/ids separated by a comma. The output is as desired.
sel option chooses the Select/Query mode of xmlstarlet-t indicates the start of an "XSLT template"-v option outputs the value of the XPath expression-m option creates a for-each over the XPath expression-v option outputs the value of the XPath expression relative to the context value of the for-eachIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With