darkmage784
09-21-2005, 07:56 PM
A while ago, I was trying to transform XML data into a rowed or columned data. The problem is fixed now, and I'm posting my solution for anyone who needs it.
The XML structure:
<table>
<type>row | column</type>
<tableData>
<container>
<header>Transformed to th</header>
<data>transformed to td</data>
<data>etc</data>
</container>
</tableData>
</table>
And here is the XSLT snippet to transform it:
<xsl:if test="//table/type = 'column'">
<table border="3">
<tr>
<xsl:for-each select="//table/tableData/container">
<th><xsl:value-of select="header" /></th>
</xsl:for-each>
</tr>
<xsl:for-each select="//tableData/container[1]/data">
<tr>
<xsl:variable name="pos" select="position()" />
<xsl:for-each select="//tableData/container/data[position()=$pos]">
<td><xsl:value-of select="." /></td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</xsl:if>
<xsl:if test="//table/type = 'row'">
<table border="3">
<xsl:for-each select="//tableData/container">
<tr>
<th><xsl:value-of select="header" /></th>
<xsl:for-each select="./data">
<td><xsl:value-of select="." /></td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</xsl:if>
The XML structure:
<table>
<type>row | column</type>
<tableData>
<container>
<header>Transformed to th</header>
<data>transformed to td</data>
<data>etc</data>
</container>
</tableData>
</table>
And here is the XSLT snippet to transform it:
<xsl:if test="//table/type = 'column'">
<table border="3">
<tr>
<xsl:for-each select="//table/tableData/container">
<th><xsl:value-of select="header" /></th>
</xsl:for-each>
</tr>
<xsl:for-each select="//tableData/container[1]/data">
<tr>
<xsl:variable name="pos" select="position()" />
<xsl:for-each select="//tableData/container/data[position()=$pos]">
<td><xsl:value-of select="." /></td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</xsl:if>
<xsl:if test="//table/type = 'row'">
<table border="3">
<xsl:for-each select="//tableData/container">
<tr>
<th><xsl:value-of select="header" /></th>
<xsl:for-each select="./data">
<td><xsl:value-of select="." /></td>
</xsl:for-each>
</tr>
</xsl:for-each>
</table>
</xsl:if>