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)
-> - Stylus Studio - Registrar en o... (1)
-> + Stylus Studio - Registrar en o... (2)
-> + Can a pipeline send a file by ... (2)
-> + After Updateing WIN10 to WIN11... (12)
-> + Where do I add the custom java... (3)
-> + Where is the Diagram tab? (5)
-> + Applying XSLT to Word DOCX/XML (2)
-> - CSV conversion via ConvertToXM... (1)
-> + Text symbols in SS not same as... (4)
-> + Exposing xquery as webservice ... (6)
-> + Syntax Identifier (2)
-> + Saving a Converted XML as an X... (5)
-> + Output document cannot be pars... (4)
-> - Archiving output from conversi... (1)
-> + EDIFACT guideline from Stylus ... (3)
-> + CSV file putting all the data ... (5)
-> + Can't install Home version 64b... (5)
-> + presale - Can I covers this sc... (5)
-> + Problem with UNB (5)
-> + Splitting EDIFACT files pipeli... (4)
-- [1-20] [21-40] [41-60] Next
+ Website Feedback (249)
+ XSLT Help and Discussion (7625)
+ XQuery Help and Discussion (2017)
+ Stylus Studio FAQs (159)
+ Stylus Studio Code Samples & Utilities (364)
+ Stylus Studio Announcements (113)
Topic  
Postnext
Dan WonacottSubject: HTML tables from sorted XSL
Author: Dan Wonacott
Date: 28 Nov 2007 08:41 PM
Hi,
I'm trying to create a two-dimensional table where each cell is filled according to XML node data which are sorted. This is something we commonly require and have yet to find a solution.
The only solution I can come up with cheats by using CDATA sections to open and close the table rows which, although totally effective, I don't believe is using XSL appropriately and I'd like to find a better solution. The XML and XSL should explain the problem more clearly:

The XML:
<document>
<test>
<value>G</value>
</test>
<test>
<value>A</value>
</test>
<test>
<value>D</value>
</test>
<test>
<value>C</value>
</test>
<test>
<value>F</value>
</test>
<test>
<value>B</value>
</test>
<test>
<value>E</value>
</test>
</document>

The XSL:
<xsl:template match="/">
<table>
<xsl:apply-templates select="/document/test" mode="makeCell">
<xsl:sort select="value"/>
</xsl:apply-templates>
</table>
</xsl:template>

<xsl:template match="test" mode="makeCell">
<xsl:if test="position() mod 3 = 1">
<xsl:text disable-output-escaping="yes"><![CDATA[<tr>]]></xsl:text>
</xsl:if>
<td><xsl:value-of select="value"/></td>
<xsl:if test="position() mod 3 = 0 or position() = last()">
<xsl:text disable-output-escaping="yes"><![CDATA[</tr>]]></xsl:text>
</xsl:if>
</xsl:template>

The output:
<table>
<tr>
<td>A</td>
<td>B</td>
<td>C</td>
</tr>
<tr>
<td>D</td>
<td>E</td>
<td>F</td>
</tr>
<tr>
<td>G</td>
</tr>
</table>

If it wasn't for the sort I would use a following-sibling method as Michael Kay suggests in XSLT 2.0.

Any suggestions?

Thanks. Dan Wonacott

Postnext
(Deleted User) Subject: HTML tables from sorted XSL
Author: (Deleted User)
Date: 30 Nov 2007 04:44 AM
Hi dan,
maybe it's not the most efficient way of doing this, but this should work (unless you have duplicate values...):

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

<xsl:template match="/">
<table border="1">
<xsl:for-each select="/document/test">
<xsl:sort select="value"/>
<xsl:if test="position() mod 3 = 1">
<tr>
<xsl:call-template name="print3">
<xsl:with-param name="startAt" select="value/text()"/>
</xsl:call-template>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</xsl:template>

<xsl:template name="print3">
<xsl:param name="startAt"/>
<xsl:for-each select="/document/test[value/text() &gt;= string($startAt)]">
<xsl:sort select="value"/>
<xsl:if test="position() &lt;= 3">
<td><xsl:value-of select="."/></td>
</xsl:if>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

Hope this helps,
Alberto

Posttop
Dan WonacottSubject: HTML tables from sorted XSL
Author: Dan Wonacott
Date: 02 Dec 2007 03:28 PM
Hi Alberto,

Thanks for your reply, it's an excellent solution. I was hoping there might be a way to do this without repeating the sort; I imagine this to be inefficient with large XML but I can't see there's an XSL alternative. Fortunately, the data I'm working with is pretty small so this will fit the bill perfectly. Thanks again!

Dan

   
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.