xml_grep get attribute from element
Asked Answered
P

2

5

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.

example:

    <eid>
       <identity nationalnumber="13021912354" dateofbirth="20130219" gender="male">
          <name>Menten</name>
          <firstname>Kasper</firstname>
          <middlenames>Marie J</middlenames>
          <nationality>Belg</nationality>
          <placeofbirth>Sint-Truiden</placeofbirth>
          <photo>base64-string</photo>
       </identity>
    </eid>

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

Pyelitis answered 19/2, 2013 at 13:58 Comment(0)
G
9

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

Gaur answered 19/2, 2013 at 14:6 Comment(2)
For me it only worked when I used the answer given here: https://mcmap.net/q/348663/-how-can-i-get-the-value-from-an-attribute-using-xmllint-and-xpathDais
@gilles-quenot For multiple <eid>s in a single file, if I want to extract dateofbirth for particular nationalnumber.....how should I achieve that?Justly
G
2

The version of xmllint that I have installed on RHEL6.3 doesn't seem to have this magic --xpath option. What version of xmllint provides support for this option?

Germainegerman answered 21/2, 2013 at 15:37 Comment(1)
xmllint: using libxml version 20800Pyelitis

© 2022 - 2024 — McMap. All rights reserved.