souser
souser

Reputation: 6124

parse xml using xmllint

I have an xml file as shown below. I want to use xmllint to parse it such that if the category is "SciFi", it will display the "isbn" number.

After struggling a bit I could figure out how to print only the category or only the isbn number ; but cannot figure out how to add the condition.

<book>
 <name>
  <category>SciFi</category>
  <isbn>12345678</isbn>
 </name>
 <name>
  <category>Mystery</category>
  <isbn>23456789</isbn>
 </name>
<book>

Edit : The version of xmllint I have does not support xpath

I made a mistake in my original post ; corrected xml above

xmllint --version produces

xmllint: using libxml version 20626

I use the following commands to print the information

cat /book/name[1]/category
cat /book/name[2]/category

cat /book/name[1]/isbn
cat /book/name[2]/isbn

Upvotes: 0

Views: 3090

Answers (1)

Mike Sokolov
Mike Sokolov

Reputation: 7054

This sounds like a poor tool for the purpose. If you have xmllint installed, do you have xsltproc? That would be more useful, I think.

Also, just to be sure, have you tried cat /book/name[category='SciFi']/isbn ?

Upvotes: 0

Related Questions