Skip to Content
0
Jan 20, 2015 at 10:03 AM

XSLT Coding - change of namespace

984 Views

Hello,

I would like to seek your help on changing the namespace of an XML into a different one.

INPUT XML:

<?xml version="1.0" encoding="UTF-8"?>

<ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">

<ns0:Message1>

<ns2:despatchAdvice xmlns:ns2="http://www.ean-ucc.org/schemas/1.3.1/eanucc" contentVersion="1.3.1" documentStructureVersion="1.3.1" lastUpdateDate="2015-01-20" creationDate="2015-01-20T10:59:44" documentStatus="ORIGINAL">

<despatchAdviceIdentification>

..

..

<ns2:despatchAdvice xmlns:ns2="http://www.ean-ucc.org/schemas/1.3.1/eanucc" contentVersion="1.3.1" documentStructureVersion="1.3.1" lastUpdateDate="2015-01-20" creationDate="2015-01-20T10:59:44" documentStatus="ORIGINAL">

<despatchAdviceIdentification>

..

..

The highlighted one

Basically, the target is to replace the whole namespace of <ns2:despatchAdvice> into the below.

<eanucc:despatchAdvice xmlns:xsd="http://www.xx.xxx/2001/XMLSchema" xmlns:eanucc="http://www.ean-ucc.org/schemas/1.3.1/eanucc" xmlns:AAAA="http://www.test.com/Schema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ean-ucc.org/schemas/1.3.1/eanuccDesadv_ld_AAAA_1.xsd" contentVersion="1.3.1" documentStructureVersion="1.3.1" lastUpdateDate="2015-01-20" creationDate="2015-01-20T09:43:52" documentStatus="ORIGINAL">

The namespace <ns2:despatchAdvice > can occur multiple times.


This is the current XSLT code I have now which is not working.


<?xml version='1.0'?>

<xsl:stylesheet

xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"

xmlns:ns1="http://www.ean-ucc.org/schemas/1.3.1/eanucc"

exclude-result-prefixes="ns1">

<xsl:output encoding="UTF-8" indent="yes" method="xml" version="1.0"/>

<xsl:template match="@*|text()|comment()|processing-instruction()">

<xsl:copy>

<xsl:apply-templates select="@*|node()"/>

</xsl:copy>

</xsl:template>

<xsl:template match="/ns1:despatchAdvice">

<eanucc:despatchAdvice

xmlns:eanucc="http://www.ean-ucc.org/schemas/1.3.1/eanucc"

xmlns:xsi="http://www.xx.xxx/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.ean-ucc.org/schemas/1.3.1/eanuccDesadv_ld_AAAA_1.xsd"

xmlns:AAAA="http://www.test.com/Schema"

xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<xsl:apply-templates select="@*|node()"/>

</eanucc:despatchAdvice>

</xsl:template>

<xsl:template match="*">

<xsl:element name="{local-name()}">

<xsl:apply-templates select="@*|node()"/>

</xsl:element>

</xsl:template>

</xsl:stylesheet>

Appreciate your inputs here! Thanks in advance!

Fred