Home > Enterprise >  How to Remove Element Tags from XML Using XSLT
How to Remove Element Tags from XML Using XSLT

Time:10-19

I have an XML file where I need to remove some element tags, my question is how would I go about transforming the XML file using XSLT. How would I go about selecting a specific block of tags to remove in this example here -

   <?xml version="1.0" encoding="UTF-8"?>
<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03">
    <CstmrCdtTrfInitn>
        <GrpHdr>
            <MsgId>1234</MsgId>
            <CreDtTm>2021-09-03T12:35:50</CreDtTm>
            <NbOfTxs>7</NbOfTxs>
            <CtrlSum>999</CtrlSum>
            <InitgPty>
                <Nm>TestName</Nm>
                <Id>
                    <OrgId>
                        <Othr>
                            <Id>ABC1234</Id>
                        </Othr>
                    </OrgId>
                </Id>
            </InitgPty>
        </GrpHdr>
        <PmtInf>
            <PmtInfId>1234</PmtInfId>
            <PmtMtd>TRF</PmtMtd>
            <BtchBookg>true</BtchBookg>
            <NbOfTxs>7</NbOfTxs>
            <CtrlSum>1234</CtrlSum>
            <PmtTpInf>
                <InstrPrty>NORM</InstrPrty>
                <SvcLvl>
                    <Cd>SEPA</Cd>
                </SvcLvl>
            </PmtTpInf>
            <ReqdExctnDt>2021-09-07</ReqdExctnDt>
            <Dbtr>
                <Nm>Test</Nm>
                <PstlAdr>
                    <Ctry>GB</Ctry>
                    <AdrLine>TEST</AdrLine>
                    <AdrLine>Test</AdrLine>
                </PstlAdr>
            </Dbtr>
            <DbtrAcct>
                <Id>
                    <IBAN>1234</IBAN>
                </Id>
                <Ccy>EUR</Ccy>
            </DbtrAcct>
            <DbtrAgt>
                <FinInstnId>
                    <BIC>1234</BIC>
                </FinInstnId>
            </DbtrAgt>
            <ChrgBr>SLEV</ChrgBr>
            <CdtTrfTxInf>
                <PmtId>
                    <InstrId>1234</InstrId>
                    <EndToEndId>1234</EndToEndId>
                </PmtId>
                <Amt>
                    <InstdAmt Ccy="EUR">999</InstdAmt>
                </Amt>
                <CdtrAgt>
                    <FinInstnId>
                        <BIC>1234</BIC>
                    </FinInstnId>
                </CdtrAgt>
                <Cdtr>
                    <Nm> TEST</Nm>
                    <PstlAdr>
                        <Ctry>NL</Ctry>
                        <AdrLine>33</AdrLine>
                        <AdrLine>TEST</AdrLine>
                    </PstlAdr>
                </Cdtr>
                <CdtrAcct>
                    <Id>
                        <IBAN>TEST</IBAN>
                    </Id>
                </CdtrAcct>
                <RmtInf>
                    <Ustrd>FBB</Ustrd>
                </RmtInf>
            </CdtTrfTxInf>
            <CdtTrfTxInf>
                <PmtId>
                    <InstrId>1234</InstrId>
                    <EndToEndId>1234</EndToEndId>
                </PmtId>
                <Amt>
                    <InstdAmt Ccy="EUR">1234</InstdAmt>
                </Amt>
                <CdtrAgt>
                    <FinInstnId>
                        <BIC>1234</BIC>
                    </FinInstnId>
                </CdtrAgt>
                <Cdtr>
                    <Nm> Test</Nm>
                    <PstlAdr>
                        <Ctry>NL</Ctry>
                        <AdrLine> 77</AdrLine>
                        <AdrLine>TEST</AdrLine>
                    </PstlAdr>
                </Cdtr>
                <CdtrAcct>
                    <Id>
                        <IBAN>1234</IBAN>
                    </Id>
                </CdtrAcct>
                <RmtInf>
                    <Ustrd>1234</Ustrd>
                </RmtInf>
            </CdtTrfTxInf>
            <CdtTrfTxInf>
                <PmtId>
                    <InstrId>1234</InstrId>
                    <EndToEndId>1234</EndToEndId>
                </PmtId>
                <Amt>
                    <InstdAmt Ccy="EUR">1234</InstdAmt>
                </Amt>
                <CdtrAgt>
                    <FinInstnId>
                        <BIC>1234</BIC>
                    </FinInstnId>
                </CdtrAgt>
                <Cdtr>
                    <Nm>Test.</Nm>
                    <PstlAdr>
                        <Ctry>NL</Ctry>
                        <AdrLine>Test</AdrLine>
                        <AdrLine>Test</AdrLine>
                    </PstlAdr>
                </Cdtr>
                <CdtrAcct>
                    <Id>
                        <IBAN>1234</IBAN>
                    </Id>
                </CdtrAcct>
                <RmtInf>
                    <Ustrd>1234</Ustrd>
                </RmtInf>
            </CdtTrfTxInf>
            <CdtTrfTxInf>
                <PmtId>
                    <InstrId>1234</InstrId>
                    <EndToEndId>1234</EndToEndId>
                </PmtId>
                <Amt>
                    <InstdAmt Ccy="EUR">1234</InstdAmt>
                </Amt>
                <CdtrAgt>
                    <FinInstnId>
                        <BIC>1234</BIC>
                    </FinInstnId>
                </CdtrAgt>
                <Cdtr>
                    <Nm> Test</Nm>
                    <PstlAdr>
                        <Ctry>NL</Ctry>
                        <AdrLine>Test</AdrLine>
                        <AdrLine>Test</AdrLine>
                    </PstlAdr>
                </Cdtr>
                <CdtrAcct>
                    <Id>
                        <IBAN>Test</IBAN>
                    </Id>
                </CdtrAcct>
                <RmtInf>
                    <Ustrd>Test</Ustrd>
                </RmtInf>
            </CdtTrfTxInf>
            <CdtTrfTxInf>
                <PmtId>
                    <InstrId>Test</InstrId>
                    <EndToEndId>Test</EndToEndId>
                </PmtId>
                <Amt>
                    <InstdAmt Ccy="EUR">1234</InstdAmt>
                </Amt>
                <CdtrAgt>
                    <FinInstnId>
                        <BIC>1234</BIC>
                    </FinInstnId>
                </CdtrAgt>
                <Cdtr>
                    <Nm>Test </Nm>
                    <PstlAdr>
                        <Ctry>Test</Ctry>
                        <AdrLine> 1234</AdrLine>
                        <AdrLine>Test</AdrLine>
                    </PstlAdr>
                </Cdtr>
                <CdtrAcct>
                    <Id>
                        <IBAN>1234</IBAN>
                    </Id>
                </CdtrAcct>
                <RmtInf>
                    <Ustrd>1234</Ustrd>
                </RmtInf>
            </CdtTrfTxInf>
            <CdtTrfTxInf>
                <PmtId>
                    <InstrId>1234</InstrId>
                    <EndToEndId>1234</EndToEndId>
                </PmtId>
                <Amt>
                    <InstdAmt Ccy="EUR">1234</InstdAmt>
                </Amt>
                <CdtrAgt>
                    <FinInstnId>
                        <BIC>1234</BIC>
                    </FinInstnId>
                </CdtrAgt>
                <Cdtr>
                    <Nm> Test</Nm>
                    <PstlAdr>
                        <Ctry>NL</Ctry>
                        <AdrLine>Test</AdrLine>
                        <AdrLine>Test</AdrLine>
                    </PstlAdr>
                </Cdtr>
                <CdtrAcct>
                    <Id>
                        <IBAN>Test</IBAN>
                    </Id>
                </CdtrAcct>
                <RmtInf>
                    <Ustrd>Test</Ustrd>
                </RmtInf>
            </CdtTrfTxInf>
            <CdtTrfTxInf>
                <PmtId>
                    <InstrId>Test</InstrId>
                    <EndToEndId>1234</EndToEndId>
                </PmtId>
                <Amt>
                    <InstdAmt Ccy="EUR">1234</InstdAmt>
                </Amt>
                <CdtrAgt>
                    <FinInstnId>
                        <BIC>1234</BIC>
                    </FinInstnId>
                </CdtrAgt>
                <Cdtr>
                    <Nm>Test</Nm>
                    <PstlAdr>
                        <Ctry>Test</Ctry>
                        <AdrLine>Test</AdrLine>
                        <AdrLine>Test</AdrLine>
                    </PstlAdr>
                </Cdtr>
                <CdtrAcct>
                    <Id>
                        <IBAN>1234</IBAN>
                    </Id>
                </CdtrAcct>
                <RmtInf>
                    <Ustrd>1234</Ustrd>
                </RmtInf>
            </CdtTrfTxInf>
        </PmtInf>
    </CstmrCdtTrfInitn>
</Document>

I would essentially like to delete the following tags -

               <Id>
                   <OrgId>
                      <Othr>
                         <Id>ABC1234</Id>
                      </Othr>
                   </OrgId>
              </Id>

If you require anything else please let me know.

Many Thanks,

CodePudding user response:

I suppose you could do simply:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="ns0:Id"/>

</xsl:stylesheet>

CodePudding user response:

With xmlstarlet:

<infile.xml xml ed -N ns=urn:iso:std:iso:20022:tech:xsd:pain.001.001.03 \
                   -d 'ns:Document/ns:CstmrCdtTrfInitn/ns:GrpHdr/ns:InitgPty/ns:Id' > outfile.xml
  • Related