从xml文件中提取或grep值

时间:2015-02-27 05:28:26

标签: xml grep redhat xmlstarlet

我正在使用xgrep和xmlgrep从XML文件中找到某些信息(http://pastebin.com/1BJEWVJJ

示例代码段

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE cXML SYSTEM "http://xml.cXML.org/schemas/cXML/1.2.014/cXML.dtd">
<cXML xml:lang="en" payloadID="2015-02-24T14:17:26_3764135.ConfirmationRequest@axis.mincom.com" timestamp="2015-02-24T14:17:26+10:00">
<Header>
<From>
<Credential domain="SpotlessBranchID">
<Identity>
L678</Identity>
</Credential>
<Credential domain="SupplierCustno">
<Identity>
SPOWOC</Identity>
</Credential>
<Credential domain="MincomAxis">
<Identity>
101000026139003</Identity>
</Credential>
</From>
<To>
<Credential domain="SpotlessSAPSupplierID">
<Identity>
0002010354</Identity>
</Credential>
<Credential domain="SpotlessSupplierID">
<Identity>
0002010354</Identity>
</Credential>

我想提取SpotLessSupplierID身份值(例如0002010354)

我尝试了xgrep -s 'Credential:Identity/.*/' PO_3764135.xml但是输出太多了(太多&#34;凭据&#34; s)

<!--         Start of node set (Search: Credential:Identity/.*/)                 -->
<!--         Node   0 in node set               -->
<Credential domain="SpotlessBranchID">
  <Identity>L678</Identity>
</Credential>

<!--         Node   1 in node set               -->
<Credential domain="SupplierCustno">
  <Identity>SPOWOC</Identity>
</Credential>

<!--         Node   2 in node set               -->
<Credential domain="MincomAxis">
  <Identity>101000026139003</Identity>
</Credential>

<!--         Node   3 in node set               -->
<Credential domain="SpotlessSAPSupplierID">
  <Identity>0002010354</Identity>
</Credential>

<!--         Node   4 in node set               -->
<Credential domain="SpotlessSupplierID">
  <Identity>0002010354</Identity>
</Credential>

<!--         Node   5 in node set               -->
<Credential domain="MincomAxis">
  <Identity>101000321896001</Identity>
</Credential>

<!--         Node   6 in node set               -->
<Credential domain="SpotlessBranchID">
  <Identity>L678</Identity>
</Credential>

<!--         Node   7 in node set               -->
<Credential domain="MincomAxisTPID">
  <Identity>101000026139</Identity>
</Credential>

<!--         End of node set                    -->

1 个答案:

答案 0 :(得分:1)

XmlStarlet 是这些案例中非常有用的工具,您可以下载并试试这个:

xmlstarlet sel -t -m "//header/To/Credential[@domain = 'SpotlessSAPSupplierID']" -v Identity -n /path_to_your_xml_file

用于文档:这是一个最佳链接: XmlStarlet