Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
569 views
in Technique[技术] by (71.8m points)

xslt - XSL Copy nodes without xmlns

I have a specific problem. I have to transform an XML structure to other, where the base XSD is same, different only the namespace definition. The first part is simple, because here I have to use field-mapping. The second part is the simple copy. And here is the problem. The copied "main" node contains the original xmlns attribute. I need to remove this "attribute".

The base xml:

<?xml version="1.0" encoding="UTF-8"?>
<S2SCTIcf:SCTIcfBlkCredTrf xmlns:S2SCTIcf="urn:S2SCTIcf:xsd:$SCTIcfBlkCredTrf"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="urn:S2SCTIcf:xsd:$SCTIcfBlkCredTrf SCTIcfBlkCredTrf.xsd">
    <S2SCTIcf:SndgInst>XXXXXXX0</S2SCTIcf:SndgInst>
    <S2SCTIcf:RcvgInst>YYYYYYY0</S2SCTIcf:RcvgInst>
    <S2SCTIcf:FileRef>2013111200800546</S2SCTIcf:FileRef>
    <S2SCTIcf:SrvcId>SCT</S2SCTIcf:SrvcId>
    <S2SCTIcf:TstCode>T</S2SCTIcf:TstCode>
    <S2SCTIcf:FType>ICF</S2SCTIcf:FType>
    <S2SCTIcf:FDtTm>2013-11-12T16:26:31</S2SCTIcf:FDtTm>
    <S2SCTIcf:NumCTBlk>1</S2SCTIcf:NumCTBlk>
    <S2SCTIcf:NumPCRBlk>0</S2SCTIcf:NumPCRBlk>
    <S2SCTIcf:NumRFRBlk>0</S2SCTIcf:NumRFRBlk>
    <S2SCTIcf:NumROIBlk>0</S2SCTIcf:NumROIBlk>
    <S2SCTIcf:FIToFICstmrCdtTrf xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02">
        <GrpHdr>
            <MsgId>xxddccxxaaa</MsgId>
            <CreDtTm>2013-11-12T16:26:31</CreDtTm>
            <NbOfTxs>1</NbOfTxs>
            <TtlIntrBkSttlmAmt Ccy="EUR">469.12</TtlIntrBkSttlmAmt>
            <IntrBkSttlmDt>2013-11-13</IntrBkSttlmDt>
            <SttlmInf>
                <SttlmMtd>CLRG</SttlmMtd>
                <ClrSys>
                    <Prtry>ST2</Prtry>
                </ClrSys>
            </SttlmInf>
            <InstgAgt>
                <FinInstnId>
                    <BIC>XXXXXXX0</BIC>
                </FinInstnId>
            </InstgAgt>
        </GrpHdr>
        <CdtTrfTxInf>
            <PmtId>
                <EndToEndId>1114405599,1114382976</EndToEndId>
                <TxId>F3232323232</TxId>
            </PmtId>
            <PmtTpInf>
                <SvcLvl>
                    <Cd>SEPA</Cd>
                </SvcLvl>
            </PmtTpInf>
            <IntrBkSttlmAmt Ccy="EUR">469.12</IntrBkSttlmAmt>
            <ChrgBr>SLEV</ChrgBr>
            <Dbtr>
                <Nm>ddffrrddsaasas</Nm>
                <PstlAdr>
                    <Ctry>HU</Ctry>
                    <AdrLine>dssdsdsdsdsdaas</AdrLine>
                </PstlAdr>
            </Dbtr>
            <DbtrAcct>
                <Id>
                    <IBAN>HU26XXXXXXXXXXXXXX</IBAN>
                </Id>
            </DbtrAcct>
            <DbtrAgt>
                <FinInstnId>
                    <BIC>CCCCHUH0</BIC>
                </FinInstnId>
            </DbtrAgt>
            <CdtrAgt>
                <FinInstnId>
                    <BIC>CVCVCVCVCVC</BIC>
                </FinInstnId>
            </CdtrAgt>
            <Cdtr>
                <Nm>XXXXX</Nm>
                <PstlAdr>
                    <Ctry>DE</Ctry>
                </PstlAdr>
            </Cdtr>
            <CdtrAcct>
                <Id>
                    <IBAN>DE12vvvvvvvhghhg</IBAN>
                </Id>
            </CdtrAcct>
            <RmtInf>
                <Ustrd>0000000000,0000000000 </Ustrd>
            </RmtInf>
        </CdtTrfTxInf>
    </S2SCTIcf:FIToFICstmrCdtTrf>
