cancel
Showing results for 
Search instead for 
Did you mean: 

XSLT Coding - change of namespace

0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Frederick,

I did not get why you were using ns1 in your snippet when your example data is using ns2?

A full example might be helpful, otherwise you might try like following:


<xsl:stylesheet version='2.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:ns2="http://www.ean-ucc.org/schemas/1.3.1/eanucc">

    <xsl:output encoding='UTF-8' indent='yes' method='xml'/>

   

    <!-- copy everything into the output -->

    <xsl:template match='@*|node()'>

        <xsl:copy>

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

        </xsl:copy>

    </xsl:template>

   

    <!-- change the namespace declaration for despatchAdvice -->

    <xsl:template match="ns2:despatchAdvice">

        <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">

           

            <xsl:copy-of select="node()|@*"/>

        </eanucc:despatchAdvice>

    </xsl:template>

</xsl:stylesheet>

0 Kudos

Thanks everyone for your replies.

I tried the one with Christian and it worked.

Answers (2)

Answers (2)

RaghuVamseedhar
Active Contributor
0 Kudos

Frederick,

I think, handling this requirement in XSLT is difficult. In Graphical mapping, it's not possible. Please use below Java Mapping.


package com.javaMapping; 

import java.io.*; 

import com.sap.aii.mapping.api.*; 

public class WellformedXML_JavaMapping extends AbstractTransformation { 

    @Override 

    public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput) throws StreamTransformationException { 

        try { 

            InputStream inputstream = transformationInput.getInputPayload().getInputStream(); 

            OutputStream outputstream = transformationOutput.getOutputPayload().getOutputStream(); 

            // a) Copy Input content to String 

            byte[] b = new byte[inputstream.available()]; 

            inputstream.read(b); 

            String inputContent = new String(b); 

            // b) Replace logic here. Edit StartTag and EndTag, below. FYI. Assuming namespace-alias ns2 will be 3 characters (3 dots).

            inputContent = inputContent.replaceAll("<...:despatchAdvice>", "StartTag").replaceAll("</...:despatchAdvice>", "EndTag"); 

           

            outputstream.write(inputContent.getBytes()); 

        } catch (Exception exception) { 

            getTrace().addDebugMessage(exception.getMessage()); 

            throw new StreamTransformationException(exception.toString()); 

        } 

    } 

Former Member
0 Kudos

You can try XMLAnonymizerBean in the module configuration instead of XSLT Mapping.

Refer tthe note - 880173 - XI 3.0 Adapter Framework XML Anonymizer Module.