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

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

For me it only worked when I used the answer given here: stackoverflow.com/a/11906002/1306012
@gilles-quenot For multiple <eid>s in a single file, if I want to extract dateofbirth for particular nationalnumber.....how should I achieve that?
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?

1 Comment

xmllint: using libxml version 20800

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.