</S2SCTIcf:SCTIcfBlkCredTrf>

The xslt:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns0="urn:S2SCTIcf:xsd:$SCTIcfBlkCredTrf"
    xmlns:sw8="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02"
    xmlns:S2SCTScf="urn:S2SCTScf:xsd:$SCTScfBlkCredTrf" exclude-result-prefixes="xs ns0 ">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
        <xsl:variable name="var1_SCTIcfBlkCredTrf" select="ns0:SCTIcfBlkCredTrf"/>
        <S2SCTScf:SCTScfBlkCredTrf
            xsi:schemaLocation="urn:S2SCTScf:xsd:$SCTScfBlkCredTrf SCTScfBlkCredTrf.xsd"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xmlns:S2SCTScf="urn:S2SCTScf:xsd:$SCTScfBlkCredTrf">
            <xsl:for-each select="$var1_SCTIcfBlkCredTrf">
                <S2SCTScf:SndgInst>
                    <xsl:value-of select="string(ns0:RcvgInst)"/>
                </S2SCTScf:SndgInst>
            </xsl:for-each>
            <xsl:for-each select="$var1_SCTIcfBlkCredTrf">
                <S2SCTScf:RcvgInst>
                    <xsl:value-of select="string(ns0:SndgInst)"/>
                </S2SCTScf:RcvgInst>
            </xsl:for-each>
            <xsl:for-each select="$var1_SCTIcfBlkCredTrf">
                <S2SCTScf:SrvcId>
                    <xsl:value-of select="string(ns0:SrvcId)"/>
                </S2SCTScf:SrvcId>
            </xsl:for-each>
            <xsl:for-each select="$var1_SCTIcfBlkCredTrf">
                <S2SCTScf:TstCode>
                    <xsl:value-of select="string(ns0:TstCode)"/>
                </S2SCTScf:TstCode>
            </xsl:for-each>
            <S2SCTScf:FType>SCF</S2SCTScf:FType>
            <xsl:for-each select="$var1_SCTIcfBlkCredTrf">
                <S2SCTScf:FileRef>
                    <xsl:value-of select="string(ns0:FileRef)"/>
                </S2SCTScf:FileRef>
            </xsl:for-each>
            <S2SCTScf:RoutingInd>DIR</S2SCTScf:RoutingInd>
            <xsl:for-each select="$var1_SCTIcfBlkCredTrf">
                <S2SCTScf:FileBusDt>
                    <xsl:value-of select="string(ns0:FDtTm)"/>
                </S2SCTScf:FileBusDt>
            </xsl:for-each>
            <S2SCTScf:FIToFICstmrCdtTrf xmlns="urn:iso:std:iso:20022:tech:xsd:pacs.008.001.02">
                <xsl:for-each select="$var1_SCTIcfBlkCredTrf/ns0:FIToFICstmrCdtTrf">
                    <GrpHdr>
                        <xsl:for-each select="$var1_SCTIcfBlkCredTrf/ns0:FIToFICstmrCdtTrf">
                            <MsgId>
                                <xsl:value-of select="string(sw8:GrpHdr/sw8:MsgId)"/>
                            </MsgId>
                        </xsl:for-each>
                        <xsl:for-each select="$var1_SCTIcfBlkCredTrf/ns0:FIToFICstmrCdtTrf">
                            <CreDtTm>
                                <xsl:value-of select="string(sw8:GrpHdr/sw8:CreDtTm)"/>
                            </CreDtTm>
                        </xsl:for-each>
                        <xsl:for-each select="$var1_SCTIcfBlkCredTrf/ns0:FIToFICstmrCdtTrf">
                            <NbOfTxs>
                                <xsl:value-of select="string(sw8:GrpHdr/sw8:NbOfTxs)"/>
                            </NbOfTxs>
                        </xsl:for-each>
                        <xsl:for-each select="$var1_SCTIcfBlkCredTrf/ns0:FIToFICstmrCdtTrf">
                            <xsl:variable name="var2_TtlIntrBkSttlmAmt"
                                select="sw8:GrpHdr/sw8:TtlIntrBkSttlmAmt"/>
                            <TtlIntrBkSttlmAmt>
                                <xsl:attribute name="Ccy" namespace="">
                                    <xsl:value-of select="string($var2_TtlIntrBkSttlmAmt/@Ccy)"/>
                                </xsl:attribute>
                                <xsl:value-of
                                    select="string(number(string($var2_TtlIntrBkSttlmAmt)))"/>
                            </TtlIntrBkSttlmAmt>
                        </xsl:for-each>
                        <xsl:for-each select="$var1_SCTIcfBlkCredTrf/ns0:FIToFICstmrCdtTrf">
                            <IntrBkSttlmDt>
                                <xsl:value-of select="string(sw8:GrpHdr/sw8:IntrBkSttlmDt)"/>
                            </IntrBkSttlmDt>
                        </xsl:for-each>
                        <SttlmInf>
                            <xsl:for-each select="$var1_SCTIcfBlkCredTrf/ns0:FIToFICstmrCdtTrf">
                                <SttlmMtd>
                                    <xsl:value-of
                                        select="string(sw8:GrpHdr/sw8:SttlmInf/sw8:SttlmMtd)"/>
                                </SttlmMtd>
                            </xsl:for-each>
                            <ClrSys>
                                <xsl:for-each select="$var1_SCTIcfBlkCredTrf/ns0:FIToFICstmrCdtTrf">
                                    <Prtry>
                                        <xsl:value-of
                                            select="string(sw8:GrpHdr/sw8:SttlmInf/sw8:ClrSys/sw8:Prtry)"
                                        />
                                    </Prtry>
                                </xsl:for-each>
                            </ClrSys>
                        </SttlmInf>
                        <InstdAgt>
                            <FinInstnId>
                                <xsl:for-each
                                    select="$var1_SCTIcfBlkCredTrf/ns0:FIToFICstmrCdtTrf/sw8:GrpHdr/sw8:InstgAgt">
                                    <BIC>
                                        <xsl:value-of select="string(sw8:FinInstnId/sw8:BIC)"/>
                                    </BIC>
                                </xsl:for-each>
                            </FinInstnId>
                        </InstdAgt>
                    </GrpHdr>
                    <xsl:copy-of select="$var1_SCTIcfBlkCredTrf/ns0:FIToFICstmrCdtTrf/sw8:CdtTrfTxInf" />
                </xsl:for-each>
            </S2SCTScf:FIToFICstmrCdtTrf>
        </S2SCTScf:SCTScfBlkCredTrf>
    </xsl:template>
