Apojoost
Apojoost

Reputation: 137

Listing ELements from XML using Xmlstarlet select

I have the following XML:

<?xml version="1.0" encoding="utf-8"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.008.001.02" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <CstmrDrctDbtInitn>
    <GrpHdr>
      <MsgId>331-2018-01-01-01:59:01</MsgId>
      <CreDtTm>2018-01-22T12:59:01</CreDtTm>
      <NbOfTxs>1</NbOfTxs>
      <CtrlSum>11.13</CtrlSum>
      <InitgPty>
        <Nm>Company B.V.</Nm>
      </InitgPty>
    </GrpHdr>
    <PmtInf>
      <PmtInfId>331-264-1</PmtInfId>
      <PmtMtd>DD</PmtMtd>
      <BtchBookg>true</BtchBookg>
      <NbOfTxs>1</NbOfTxs>
      <CtrlSum>11.13</CtrlSum>
      <PmtTpInf>
        <SvcLvl>
          <Cd>SEPA</Cd>
        </SvcLvl>
        <LclInstrm>
          <Cd>CORE</Cd>
        </LclInstrm>
        <SeqTp>FRST</SeqTp>
      </PmtTpInf>
      <ReqdColltnDt>2018-01-01</ReqdColltnDt>
      <Cdtr>
        <Nm>Company B.V.</Nm>
        <PstlAdr>
          <Ctry>NL</Ctry>
          <AdrLine>Street 1</AdrLine>
          <AdrLine>2345 AA City</AdrLine>
        </PstlAdr>
      </Cdtr>
      <CdtrAcct>
        <Id>
          <IBAN>NL25RABO0123456789</IBAN>
        </Id>
      </CdtrAcct>
      <CdtrAgt>
        <FinInstnId>
          <BIC>RABONL2U</BIC>
        </FinInstnId>
      </CdtrAgt>
      <ChrgBr>SLEV</ChrgBr>
      <CdtrSchmeId>
        <Id>
          <PrvtId>
            <Othr>
              <Id>NL99ZZZ112233445566</Id>
              <SchmeNm>
                <Prtry>SEPA</Prtry>
              </SchmeNm>
            </Othr>
          </PrvtId>
        </Id>
      </CdtrSchmeId>
      <DrctDbtTxInf>
        <PmtId>
          <EndToEndId>1111C2233444.555.996666</EndToEndId>
        </PmtId>
        <InstdAmt Ccy="EUR">11.13</InstdAmt>
        <DrctDbtTx>
          <MndtRltdInf>
            <MndtId>02001111-0000110</MndtId>
            <DtOfSgntr>2018-01-01</DtOfSgntr>
            <AmdmntInd>false</AmdmntInd>
          </MndtRltdInf>
        </DrctDbtTx>
        <DbtrAgt>
          <FinInstnId>
            <BIC>ABNANL2A</BIC>
          </FinInstnId>
        </DbtrAgt>
        <Dbtr>
          <Nm>Student</Nm>
          <PstlAdr>
            <Ctry>NL</Ctry>
            <AdrLine>Street 1</AdrLine>
            <AdrLine>1111 AA CITY</AdrLine>
          </PstlAdr>
          <Id>
            <OrgId>
              <BICOrBEI>ABNANL2A</BICOrBEI>
            </OrgId>
          </Id>
        </Dbtr>
        <DbtrAcct>
          <Id>
            <IBAN>NL91ABNA0417164300</IBAN>
          </Id>
        </DbtrAcct>
        <RmtInf>
          <Ustrd>1111/ 1000000 Pat P0000100/1200000 XX001155/01200000XX Pat P0000100/1200000 1111 AA CITY</Ustrd>
        </RmtInf>
      </DrctDbtTxInf>
    </PmtInf>
  </CstmrDrctDbtInitn>
</Document>

I want to Listing elements from this XML using Xmlstarlet select.

I tried listing the Ustrd from this file using:

xmlstarlet sel -t -m "//CstmrDrctDbtInitn/PmtInf/DrctDbtTxInf/PmtId" -v EndToEndId -n SDDCore001.xml 

-m //CstmrDrctDbtInitn/PmtInf/DrctDbtTxInf/PmtId - Find element CstmrDrctDbtInitn/PmtInf/DrctDbtTxInf/PmtId at any depth in the XML and for each such element.

-v EndToEndId - print the value of the child element called EndToEndId.

-n - and finish each printed match with a new line

Why doesn't this work?

Upvotes: 1

Views: 352

Answers (1)

har07
har07

Reputation: 89285

The problem is that your XML has default namespace declared at the root element. This means that all elements in your XML are belong to this namespace, and therefore your attempted XPath didn't match any element.

To reference element in namespace using XPath you need a combination of: prefix that references the correct namespace URI, and element's local-name. In xmlstarlet prefix mapping can be defined using -N parameter :

xmlstarlet sel -N d=urn:iso:std:iso:20022:tech:xsd:pain.008.001.02 \
 -t -m "//d:CstmrDrctDbtInitn/d:PmtInf/d:DrctDbtTxInf/d:PmtId" \
 -v d:EndToEndId -o "\n" -n SDDCore001.xml

Upvotes: 3

Related Questions