Skip to Content
avatar image
Former Member

Extensible Stylesheet Language Transformations

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 .

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    Jan 15 at 12:02 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

  • Jan 15 at 08:09 AM

    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

    Add comment
    10|10000 characters needed characters exceeded