Generic XSL to Excel
Hi, I have spent way too much tme on this and clearly don't know what I am doing. I am an XSLT newbie.
I have a .NET application generating a data adaptor of various recordsets that I need to load to Excel. One dataset per Excel tab. What I need to be able to do is to transform the first row of the dataset node names (i.e. my Excel column headers) then tranform all the rows for the data values. I never know the number of "columns" so the XSL needs to be able to dynamically adjust. Actually, I usually know the first 3 or four columns which need individual styling which is why I pull them separately with position()
Here is the sort of code I was playing with and feel free to call me an idiot - this is a bit alien to me.
<xsl:template match="/NewDataSet/usp_InfoMetrics_ReportTab1">
<Worksheet name="Staging31">
<Table x:FullColumns="1" x:FullRows="1">
<Row>
<xsl:for-each select="/NewDataSet/usp_InfoMetrics_ReportTab1[1]/*">
<xsl:choose>
<xsl:when test="position() = 1">
<Cell>
<Data ss:Type="String">
<xsl:value-of select="local-name()"/>
</Data>
</Cell>
</xsl:when>
<xsl:when test="position() = 2">
<Cell>
<Data ss:Type="String">
<xsl:value-of select="local-name()"/>
</Data>
</Cell>
</xsl:when>
<xsl:when test="position() = 3">
<Cell>
<Data ss:Type="String">
<xsl:value-of select="local-name()"/>
</Data>
</Cell>
</xsl:when>
<xsl:otherwise>
<Cell>
<Data ss:Type="String">
<xsl:value-of select="local-name()"/>
</Data>
</Cell>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<xsl:apply-templates select="/NewDataSet/usp_InfoMetrics_ReportTab1[1]" />
</Row>
<Row>
<xsl:for-each select="following-sibling::node()">
<xsl:choose>
<xsl:when test="position() = 1">
<Cell>
<Data ss:Type="String">
<xsl:value-of select="."/>
</Data>
</Cell>
</xsl:when>
<xsl:when test="position() = 2">
<Cell>
<Data ss:Type="String">
<xsl:value-of select="."/>
</Data>
</Cell>
</xsl:when>
<xsl:when test="position() = 3">
<Cell>
<Data ss:Type="String">
<xsl:value-of select="."/>
</Data>
</Cell>
</xsl:when>
<xsl:otherwise>
<Cell>
<Data ss:Type="String">
<xsl:value-of select="."/>
</Data>
</Cell>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
<xsl:apply-templates select="/NewDataSet/usp_InfoMetrics_ReportTab1[position() > 1]" />
</Row>
</Table>
</Worksheet>
</xsl:template>
The data will look somehting like this:
<NewDataSet>
<usp_InfoMetrics_ReportTab1>
<Column1>No Restriction</Column1>
<Column2>Test1</Column2>
<Column3>Test2</Column3>
<Column4>Test3</Column4>
</usp_InfoMetrics_ReportTab1>
<usp_InfoMetrics_ReportTab1>
<Column1>A lot of Restriction</Column1>
<Column2>Test6</Column2>
<Column3>Test7</Column3>
<Column4>Test8</Column4>
</usp_InfoMetrics_ReportTab1>
<usp_InfoMetrics_ReportTab1>
<Column1>More restrictions</Column1>
<Column2>Test67</Column2>
<Column3>Test77</Column3>
<Column4>Test87</Column4>
</usp_InfoMetrics_ReportTab1>
<usp_InfoMetrics_ReportTab2>
<Bill1>Bills recordset</Bill1>
<Bill2>Test1</Bill2>
<Bill3>Test2</Bill3>
<Bill4>Test3</Bill4>
</usp_InfoMetrics_ReportTab2>
<usp_InfoMetrics_ReportTab2>
<Bill1>Bills recordset second row</Bill1>
<Bill2>Test6</Bill2>
<Bill3>Test7</Bill3>
<Bill4>Test8</Bill4>
</usp_InfoMetrics_ReportTab2>
</NewDataSet>
Any pointers would be really useful, thanks.
|