I need to convert a table of data that comes in xml outputs like the following. C1 column 1 c2 column2 etc
<?xml version="1.0" encoding="UTF-8"?>
<report>
<report_header>
<c1>desc</c1>
<c2>prname</c2>
<c3>prnum</c3>
<c4>cdate</c4>
<c5>phase</c5>
<c6>stype</c6>
<c7>status</c7>
<c8>parent</c8>
<c9>location</c9>
</report_header>
<report_row>
<c1></c1>
<c2>IT Project Message Validation</c2>
<c3>IT-0000021</c3>
<c4>12/14/2010 09:56 AM</c4>
<c5>Preparation</c5>
<c6>IT Projects</c6>
<c7>Active</c7>
<c8>IT</c8>
<c9>/IT/BIOMED</c9>
</report_row>
<report_row>
<c1></c1>
<c2>David, Michael John Morning QA Test</c2>
<c3>IT-0000020</c3>
<c4>12/14/2010 08:12 AM</c4>
<c5>Preparation</c5>
<c6>IT Projects</c6>
<c7>Active</c7>
<c8>IT</c8>
<c9>/IT/BIOMED</c9>
</report_row>
</report>
into
<?xml version="1.0" encoding="UTF-8"?>
<report>
<report_row>
<desc></desc>
<prname>IT Project Message Validation</prname>
<prnum>IT-0000021</prnum>
<cdate>12/14/2010 09:56 AM</cdate>
<phase>Preparation</phase>
<stype>IT Projects</stype>
<status>Active</status>
<parent>IT</parent>
<location>/IT/BIOMED</location>
</report_row>
<report_row>
<desc></desc>
<prname>David, Michael John Morning QA Test</prname>
<prnum>IT-0000020</prnum>
<cdate>12/14/2010 08:12 AM</cdate>
<phase>Preparation</phase>
<stype>IT Projects</stype>
<status>Active</status>
<parent>IT</parent>
<location>/IT/BIOMED</location>
</report_row>
</report>
my current xslt looks like this
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exslt="http://exslt.org/common">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<report>
<xsl:apply-templates select="/report/report_row"/>
</report>
</xsl:template>
<xsl:template match="/report/report_row">
<report_row>
<xsl:apply-templates select="c1"/>
<xsl:apply-templates select="c2"/>
<xsl:apply-templates select="c3"/>
<xsl:apply-templates select="c4"/>
<xsl:apply-templates select="c5"/>
<xsl:apply-templates select="c6"/>
<xsl:apply-templates select="c7"/>
<xsl:apply-templates select="c8"/>
<xsl:apply-templates select="c9"/>
</report_row>
</xsl:template>
<xsl:template match="c1">
<xsl:element name="{/report/report_header/c1}"><xsl:value-of select="current()"/></xsl:element>
</xsl:template>
<xsl:template match="c2">
<xsl:element name="{/report/report_header/c2}"><xsl:value-of select="current()"/></xsl:element>
</xsl:template>
<xsl:template match="c3">
<xsl:element name="{/report/report_header/c3}"><xsl:value-of select="current()"/></xsl:element>
</xsl:template>
<xsl:template match="c4">
<xsl:element name="{/report/report_header/c4}"><xsl:value-of select="current()"/></xsl:element>
</xsl:template>
<xsl:template match="c5">
<xsl:element name="{/report/report_header/c5}"><xsl:value-of select="current()"/></xsl:element>
</xsl:template>
<xsl:template match="c6">
<xsl:element name="{/report/report_header/c6}"><xsl:value-of select="current()"/></xsl:element>
</xsl:template>
<xsl:template match="c7">
<xsl:element name="{/report/report_header/c7}"><xsl:value-of select="current()"/></xsl:element>
</xsl:template>
<xsl:template match="c8">
<xsl:element name="{/report/report_header/c8}"><xsl:value-of select="current()"/></xsl:element>
</xsl:template>
<xsl:template match="c9">
<xsl:element name="{/report/report_header/c9}"><xsl:value-of select="current()"/></xsl:element>
</xsl:template>
</xsl:transform>
My transform works if I assume a maximal number of columns and the column headers can be legal qnames.
It started failing when I got more than the 100 limit I had assumed and column headers with spaces in them.
How do I create a transform that uses wildcards instead and how do I strip spaces and illegal characters from the column headers to make them legal qnames?
Thanks
Consider the following stylesheet:
Applied to an XML with spaces in headers:
It produces this result:
I know this has already been answered, but I figure I'd include a StAX version in ColdFusion, seeing as the question was originally tagged as such. Will serve posterity if bitten by OoM errors using XSLT:
This transformation:
when applied to the provided XML document:
produces the wanted, correct result:
Do note:
The transformation successfully converts any text with any number of different non-alphanumeric characters to a syntactically-correct XML name.
Efficiency is achieved using keys.
You can use the translate function to strip undesired characters from the name.
You can use regular wildcard templates to match any child element; use a mode to prevent this to get in the way of the regular wildcard template. And you can use the local-name function to lookup an element by element name.