XSLT - ordering dataTables - Template-driven transformation

W

Walt Borders

Hi, using XSLT I'm able to output data, in CSV form, to an Excel 2002
spreadsheet. The report format follows the DataSet table order, not
the required display order.

Problem: I need a report with a different table order.

I am trying to format the output of my DataSet from a C# ASP.NET web
page, I would greatly appreciate some help.


Possible solution paths:
Plan A: Either order the "dataTables/document" in the DataSet and use
the XSLT as a data-driven transformation
or Plan B: use the XSLT to transform via Template-driven
Transformation.
or Plan C: something else.


My Environment: C#, ASP.Net, 1.1 Framework, MSXML 4.0 SDK, Excel 2002.


I am able to fill a DataSet with three dataTables:

2 by way of dataAdapters,
dataTable "Report"
dataTable "Detail"

1 by way of manual creation of
dataTable "Title". Data is added with Session variables.


The current order of the DataSet dataTables is:
1. Report, 2. Detail, 3, Title

What is needed is:
1.Title, 2. Report, 3. Detail


***************
What I have done:
Trying to change the order of "document":

1) I've tried changing the XML file with no effect.

2) In the DataSet.XSD, To force the order of the DataSet, I tried
inserting the manually created table "Title" in the proper order. I
received the following error: System.Data.DuplicateNameException: A
DataTable named 'Title' already belongs to this DataSet.

3) I tried changing the order of the dataTable creation inside the
DataSet, without success. It appears that manually created tables are
last.


Trying to change the order of XSLT transform:

1) Changing the script below, results in the entire contents of the
DataSet is dumped into cell A1.... in the familiar Report-Detail-Title
order.


BTW: Thanks to Oleg Tkachenko, Dare Obasanjo, exslt.org, and Microsoft
Development Team for providing the Articles and String Class(es) to do
RegEx!

Thanks in advance for your comments!

Walt Borders
(e-mail address removed)


My XSLT script:
This working script puts the data into three tables, but in the wrong
order. I have been unsuccessful in making this script a
Template-driven transformation.
I did not find the MSDN pages helpful on this topic.
***************

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:n1="http://www.tempuri.org/DataSet1.xsd"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:regexp="http://exslt.org/regular-expressions"
extension-element-prefixes="regexp"
xmlns:str="http://exslt.org/strings" exclude-result-prefixes="str">

<xsl:strip-space elements="*"/>

<xsl:blush:utput method="text" encoding="iso-8859-1" media-type="text/csv"
/>

<xsl:template match='/'>
<xsl:apply-templates/>
</xsl:template>

<!--
Title section
-->
<xsl:template match='n1:Title/child::node()'>
<xsl:text>
</xsl:text>
<xsl:text/><xsl:apply-templates/><xsl:text/>
</xsl:template>

<!--
Report section
-->
<xsl:template match='n1:Report'>
<xsl:if test="position()='1'">
<xsl:for-each select="child::node()">

<!-- substitute hex space in XML element/column names with an ascii
space -->
<xsl:value-of select="regexp:replace(name(), '_x0020_','',' ')" />
<xsl:if test="position()!=last()">,</xsl:if>
<xsl:if test="position()=last()">
<xsl:text>
</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:if>
<xsl:apply-templates/>
<xsl:text> </xsl:text>
</xsl:template>

<xsl:template match='n1:Report/child::node()'>
<xsl:text/><xsl:apply-templates/><xsl:text/>
<xsl:if test="not(following-sibling::n1:Report)">,</xsl:if>
<xsl:if test="position()=last()">
<xsl:text>
</xsl:text>
</xsl:if>
</xsl:template>

<!--
Detail section
-->
<xsl:template match='n1:Detail'>
<xsl:if test="position()='1'">
<xsl:for-each select="child::node()">
<xsl:value-of select="regexp:replace(name(), '_x0020_','',' ')"
/>
<xsl:if test="position()!=last()">,</xsl:if>
<xsl:if test="position()=last()">
<xsl:text>
</xsl:text>
</xsl:if>
</xsl:for-each>
</xsl:if>
<xsl:apply-templates/>
<xsl:text> </xsl:text>
</xsl:template>

<xsl:template match='n1:Detail/child::node()'>
<xsl:text/><xsl:apply-templates/><xsl:text/>
<xsl:if test="not(following-sibling::n1:Detail)">,</xsl:if>
<xsl:if test="position()=last()">
<xsl:text>
</xsl:text></xsl:if>
</xsl:template>
</xsl:stylesheet>

*************** EOF
 
O

Oleg Tkachenko [MVP]

Walt said:
Hi, using XSLT I'm able to output data, in CSV form, to an Excel 2002
spreadsheet. The report format follows the DataSet table order, not
the required display order.

Problem: I need a report with a different table order.

Basically you can sort in XSLT using xsl:sort instruction. This way you
can control display order.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top