wissem46
wissem46

Reputation: 403

extracting nodes values with xmlstarlet

I have this XML schema, what I want is how to extract the values of all the <imageName> nodes one by one, using XMLStarlet, in shell script

     <service>
        <imageScroll>
           <imageName>Photo_Gallerie_1.jpg</imageName>
        </imageScroll>
        <imageScroll>
           <imageName>Photo_Gallerie_2.jpg</imageName>
        </imageScroll>
        <imageScroll>
           <imageName>Photo_Gallerie_3.jpg</imageName>
        </imageScroll>
      </service>

Upvotes: 6

Views: 12151

Answers (2)

wissem46
wissem46

Reputation: 403

that was the solution that i found and it did perfectly the job.

imagescroller=`xmlstarlet sel -t -m "//root/services/service/imageScroll[rank_of_the_desired_item]" -v imageName -n myfile.xml

sorry for late.

Upvotes: 0

Dustin Glover
Dustin Glover

Reputation: 176

xmlstarlet sel -t -m "//imageName" -v . -n your.xml

output:

Photo_Gallerie_1.jpg
Photo_Gallerie_2.jpg
Photo_Gallerie_3.jpg

Is that what you needed?

  • sel (select mode)
  • -t (output template(this is pretty much required)
  • -m (for each match of the following value)
  • "// (the double slash means it could be anywhere in the tree)
  • imageName (name of node you want)"
  • -v (requests the value of an element in the current path) and the . represents current element in iteration (you could put the name of the node there but it's generally easier this way) and then the
  • -n is to add a line for every value you match.

Upvotes: 15

Related Questions