如何使用 xmlstarlet 从 xml 打印属性值?

How to print the attribute value from xml using xmlstarlet?

我有一个下面格式的 xml,如何只列出 root/target/parts/name?

下面xml,我需要打印

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>

我找不到正确的选项,我尝试了以下(以及许多其他选项)但它打印了整个部分

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

看起来像你想要的:

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