4
votes

i have this xml schema , what i want is how to extract the values of all the 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>
2

2 Answers

13
votes
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.
0
votes

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.