0

i want to remove "nso" prefix from the output xml file with out removing the namespace declaration but in the declaration part also i need to remove "ns0:" & ":ns0".

`

<?xml version="1.0" encoding="UTF-8"?>
<ns0:Document xmlns:ns0="urn:iso:std:iso:20022:tech:xsd:abcd">**<!-- I do not want to remove this declaration line from the output only needs to remove "ns0:" & ":ns0" from the declaration-->**
    <ns0:CstmrCdtTrfInitn>
        <ns0:GrpHdr>
            <ns0:MsgId>abcd</ns0:MsgId>
            <ns0:CreDtTm>2023-01-24T14:47:17Z</ns0:CreDtTm>
            <ns0:NbOfTxs>2 </ns0:NbOfTxs>
            <ns0:CtrlSum>580000.00</ns0:CtrlSum>
            <ns0:InitgPty>
                <ns0:Nm>abcd</ns0:Nm>
                <ns0:CtryOfRes>IN</ns0:CtryOfRes>
            </ns0:InitgPty>
        </ns0:GrpHdr>
</ns0:CstmrCdtTrfInitn>
</ns0:Document>`

`

<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns ="urn:iso:std:iso:20022:tech:xsd:abcd">
    <CstmrCdtTrfInitn>
        <GrpHdr>
            <MsgId>abcd</MsgId>
            <CreDtTm>2023-01-24T14:47:17Z</CreDtTm>
            <NbOfTxs>2 </NbOfTxs>
            <CtrlSum>580000.00</CtrlSum>
            <InitgPty>
                <Nm>abcd</Nm>
                <:CtryOfRes>IN</CtryOfRes>
            </InitgPty>
        </GrpHdr>
</CstmrCdtTrfInitn>
</Document>`

Please help me on this requirment. Thanks.

i used below code but it is removing all the nso prefix's along with namespace declarations but i want to remove only nso prefix's for individual xml tags from the element "ns0:CstmrCdtTrfInitn" before this element i want to remove "ns0:" & ":ns0" and keep the declaration with out "ns0".

`

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="no" />
<xsl:template match="/|comment()|processing-instruction()">
<xsl:copy>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@*|node()" />
</xsl:element>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="." />
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
  • This is a significantly different requirement from the original one. Next time, please post a new question once you have received answers. – michael.hor257k Jan 25 '23 at 08:02

2 Answers2

0

Change

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

to

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

and add a template

<xsl:template match="/*">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • Hi Martin, Above codes also worked but i need to remove "ns0:" & ":ns0" from the namaspace declaration and at end of the xml message ns0: from the prefix for tag. Thanks. – munireddy pulagam Jan 25 '23 at 07:37
0

--- edited in view of changed requirement ---

To remove the ns0 prefix from all elements in the input XML, while keeping them in the original namespace, do:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="*">
    <xsl:element name="{local-name()}" namespace="{namespace-uri()}">
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>
    
</xsl:stylesheet>
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51