XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
Anatoli TitarevSubject: XSL Transformation help needed (original and new provided)
Author: Anatoli Titarev
Date: 22 Apr 2005 01:45 AM
Originally Posted: 22 Apr 2005 01:34 AM
Hi all, I am to this forum and new to XSL. I am working on a generic reporting solution. XSL is the stumbling block. I need to create an XSL stylesheet that converts ANY(!) XML created by .Net DataSet object into an XML file, which can be diplayed in a web page using another stylesheet. The DataSet can have results from multiple (!) queries. The very first one unnamed SQL creates <table> node with the column names as children where text has the data. Every new SQL creates Table1, Table2, etc. I need to convert all queries into headers with column names and rows with data. Original: <?xml version="1.0" standalone="yes"?> <OriginalDataSet> <Table> <Column1>25.7500</Column1> </Table> <Table1> <Test_x0020_Decimal>45.15</Test_x0020_Decimal> </Table1> <Table2> <Today>2005-04-13T23:02:38.6270000+10:00</Today> </Table2> <Table3> <Num_x0020_of_x0020_Authors>23</Num_x0020_of_x0020_Authors> </Table3> <Table4> <Num_x0020_of_x0020_Employees>43</Num_x0020_of_x0020_Employees> </Table4> <Table5> <Column1>Testing Output</Column1> </Table5> <Table6> <au_id>172-32-1176</au_id> <au_lname>White</au_lname> <au_fname>Johnson</au_fname> <phone>408 496-7223</phone> <address>10932 Bigge Rd.</address> <city>Menlo Park</city> <state>CA</state> <zip>94025</zip> <contract>true</contract> </Table6> <Table6> <au_id>213-46-8915</au_id> <au_lname>Green</au_lname> <au_fname>Marjorie</au_fname> <phone>415 986-7020</phone> <address>309 63rd St. #411</address> <city>Oakland</city> <state>CA</state> <zip>94618</zip> <contract>true</contract> </Table6> </OriginalDataSet> New, comments are not necessary, they are for readability only <?xml version="1.0" standalone="yes"?> <?xml-stylesheet type="text/xsl" href="datasetstylesheet.xsl" ?> <DATASET> <!-- <Table> --> <header> <field>Column1</field> </header> <row> <field>25.7500</field> </row> <!-- <Table1> --> <header> <field>Test_x0020_Decimal</field> </header> <row> <field>45.15</field> </row> <!-- <Table2> --> <header> <field>Today</field> </header> <row> <field>2005-04-13T23:02:38.6270000+10:00</field> </row> <!-- <Table3> --> <header> <field>Num_x0020_of_x0020_Authors</field> </header> <row> <field>23</field> </row> <!-- <Table4> --> <header> <field>Num_x0020_of_x0020_Employees</field> </header> <row> <field>43</field> </row> <!-- <Table5> --> <header> <field>Column1</field> </header> <row> <field>Testing Output</field> </row> <!-- <Table6>, NOTE: only one header but 2 rows! --> <header> <field>au_id</field> <field>au_lname</field> <field>au_fname</field> <field>phone</field> <field>address</field> <field>city</field> <field>state</field> <field>zip</field> <field>contract</field> </header> <row> <field>172-32-1176</field> <field>White</field> <field>Johnson</field> <field>408 496-7223</field> <field>10932 Bigge Rd.</field> <field>Menlo Park</field> <field>CA</field> <field>94025</field> <field>true</field> </row> <row> <field>213-46-8915</field> <field>Green</field> <field>Marjorie</field> <field>415 986-7020</field> <field>309 63rd St. #411</field> <field>Oakland</field> <field>CA</field> <field>94618</field> <field>false</field> </row> </DATASET> Your help is appreciated! Anatoli If you need more info, please post here.


UnknownOriginal.xml
Original file

UnknownNew(1).xml
New file

Postnext
Anatoli TitarevSubject: XSL Transformation help needed (original and new provided)
Author: Anatoli Titarev
Date: 22 Apr 2005 01:43 AM
That's me again. The forum page reformats my post so it becomes unreadable, what a nuisance!

Postnext
(Deleted User) Subject: XSL Transformation help needed (original and new provided)
Author: (Deleted User)
Date: 22 Apr 2005 04:09 AM
Hi Anatoli,
here is my solution; when the stylesheet matches the TableXX element, it checks if the preceding node had the same name: if it doesn't, it outputs the header. In any case it outputs a <row> element by iterating over the child elements.

Hope it helps,
Alberto


<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<DATASET>
<xsl:apply-templates/>
</DATASET>
</xsl:template>

<xsl:template match="node()[substring(name(),1,5)='Table']">
<xsl:variable name="myname" select="name()"/>
<xsl:if test="preceding-sibling::*[1][name()!=$myname]">
<xsl:comment><xsl:value-of select="name()"/></xsl:comment>
<header>
<xsl:for-each select="*">
<field><xsl:value-of select="name()"/></field>
</xsl:for-each>
</header>
</xsl:if>
<row>
<xsl:for-each select="*">
<field><xsl:value-of select="."/></field>
</xsl:for-each>
</row>
</xsl:template>

</xsl:stylesheet>

Postnext
Anatoli TitarevSubject: XSL Transformation help needed (original and new provided)
Author: Anatoli Titarev
Date: 22 Apr 2005 08:52 PM
Thank you very much, Alberto but I would expect the following result of the iteration:

<?xml version="1.0" encoding="utf-8"?>
<DataSet>
<header>
<field>Column1</field>
</header>
<row>
<field>25.7500</field>
</row>

<header>
<field>Test_x0020_Decimal</field>
</header>
<row>
<field>45.15</field>
</row>

<header>
<field>Today</field>
</header>
<row>
<field>2005-04-13T23:02:38.6270000+10:00</field>
</row>

<header>
<field>Num_x0020_of_x0020_Authors</field>
</header>
<row>
<field>23</field>
</row>

<header>
<field>Num_x0020_of_x0020_Employees</field>
</header>
<row>
<field>43</field>
</row>

<header>
<field>Column1</field>
</header>
<row>
<field>Testing Output</field>
</row>

<header>
<field>au_id</field>
<field>au_lname</field>
<field>au_fname</field>
<field>phone</field>
<field>address</field>
<field>city</field>
<field>state</field>
<field>zip</field>
<field>contract</field>
</header>
<row>
<field>172-32-1176</field>
<field>White</field>
<field>Johnson</field>
<field>408 496-7223</field>
<field>10932 Bigge Rd.</field>
<field>Menlo Park</field>
<field>CA</field>
<field>94025</field>
<field>true</field>
</row>
<row>
<field>213-46-8915</field>
<field>Green</field>
<field>Marjorie</field>
<field>415 986-7020</field>
<field>309 63rd St. #411</field>
<field>Oakland</field>
<field>CA</field>
<field>94618</field>
<field>true</field>
</row>

</DataSet>

Postnext
Anatoli TitarevSubject: XSL Transformation help needed (original and new provided)
Author: Anatoli Titarev
Date: 22 Apr 2005 08:54 PM
Please note in my post the order of the nodes and that the header node appears only once for TABLE6.

Postnext
(Deleted User) Subject: XSL Transformation help needed (original and new provided)
Author: (Deleted User)
Date: 25 Apr 2005 04:47 AM
Hi Anatoli,
the XSL I pasted in my previous message does generate only one <header> element for Table6. However, it fails to print the header for the first table.
This is the updated XSL

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="xml"/>
<xsl:template match="/">
<DATASET>
<xsl:apply-templates/>
</DATASET>
</xsl:template>

<xsl:template match="node()[substring(name(),1,5)='Table']">
<xsl:variable name="myname" select="name()"/>
<xsl:if test="not(preceding-sibling::*[1][name()=$myname])">
<xsl:comment><xsl:value-of select="name()"/></xsl:comment>
<header>
<xsl:for-each select="*">
<field><xsl:value-of select="name()"/></field>
</xsl:for-each>
</header>
</xsl:if>
<row>
<xsl:for-each select="*">
<field><xsl:value-of select="."/></field>
</xsl:for-each>
</row>
</xsl:template>

</xsl:stylesheet>

and this is the result of the transformation

<?xml version='1.0' ?>
<DATASET>

<!--Table-->
<header>
<field>Column1</field>
</header>
<row>
<field>25.7500</field>
</row>

<!--Table1-->
<header>
<field>Test_x0020_Decimal</field>
</header>
<row>
<field>45.15</field>
</row>

<!--Table2-->
<header>
<field>Today</field>
</header>
<row>
<field>2005-04-13T23:02:38.6270000+10:00</field>
</row>

<!--Table3-->
<header>
<field>Num_x0020_of_x0020_Authors</field>
</header>
<row>
<field>23</field>
</row>

<!--Table4-->
<header>
<field>Num_x0020_of_x0020_Employees</field>
</header>
<row>
<field>43</field>
</row>

<!--Table5-->
<header>
<field>Column1</field>
</header>
<row>
<field>Testing Output</field>
</row>

<!--Table6-->
<header>
<field>au_id</field>
<field>au_lname</field>
<field>au_fname</field>
<field>phone</field>
<field>address</field>
<field>city</field>
<field>state</field>
<field>zip</field>
<field>contract</field>
</header>
<row>
<field>172-32-1176</field>
<field>White</field>
<field>Johnson</field>
<field>408 496-7223</field>
<field>10932 Bigge Rd.</field>
<field>Menlo Park</field>
<field>CA</field>
<field>94025</field>
<field>true</field>
</row>

<row>
<field>213-46-8915</field>
<field>Green</field>
<field>Marjorie</field>
<field>415 986-7020</field>
<field>309 63rd St. #411</field>
<field>Oakland</field>
<field>CA</field>
<field>94618</field>
<field>true</field>
</row>

</DATASET>

Alberto

Posttop
Anatoli TitarevSubject: XSL Transformation help needed (original and new provided)
Author: Anatoli Titarev
Date: 26 Apr 2005 08:51 PM
This is great, Alberto. Thanks very much!

Meanwhile, I've got a solution from another source (not my work, I am not very skilled with XSL):

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="tables" match="OriginalDataSet/*" use="name()" />
<xsl:output method="xml" version="1.0" />
<xsl:template match="/">
<DataSet>
<xsl:apply-templates select="OriginalDataSet/*[count( . | key('tables',name())[1]) = 1]" mode="table" />
</DataSet>
</xsl:template>
<xsl:template match="*" mode="table">
<xsl:variable name="name" select="name()" />
<header>
<xsl:apply-templates select="*" mode="header" />
</header>
<xsl:for-each select="key('tables',$name)">
<row>
<xsl:apply-templates select="*" mode="row" />
</row>
</xsl:for-each>
</xsl:template>
<xsl:template match="*" mode="header">
<field>
<xsl:value-of select="name()" />
</field>
</xsl:template>
<xsl:template match="*" mode="row">
<field>
<xsl:value-of select="." />
</field>
</xsl:template>
</xsl:stylesheet>

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.