从具有相似块的 XML 文件中提取数据

Extract data from XML file with similar Blocks

来源

<?xml version="1.0"?>
 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
      <lima2t:runlargeResponse xmlns:lima2t="uri://Metadata.com/home/1.0">
        <data>
          <capacity>
              <name>Everyday.Italian.Dish</name>
              <currency>($)</currency>
              <CashAmount>not.used.for.this.transaction</CashAmount>
              <CheckAmount>not.used.for.this.transaction</CheckAmount>
              <CreditAmount>not.used.for.this.transaction</CreditAmount>
          </capacity>
          <capacity>
              <name>Everyday.French.Dish</name>
              <currency>($)</currency>
              <CashAmount>not.used.for.this.transaction</CashAmount>
              <CheckAmount>not.used.for.this.transaction</CheckAmount>
              <CreditAmount>134,70</CreditAmount>
          </capacity>
      </data>
    </lima2t:runlargeResponse>
  </soap:Body>
</soap:Envelope>

目标 摘录:

134,70 来自 <CreditAmount>134,70</CreditAmount>

我试过这个命令,但是这个命令输出了屏幕上的所有内容

xmllint --path '//soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"/soap:Body/lima2t:runlargeResponse xmlns:lima2t="uri://Metadata.com/home/1.0"/data/capacity/@name="Everyday.French.Dish"/CreditAmount/text()' op.xml

总觉得少了点什么,但又找不到

像这样:

xmllint --xpath '
    //data/capacity[name="Everyday.French.Dish"]/CreditAmount/text()
' op.xml

或使用shell开关(需要额外的post处理来清除垃圾):

xmllint --shell op.xml<<EOF
cat //data/capacity[name="Everyday.French.Dish"]/CreditAmount/text()
EOF

或使用:

xmlstarlet sel -t -v '
    //data/capacity[name="Everyday.French.Dish"]/CreditAmount/text()
' op.xml

输出:

134,70