苦苦于xmlstarlet

use*_*928 2 xml xmlstarlet

我有一个非常简单的XML文件,我需要用xmlstarlet解析(我在Windows下):

<?xml version="1.0" encoding="utf-8"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.02" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <pain.001.001.02>
    <GrpHdr>
      <MsgId>IDENTIFYER</MsgId>
      <CreDtTm>2013-01-25T11:15:02</CreDtTm>
      <NbOfTxs>6</NbOfTxs>
      <CtrlSum>6268.07</CtrlSum>
      <Grpg>MIXD</Grpg>
    </GrpHdr>
   <PmtInf>
   </PmtInf>
  </pain.001.001.02>
</Document>
Run Code Online (Sandbox Code Playgroud)

假设我想要检索CtrlSum元素; 我尝试时失败了

xml sel -t -m "/Document/pain.001.001.02/GrpHdr" -v CtrlSum myfile.xml
Run Code Online (Sandbox Code Playgroud)

但是如果我从文件中删除xmlns和xmlns:xsi属性它可以工作(但我不能在现实生活中这样做).我知道我必须使用-N选项,但我无法想象如何做到这一点.任何帮助都是非常有价值的.

非常感谢Laurent(或Lawrence,或Larry :))

npo*_*avs 5

xmlns属性定义了默认命名空间,您在XML文件中看到的每个元素都没有前缀(恰好是您发布的示例中的所有元素)都使用默认命名空间.XPath不允许默认名称空间,因此您必须为其分配前缀-N并在匹配时使用它:

xml sel -N p=urn:iso:std:iso:20022:tech:xsd:pain.001.001.02 -t -v /p:Document/p:pain.001.001.02/p:GrpHdr/p:CtrlSum myfile.xml
Run Code Online (Sandbox Code Playgroud)