sarkasper sarkasper - 1 year ago 295
Linux Question

xml_grep get attribute from element

Is it possible (and if so, how) with xml_grep to get the value of a specific attribute from a specific element? I only seem to be able to output the information between tags.


<identity nationalnumber="13021912354" dateofbirth="20130219" gender="male">
<middlenames>Marie J</middlenames>

output the value (in this case: 13021912354) of the 'nationalnumber'-attribute from the 'identity'-element

Answer Source

With xmllint :

xmllint --xpath 'string(//identity/@nationalnumber)' file.xml

For debian distro (or derivatives) xmllint is part of the libxml2-utils package and can be installed so:

apt-get install libxml2-utils

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download