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
Conferences Close Tree View
+ Stylus Studio Feature Requests (1192)
+ Stylus Studio Technical Forum (14621)
+ Website Feedback (249)
- XSLT Help and Discussion (7625)
-> - XSLT slicing and orphan contro... (1)
-> + How to delete CDATA using XSL... (15)
-> + Remove only root namespace, bu... (3)
-> + String Parsing (3)
-> + Calling Java method from XSL (3)
-> + populate xsl file from xsd fil... (5)
-> + formating a number like #'###.... (3)
-> + Help with grouping children, f... (20)
-> + Need help for newbie! (3)
-> + xpath in string format (2)
-> + How to populate elements at ra... (4)
-> + Changing Date format in XLST (2)
-> + Please help....How could I rem... (2)
-> + Counting children and grandchi... (7)
-> + Nested tags (4)
-> + Problem with XSLT on a Soap En... (2)
-> + Problem with XSLT on a Soap En... (5)
-> + Comparison between XSLT proces... (2)
-> + Printing non-existing value (9)
-> + big input file for XSLT (14)
-> + Is there any way to store xpat... (2)
-> + Encoding special charecters (2)
-> + Sorting (3)
-> + XML to PDF using XSL:FO (3)
-> + How to find the line number in... (2)
-> + How to set a marker for paper ... (2)
-> + Not equality Function (2)
-> + Counting Elements (4)
-> + Looking for help or code samp... (2)
-> + Help needed in pattern matchin... (2)
-> + Repeaters and Ifs (2)
-> + select adjacent nodes in xpath... (3)
-> + Stripping of Empty Target Elem... (2)
-> + auto-generate Edifact Intercha... (5)
-> + Qname Error in xsl:element (2)
-> + [Newbie] Type conversion and f... (2)
-> + Do ...when (2)
-> + Using Key (3)
-> + sorting elements in option (2)
-> + NaN(not available) value repla... (3)
-> + infinite loop error when tryin... (9)
-> + Escape a character in the stri... (3)
-> + find sum of 2 elements in xml (2)
-> + placing a ' * ' by reading the... (3)
-> + Problem in dispaying Current D... (4)
-> + Merging Table Cells in XSLT re... (3)
-> + xsl fo block-container and foo... (2)
-> + Is is possible to create a .CO... (2)
-> + printing a parent node value i... (2)
-> + Mime header info setup? (3)
-> + Calculating a Date based on 4 ... (3)
-> + XSLT formatting number issue (10)
-> + map repeating elements separat... (3)
-> + Combining XML elements based o... (3)
-> + Convert XML to UTF-8 (8)
-> + XSLT and MS Access 2007 Optio... (5)
-> - WORDML list to XML list (1)
-> + [Newbie] Using lookups in XSLT... (3)
-> + Comparing 2 passed Date Values (2)
-> + ERROR XQueryMediator Error du... (2)
-> + XML / XSLT... Unordered lists ... (2)
-> + Specify image path in XSL (2)
-> + Variable within a variable (3)
-> + How to call jfreechart method ... (8)
-> + leading and trailing white spa... (2)
-> + XSLT to Html problems (6)
-> + How to set classpath, when i n... (3)
-> + Error in using functions (4)
-> + To find the occurence of a cha... (3)
-> + PLMXML to XML (4)
-> + Transform multiple node values... (2)
-> + I am in need of svg example(s)... (13)
-> + count in XSLT, Need Help Pleas... (2)
-> + Help with XML.Report and Gener... (2)
-> + for each loop and variable (5)
-> + Problem with selecting nodes f... (3)
-> + Getting "Expected elemenmt mis... (2)
-> + Help with attached XML mapping... (8)
-> + xml:space="Preserve" and Embed... (2)
-> + XSLT and Multiple Strings (2)
-> + XSL Date format (2)
-> + check element contents against... (3)
-> + XML to XML Namespace conversio... (2)
-> - Supressing subgroup headers in... (1)
-> - HTML to fo conversion using XS... (1)
-> + No Topic (2)
-> + parsing xml using xslt HELP (2)
-> + Multiple Page Headers (2)
-> + PLM Xml To Generic XML (3)
-> + XSLT/XPath Help (2)
-> + XSL Space Issue (2)
-> + wanting help with xslt custom ... (3)
-> - Update input xml content with ... (1)
-> + Error: namespace prefix xsl on... (3)
-> + Error in my xsl:if test... (2)
-> - calling javascript from XSLT (1)
-> + Error when trying to a call a ... (2)
-> - HTTP POST from XSLT (1)
-> + passing xml document as input ... (2)
-> + XSLT for WORDML table to CALS ... (2)
-- Previous [361-380] [381-400] [401-420] Next
+ XQuery Help and Discussion (2015)
+ Stylus Studio FAQs (159)
+ Stylus Studio Code Samples & Utilities (364)
+ Stylus Studio Announcements (113)
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>

   
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.