1

I am working on XML file like

<bookstore> 
   <book category="cooking"> 
       <title lang="en">Everyday Italian</title> 
       <author>adc</author> 
       <year>2005</year> 
       <price>30.00</price> </book> 
   <book category="children"> 
       <title lang="en">Harry Potter</title> 
       <author>xyz</author> 
       <year>2005</year> 
       <price>29.99</price> </book> 
</bookstore>

Using xmlstarlet on linux

xmlstarlet sel -t -m 'bookstore/author [1]' -n books.xml 

Would give output as "adc", but how can I find the field when I already know the value?

Like I know author tag has value "adc". How should I find bookstore/author [x] the x?

IMSoP
  • 89,526
  • 13
  • 117
  • 169
Solaris
  • 674
  • 7
  • 22

1 Answers1

0

After a bit of searching I found the answer and leaving it in someone else stumbles across same

To select value by field in xmlstarlet use

xmlstarlet -t -c 'bookstore/book[author="adc"]' -n books.xml

Here bookstore/book is complete path of node which contains author [author=value] is used to select nodes only where author has specific value And books.xml is your xml file

I am sure others can explain better but hope you find this of some help

Solaris
  • 674
  • 7
  • 22
  • Your XPath says to select all `bookstore/book` elements with child `author` elements equal to child `adc` elements. You're missing quotes around `adc`. Further, there are many Q/A about how to use XPath in XMLStarlet; see duplicate link for just one example. – kjhughes Dec 22 '17 at 12:17
  • @kjhughes Thnx for help, I have corrected the answer and I added it as I couldn't make use of duplicates – Solaris Dec 22 '17 at 13:33