简体   繁体   中英

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

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 ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM