on 01-15-2018 5:22 AM
Hi Experts ,
I have one requirement.
Source and Target structure is same and it's a simple one to one mapping but the issues are with the structure at the Receiver side.
Source structure :
Main node
First_name
Last_name
Job_Title
........
I want Receiver structure like below.
Main Node
First-name
Last-name
Job-Title
Thanks in advance .
Hi Shiva,
I see. A more generic approach would be this XSLT. It checks if the tag name (local-name()) contains an underline and if so it replaces it with a hyphen:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*">
<xsl:choose>
<xsl:when test="contains(local-name(),'_')">
<xsl:element name="{translate(local-name(),'_','-')}">
<xsl:apply-templates select="@*|node()" />
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@*|node()" />
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Best regards, Peter
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello Shiva,
Source XML:
<?xml version="1.0" encoding="UTF-8"?>
<Customer>
<First_name>John</First_name>
<Last_name>Test</Last_name>
<Job_Title>Test job</Job_Title>
</Customer>
XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="First_name">
<First-name>
<xsl:apply-templates select="@*|node()" />
</First-name>
</xsl:template>
<xsl:template match="Last_name">
<Last-name>
<xsl:apply-templates select="@*|node()" />
</Last-name>
</xsl:template>
<xsl:template match="Job_Title">
<Job-Title>
<xsl:apply-templates select="@*|node()" />
</Job-Title>
</xsl:template>
</xsl:stylesheet>
Target XML:
<?xml version="1.0" encoding="UTF-8"?>
<Customer>
<First-name>John</First-name>
<Last-name>Test</Last-name>
<Job-Title>Test job</Job-Title>
</Customer>
Is that what you need?
Best regards, Peter
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Peter,
Thank you very much for your quick response. This code works very fine.But can you please advise the XSLT code which replaces all "_" with "-" irrespective of filed elements because here I have more than 40 fields under the main node and XSLT code looks bigger in this case.
Best ,Shiva
User | Count |
---|---|
90 | |
10 | |
10 | |
10 | |
7 | |
7 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.