Skip to Content
0

Extensible Stylesheet Language Transformations

Jan 15 at 05:22 AM

41

avatar image
Former Member

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 .

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Best Answer
Peter Wallner Jan 15 at 12:02 PM
0

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

Show 1 Share
10 |10000 characters needed characters left characters exceeded
Former Member

Perfect Peter . You are the best !!

0
Peter Wallner Jan 15 at 08:09 AM
0

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

Show 2 Share
10 |10000 characters needed characters left characters exceeded
Former Member

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


0

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

0