Added by Ruslim Chang , last edited by Ruslim Chang on Sep 09, 2008
Scenario:
Some time, we have to add additional XML namespace at certain element of the XML as to comply with the receiver system requirements. Please see the difference between source and target XML below for instance.
Source Structure:
<?xmlversion="1.0" encoding="UTF-8"?>
<RequestService xmlns="urn:axon.com.general">
<ServiceName>CreateMaterial</ServiceName>
<ServiceVersion>1.0</ServiceVersion>
<MT_Material>
<Material>
<MaterialNo>123</MaterialNo>
<MaterialType>ROH</MaterialType>
<MaterialGroup>10</MaterialGroup>
<BaseUnit>Kilogram</BaseUnit>
<OrderUnit>1</OrderUnit>
<Date>22/06/2008</Date>
</Material>
</MT_Material>
</RequestService>
Target Structure:
<?xmlversion="1.0" encoding="UTF-8"?>
<RequestService xmlns="urn:axon.com.general">
<ServiceName>CreateMaterial</ServiceName>
<ServiceVersion>1.0</ServiceVersion>
<MT_Material xmlns="urn:axon.com.http2file:receiver">
<Material>
<MaterialNo>123</MaterialNo>
<MaterialType>ROH</MaterialType>
<MaterialGroup>10</MaterialGroup>
<BaseUnit>Kilogram</BaseUnit>
<OrderUnit>1</OrderUnit>
<Date>22/06/2008</Date>
</Material>
</MT_Material>
</RequestService>
As you can see from the target structure that complexelement MT_Material has different name space "urn:axon.com.http2file:receiver" which is different from root element name space "urn:axon.com.general". So the challenge is how we can add additional namespace at certain element level of the XML. One solution is by using XSL to do souce and target structure mapping.
Solution:
1. Source To Target Structure Mapping
Excerpt is the XSL required to add additional namespace tothe complex element_MT
Example XSLT:
<?xml version="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:template match="/">
<xsl:apply-templates select="*" mode="element"/>
</xsl:template>
<xsl:template match="*" mode="element">
<xsl:variable name="newname" select="local-name(.)"/>
<xsl:if test="($newname = 'RequestService') or ($newname = 'ServiceName') or ($newname = 'ServiceVersion')">
<xsl:element name="{$newname}" namespace="urn:axon.com.general">
<xsl:apply-templates mode="copyall" select="@*|comment()|processing-instruction()|text()"/>
<xsl:apply-templates select="*" mode="element"/>
</xsl:element>
</xsl:if>
<xsl:if test="($newname != 'RequestService') and ($newname != 'ServiceName') and ($newname != 'ServiceVersion')">
<xsl:element name="{$newname}" namespace="urn:axon.com.http2file:receiver">
<xsl:apply-templates mode="copyall" select="@*|comment()|processing-instruction()|text()"/>
<xsl:apply-templates select="*" mode="element"/>
</xsl:element>
</xsl:if>
</xsl:template>
<xsl:template mode="copyall" match="@*|comment()|processing-instruction()|text()">
<xsl:copy>
<xsl:apply-templates mode="copyall" select="@*|comment()|processing-instruction()|text()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
If you have XML editor then, you can perform testing to make sure that the transformation is done correctly. Please proceed with the next step to use the XSL in XI design and configuration.
2. XI Design and Configuration
Life is beautiful! Let's make it meaningful and colorful!