xml_grep get attribute from element

≯℡__Kan透↙ 提交于 2019-12-12 13:41:58

问题


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


回答1:


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




回答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?



来源:https://stackoverflow.com/questions/14959315/xml-grep-get-attribute-from-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!