on 01-20-2015 10:03 AM
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
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>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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());
}
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You can try XMLAnonymizerBean in the module configuration instead of XSLT Mapping.
Refer tthe note - 880173 - XI 3.0 Adapter Framework XML Anonymizer Module.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
83 | |
24 | |
12 | |
9 | |
7 | |
6 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.