I just discovered this morning this cool utility in my /bin directory : xmllint
You can use it to extract values from your xml files within your shell scripts
$ cat foo.xml
$ echo 'cat //emplist/emp[@no="1"]/ename/text()'|
xmllint --shell foo.xml |
sed -n 3p
John
I like this !
sed -n 3p
did not work for me
sed -n 2p
did work!
…or use sed to delete the lines with the “/ > ” prompt?
sed ‘/^\/ >/ d’
Ok, you have got a different input, thanks for your feedback
Pingback: Laurent Schneider » xml and powershell
Can we extract the attribute value using xmllint?
you use /@attr
$ echo 'cat //emplist/emp[@no="1"]/@no'|
xmllint --shell foo.xml |grep '"'|
cut -d'"' -f2
1
and to get the attribute name, use dir
$ echo 'dir //emplist/emp[1]'|
xmllint --shell foo.xml|
awk '$1=="ATTRIBUTE"{print $2}'
no