简体   繁体   English

如何使用 XSLT 从 XML 中删除元素标记

[英]How to Remove Element Tags from XML Using XSLT

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.我有一个 XML 文件,我需要在其中删除一些元素标签,我的问题是如何使用 XSLT 转换 XML 文件。 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,非常感谢,

I suppose you could do simply:我想你可以简单地做:

XSLT 1.0 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>

With xmlstarlet :使用xmlstarlet

<infile.xml xmlstarlet 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM