Search code examples
shellxmlstarlet

How to print the attribute value from xml using xmlstarlet?


I have an xml in below format, how to just list root/target/parts/name alone?

With below xml, I need to print

wheel
door
<root>
  <target type="CAR">
    <parts name="wheel" year="2020">
      <model type="string">202005</model>
      <city>SanDiego</city>
    </parts>
  </target>
  <target type="VAN">
    <parts name="door" year="2019">
      <model type="string">201906</model>
      <city>la</city>
    </parts>
  </target>
</root>

I couldn't find the right options, I tried below (and many other) but it prints the whole parts

 xmlstarlet sel -t -c "/root/target/parts"  -v "@name" foo.xml

Solution

  • Looks like you want:

    $ xmlstarlet sel -t -v '/root/target/parts/@name' foo.xml
    wheel
    door