</xsl:stylesheet>

The wrong part of output:

...
     </InstdAgt>
  </GrpHdr>
  <CdtTrfTxInf xmlns:S2SCTIcf="urn:S2SCTIcf:xsd:$SCTIcfBlkCredTrf">
     <PmtId>
        <EndToEndId>1114405599,1114382976</EndToEndId>
        <TxId>F3232323232</TxId>
 ...

I don't want to get the xmlns:S2SCTIcf="urn:S2SCTIcf:xsd:$SCTIcfBlkCredTrf" attribute this line.

Have someone any idea?

Thank you!

Feri

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your issue is that

<xsl:copy-of select="$var1_SCTIcfBlkCredTrf/ns0:FIToFICstmrCdtTrf/sw8:CdtTrfTxInf" />

copies the node from the original tree including its "namespace nodes", i.e. the namespace declarations that were in scope at that point in the original document. When this node is serialized any of these namespace nodes that are not already in force at this point in the output document will be declared by the serializer.

If you were able to use XSLT 2.0 then you could try setting copy-namespaces="no" on the copy-of but that isn't an option in XSLT 1.0. So instead of using copy-of you need to use templates to copy that node (and all its descendants recursively) without including the namespace nodes. The simplest way I can think of to do this is to declare two additional templates

<xsl:template match="*" mode="copy">
  <xsl:element name="{name()}" namespace="{namespace-uri()}">
    <xsl:apply-templates select="@*|node()" mode="copy" />
  </xsl:element>
</xsl:template>

<xsl:template match="@*|text()|comment()" mode="copy">
  <xsl:copy/>
</xsl:template>

and then replace that copy-of with

<xsl:apply-templates mode="copy"
   select="$var1_SCTIcfBlkCredTrf/ns0:FIToFICstmrCdtTrf/sw8:CdtTrfTxInf" />

The trick here is that xsl:element is creating a new element node that happens to have the same name and namespace as the original one, rather than copying the original node, so it doesn't copy the namespace nodes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...