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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Gertone (Geert Bormans)Information 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"?
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

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
Gertone (Geert Bormans)Information 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>
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Gertone (Geert Bormans)Information 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
kalyangkmAuthor Commented:
Thank You experts.

Regards
Kalyan.
Gertone (Geert Bormans)Information ArchitectCommented:
welcome
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.