Connecting Tech Pros Worldwide Forums | Help | Site Map

Transform XML data to Coldfusion structure

Newbie
 
Join Date: Mar 2007
Posts: 25
#1: Jan 6 '09
I want to be able to transform the contents of an XML file into a coldfusion structure, with structure elements named by the XML node names.

XML structure:

<text>
<node1>Node1 data</node1>
<node2>Node2 data</node2>
</text>

Coldfusion structure required:

struct.node1="Node1 data"
struct.node2="Node2 data"

Currently, I can only do this by defining individual .xsl tranform files for each of the nodes. There must be a more efficient way of doing this. Perhaps with only one .xsl file that can read the node names and assign them to individual structure element names, before reading the values of each node.

Can anyone point me in the direction of an example or tutorial that can assist me in doing this.

Regards Blackmore

Moderator
 
Join Date: Mar 2006
Posts: 1,103
#2: Jan 7 '09

re: Transform XML data to Coldfusion structure


?
Expand|Select|Wrap|Line Numbers
  1. <xsl:template match="text">
  2.   <xsl:for-each select="*">
  3.     <xsl:text>struct.</xsl:text>
  4.     <xsl:value-of select="local-name()"/>
  5.     <xsl:text>="</xsl:text>
  6.     <xsl:value-of select="."/>
  7.     <xsl:text>="</xsl:text>
  8.   </xsl:for-each>
  9. </xsl:template>
  10.  
Or if you want to use one select, and don't mind escaping entities, " -> &quot;
<xsl:value-of select="concat('struct.', local-name(), '=&quot;', . ,'&quot;')"/>
Reply