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 10 11 12 13 14 15 16 17 18 19 20 Go to previous topicPrev TopicGo to next topicNext 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

 
Topic Page 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 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.