I have an XML
document with such structure:
<root>
<parent id="idvalue1" attr1="val1" attr2="val2" ...>
<child attr3="val3" attr4="val4" ... />
<child attr3="val5" attr4="val6" ... />
...
</parent>
<parent id="idvalue2" attr1="val7" attr2="val8" ... />
...
</root>
I want to get a list of all the values of the id
attributes of all the nodes which have it. For now it's safe to assume that only the second level elements will have the id
attribute.
In any case, what would be the optimal way to do this? is it xmllint
, xpath
or xmlstarlet
?
You can use xmlstarlet to output a list:
xmlstarlet sel -t -v "//@id" yourfile.xml
This will output the ID values only, however.
The -t option "creates" an XSLT like suggested in Abel's answer. But the output will only be what you select with the sel
command. The option -v is to print the value of the xpath in quotes. The double slash in the xpath expression calls for all nodes.