cancel
Showing results for 
Search instead for 
Did you mean: 

Extensible Stylesheet Language Transformations

0 Kudos

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 .

Accepted Solutions (1)

Accepted Solutions (1)

peter_wallner2
Active Contributor
0 Kudos

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

0 Kudos

Perfect Peter . You are the best !!

Answers (1)

Answers (1)

peter_wallner2
Active Contributor
0 Kudos

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

0 Kudos

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


peter_wallner2
Active Contributor
0 Kudos

Hi Shiva, sure please see my second answer. Best regards, Peter