Changing the node of an XML message dynamically using XSLT

Hi Folks,

I am trying to see if we can change the node name of an XML using XSLT . For example,

If we have the following, can we check the node and dynamically change it to adifferent name for example <Advantage> instead of <Vintage>

So the actual structure which is coming as input

<Message>
   <Vintage>
        <FILEA>ABC</FILEA>
   </Vintage>
</Message>


Should be change to the following if the node is <Vintage> if not leave it as it is.

<Message>
   <Advantage>
        <FILEA>ABC</FILEA>
   </Advantage>
</Message>


Regards
Kalyan.
kalyangkmAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Geert BormansInformation ArchitectCommented:
You will need an identity transform

using xsl:element you can change the name into anything you want

Are there conditions? Or do you want any "vintage" to become "advantage"?
0
zc2Commented:
Sure, you could do that using the XSLT:

<?xml version="1.0" encoding="windows-1252"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="xml" indent="yes" encoding="windows-1252"/>
	<xsl:template match="/">
		<xsl:apply-templates select="*"/>
	</xsl:template>

	<xsl:template match="*">
		<xsl:copy>
			<xsl:apply-templates select="@* | * | text() | comment()"/>
		</xsl:copy>
    </xsl:template>
	
	<xsl:template match="text() | @* | comment()">
		<xsl:copy/>
    </xsl:template>

	<xsl:template match="Vintage">
		<Advantage>
			<xsl:apply-templates select="@* | * | text() | comment()"/>
		</Advantage>
    </xsl:template>

</xsl:stylesheet>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Geert BormansInformation ArchitectCommented:
<?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:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="Vintage">
        <xsl:element name="Advantage">
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates select="node()"/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Geert BormansInformation ArchitectCommented:
Note that the first template copies the start tag, end tag and attributes for all elements
and then processes the content

The second template does the same but renames the tag

It is an interesting mechanism because it allows you to almost copied the input tree exactly as it is to the output,
but have some minor changes in between
0
kalyangkmAuthor Commented:
Thank You experts.

Regards
Kalyan.
0
Geert BormansInformation ArchitectCommented:
welcome
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.