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

Discussion in 'XML' started by Eric, Feb 29, 2008.

  1. Eric

    Eric Guest

    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?
    Eric, Feb 29, 2008
    #1
    1. Advertising

  2. Eric

    Pavel Lepin Guest

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

    Eric <> wrote in
    <>:
    > <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.

    --
    When all you have is a transformation engine, everything
    looks like a tree.
    Pavel Lepin, Feb 29, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gavnosis
    Replies:
    0
    Views:
    493
    gavnosis
    Aug 2, 2003
  2. th3dude
    Replies:
    0
    Views:
    419
    th3dude
    Jan 3, 2007
  3. Claudio Nieder
    Replies:
    4
    Views:
    681
  4. Replies:
    6
    Views:
    656
  5. metaperl.com
    Replies:
    7
    Views:
    252
    metaperl.com
    Sep 18, 2008
Loading...

Share This Page