Advanced XSLT: Creating a two-column HTML table from an unsorted listof XML nodes Options

E

Eric

Say you have some XML elements that are NOT SORTED:

<element name="sam" />
<element name="bob" />
<element name="alice" />
<element name="mary" />
<element name="fred" />
<element name="barbara" />
etc.


and you want to create a 2-column SORTED html table with the names,
thus if we had the 6 elements above


alice fred
barbara mary
bob sam


Of course you can have any number of elements....
I have tried a few techniques but to no avail...I am using XSLT 1.1
on
Windows.


I assume something this basic is doable. Can anyone help?
 
P

Pavel Lepin

Eric said:
<element name="sam" />
<element name="bob" />
<element name="alice" />
<element name="mary" />
<element name="fred" />
<element name="barbara" />

and you want to create a 2-column SORTED html table with
the names, thus if we had the 6 elements above

alice fred
barbara mary
bob sam

Of course you can have any number of elements....
I have tried a few techniques but to no avail...I am using
XSLT 1.1 on Windows.

First time I hear about XSLT 1.1. Hmmm...

From XSL Transformations (XSLT) Version 1.1 W3C Working
Draft 24 August 2001:

As of 24 August 2001 no further work on this draft is
expected. The work on XSLT 2.0 identified a number of
issues with the approaches being pursued in this document;
solutions to the requirements of XSLT 1.1 will be
considered in the development of XSLT 2.0 [XSLT20REQ].
Other than this paragraph, the document is unchanged from
the previous version.
I assume something this basic is doable.

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="*[element]">
<xsl:param name="cols" select="2"/>
<xsl:variable name="elements" select="element"/>
<xsl:variable name="elt-cnt"
select="count($elements)"/>
<xsl:apply-templates select="$elements"
mode="row-start">
<xsl:sort select="@name" order="ascending"/>
<xsl:with-param name="elements" select="$elements"/>
<xsl:with-param name="rows"
select="$elt-cnt div $cols"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="element" mode="row-start">
<xsl:param name="elements"/>
<xsl:param name="rows"/>
<xsl:if test="position()&lt;=$rows">
<row>
<xsl:apply-templates select="."/>
<xsl:apply-templates select="$elements"
mode="row">
<xsl:sort select="@name" order="ascending"/>
<xsl:with-param name="rows" select="$rows"/>
<xsl:with-param name="row" select="position()"/>
</xsl:apply-templates>
</row>
</xsl:if>
</xsl:template>
<xsl:template match="element" mode="row">
<xsl:param name="rows"/>
<xsl:param name="row"/>
<xsl:if
test=
"
(((position()-1) mod $rows)=($row -1)) and
(position()!=$row)
">
<xsl:apply-templates select="."/>
</xsl:if>
</xsl:template>
<xsl:template match="element">
<cell>
<xsl:copy-of select="@name"/>
</cell>
</xsl:template>
</xsl:stylesheet>

Is it clunky? Sure is. Does it work? Sure does.
 

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

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top