Changing The Root Node In A ColdFusion XML Document Using XSLT

Posted April 24, 2009 at 10:17 AM by Ben Nadel

Tags: ColdFusion

The other day, I answered a reader's question on changing the name of an XML root node. I demonstrated how to do this using string parsing and how to do it using XML manipulation. In the comments, Matthew suggested that I could use XSLT to do this as well. His example hard-coded the attribute names, so I wanted to demonstrate how to use XSLT to change the root name and dynamically copy all attributes of the root node as well. Plus, to be honest, my XSLT is getting pretty rusty, so this was a great opportunity to dust of the old brain.

  • <!---
  • Create an XML string that has a root node that gets
  • repeated within the body of the XML as well.
  • --->
  • <cfsavecontent variable="strXmlData">
  •  
  • <list id="my-to-do-list">
  • <item>
  • List Item A
  • </item>
  • <item>
  • List Item B
  • </item>
  • <item>
  • <list>
  • <item>
  • Sub Item A
  • </item>
  • <item>
  • Sub Item B
  • </item>
  • </list>
  • </item>
  • <item>
  • List Item D
  • </item>
  • </list>
  •  
  • </cfsavecontent>
  •  
  •  
  • <!--- Define XSLT to repace the root node. --->
  • <cfsavecontent variable="strXSLData">
  •  
  • <?xml version="1.0" encoding="ISO-8859-1"?>
  • <xsl:transform
  • version="1.0"
  • xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  •  
  •  
  • <!--- Define new root node name BELOW. --->
  •  
  • <xsl:variable name="new-root-node">
  • <xsl:text>masterlist</xsl:text>
  • </xsl:variable>
  •  
  • <!--- Define new root node name ABOVE. --->
  •  
  •  
  • <!--- --------------------------------------------- --->
  •  
  •  
  • <!--- Match the root node. --->
  • <xsl:template match="/*">
  •  
  • <!--- Create a new element with new node name. --->
  • <xsl:element name="{$new-root-node}">
  •  
  • <!---
  • Copy all attributes of the existing root
  • node into the new root node.
  • --->
  • <xsl:for-each select="@*">
  •  
  • <xsl:attribute name="{name()}">
  • <xsl:value-of select="." />
  • </xsl:attribute>
  •  
  • </xsl:for-each>
  •  
  • <!--- Copy all children of the root node. --->
  • <xsl:copy-of select="*" />
  •  
  • </xsl:element>
  •  
  • </xsl:template>
  •  
  • </xsl:transform>
  •  
  • </cfsavecontent>
  •  
  •  
  • <!--- Transform the XML. --->
  • <cfset xmlNew = XmlTransform(
  • Trim( strXmlData ),
  • Trim( strXSLData )
  • ) />
  •  
  •  
  • <!--- Output new XML document with new root node. --->
  • <cfdump
  • var="#XmlParse( xmlNew )#"
  • label="New XML Root"
  • />

In the above example, I set a variable at the top of the XSLT definition. This variable holds the name of the new root node. Then, I match the root node in the only template and create an element with the new name (using dynamic curly-brace evaluation to set the name value). I then loop over each attribute in the root node and create a copy of it in my new element. Then, I simply use the copy-of element to get a deep copy of all the children.

When we run the above code, we get the following CFDump output:

 
 
 
 
 
 
Changing The Root Node Of A ColdFusion XML Document Using XSLT. 
 
 
 

I guess this post was more for my benefit as I just wanted to use it as an excuse to practice my XML transformations. The XSLT tag set is so different that the ColdFusion / HTML tags that I'm used to using that if I don't practice from time to time, it all goes away. Heck, even with this post, the majority of the time was spent flipping back and forth from the XSLT documentation to get the syntax right.




Reader Comments

Apr 27, 2009 at 11:17 AM // reply »
46 Comments

Ben,

Thanks for your XSLT posts on this. I did have a little problem with this code. When i have attributes on my original root node, when i pass them through this xslt transformation, the attributes are put on the first child node of the root node.

<ben id='nadel'>
<post>xxxxx</post>
.....
</ben>

changing the root node to matthew

and the first post node now has id='nadel' not the root node.

hope that makes sense. I can send you my example code in an email if you need to see it.

-Matthew


Apr 27, 2009 at 12:48 PM // reply »
46 Comments

It only seems to be a problem when the xml file has a namespace. I went back and tried a few more things. It will copy the id attribute over, but the xmlns attribute i had on my root node was removed from the root node and put on the first child node.


Apr 29, 2009 at 8:01 AM // reply »
11,238 Comments

@Matthew,

Hmmm, to be honest, I dislike name spaces and I'm not all that good at them. So, are you going another route? Or did you get it to work with name spaces?


Apr 29, 2009 at 6:28 PM // reply »
46 Comments

For now i just used the JDOM java library built into cf to do it. Ill play around more with the xslt stuff.

Cheers,

-Matthew


Apr 29, 2009 at 6:32 PM // reply »
11,238 Comments

Matthew,

Sounds good.


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 19, 2013 at 2:31 PM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
It's funny really just how well that image describes the way I would imagine most people that go with angular for some project is. I have had a similar roller-coaster ride with it as well, but not qu ... read »
May 17, 2013 at 7:42 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
Ben - thanks so much for posting these Angular articles and findings, they've been a huge help towards learning one of the more 'complex' JavaScript frameworks out there (IMO). I have been using Angu ... read »
May 16, 2013 at 5:01 PM
UPDATE: Parsing CSV Data Files In ColdFusion With csvToArray()
Your code was the closest thing I've found to obtaining some direction for converting ISO fields to values that CF can translate properly. Thank you for posting! ... read »
May 15, 2013 at 10:37 PM
Very Simple Pusher And ColdFusion Powered Chat
hi id making plz easy ... read »
May 15, 2013 at 6:07 PM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Ben, you once again saved my bacon at work. Thank you, thank you, thank you! ... read »
May 15, 2013 at 4:15 PM
What If All User Interface (UI) Data Came In Reports?
@Josh, Thanks! @Ben, I definitely recommend the David West book "Object Thinking" I've been quoting from. It goes deeply into the philosophy and history of OO programming. His breadth ... read »
May 15, 2013 at 11:36 AM
Ask Ben: Print Part Of A Web Page With jQuery
I found this helpfull when you need to keep (refresh) the original parent page after closing the iframe child print dialog (Hoping you're not using a form at this time so it won't submit again): On ... read »
May 14, 2013 at 7:13 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, If there's any books you'd recommend on the subject of domain modelling, I'd love to hear it. I just downloaded the free PDF of "Domain Driven Design Quickly". Figured I'd give it ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools