Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all attributes with same tag name with xmllint xpath

Tags:

xml

xpath

xmllint

Sample xml-

<xml>
<Tag name="attr1"></Tag>
<Tag name="attr2"></Tag>
<Tag name="attr2"></Tag>
</xml>

How can I get values of all the attributes with xmllint, like this-

attr1
attr2
attr3

I can only use xmllint. I have tried this-

xmllint --xpath 'string(//Tag/@name)'

But this only returns first match.

like image 804
some_guy Avatar asked Oct 11 '25 13:10

some_guy


1 Answers

Using string() will only give you the first match in XPath 1.0. If you remove string() you'll get all three attributes, but you'd have to post-process them to get only the values. I suppose this will depend on how you're running xmllint (what os/shell/etc).

Something like (tested with bash in cygwin)...

attrs=$(xmllint --xpath "//Tag/@name" sample.xml)
echo $attrs | sed 's/\s*name="\([^"]*\)"/\1\n/g'

Another option is to first get a count of how many Tag elements and then call xmllint that many times with a positional predicate on Tag.

Something like (tested with bash in cygwin)...

count=$(xmllint --xpath "count(//Tag)" sample.xml)

if [[ $count != 0 ]]; then
    for ((i=1; i<=$count; i++)); do
       echo $(xmllint --xpath "string(//Tag[$i]/@name)" sample.xml)
    done
fi
like image 66
Daniel Haley Avatar answered Oct 15 '25 20:10

Daniel Haley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!