Help needed with XSL

Discussion in 'XML' started by Rohit, Aug 12, 2004.

  1. Rohit

    Rohit Guest

    I have a XML which looks like this:

    <root>
    <row Site="ABC" ClassroomName="Calculus" StartDate="Jun 1 2004"/>
    <row Site="ABC" ClassroomName="Biology" StartDate="Jun 10 2004"/>
    <row Site="XYZ" ClassroomName="History" StartDate="Jun 12 2004"/>
    <row Site="XYZ" ClassroomName="English" StartDate="Jun 1 2004"/>
    </root>

    I want this to be displayed in a <table> as
    ----------------------------------------------
    ABC
    ClassroomName StartDate
    Calculus June 1 2004
    Biology June 10 2004

    XYZ
    ClassroomName StartDate
    History June 12 2004
    English June 1 2004
    ----------------------------------------------

    As you can see, the 'Site' goes first, then we have column headings
    for Classroom and Date, then the rows belonging to that 'site'. The
    XML is always sorted on 'Site'.

    This is what I have been come up with:
    ------------------------------------------------------------------------------
    <xsl:template match="/">
    <table width="100%">
    <tr>
    <td><b>Site</b></td>
    <td><b>ClassroomName</b></td>
    <td><b>Start Date</b></td>
    </tr>
    <xsl:apply-templates select="ROOT/row" />
    </table>
    </xsl:template>

    <xsl:template match="ROOT/row">
    <tr>
    <td><xsl:value-of select="@Site"></xsl:value-of></td>
    <td><xsl:value-of select="@ClassroomName"></xsl:value-of></td>
    <td><xsl:value-of select="@StartDate"></xsl:value-of>
    </tr>
    </xsl:template>
    --------------------------------------------------------------------

    Can somebody please advice me how to get the above formatting by
    modifying this xsl template.

    Thanks,
    Rohit
     
    Rohit, Aug 12, 2004
    #1
    1. Advertising

  2. > Can somebody please advice me how to get the above formatting by
    > modifying this xsl template.


    You have to get distinct @Site values. This trick is done in xslt by
    using key() feature. There is a nice template in the excellent Cooktop
    xml editor.

    You can also choose to use a less standard thing as e-xslt
    (www.exslt.org) which provide a distinct function
    (http://www.exslt.org/set/functions/distinct/index.html)

    Here is a working xsl (consider some improvement in the key to avoid the
    //row select) :

    ---------------------------------------------------------------------
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="site" match="@Site" use="."/>


    <xsl:template match="/">

    <!-- the key declaration must be global ! -->

    <!--
    substitute xpath with xpath to your xpath to the node to iterate
    substitute @att with the name of the attribute or . for txt node.
    -->
    <xsl:for-each select="root/*[count(@Site | key('site', @Site)[1]) = 1]">

    <b><xsl:value-of select="@Site" /></b>

    <table width="100%">
    <tr>
    <td><b>ClassroomName</b></td>
    <td><b>Start Date</b></td>
    </tr>
    <xsl:apply-templates select="//row[@Site=current()/@Site]" />
    </table>
    </xsl:for-each>
    </xsl:template>

    <xsl:template match="row">
    <tr>
    <td><xsl:value-of select="@ClassroomName" /></td>
    <td><xsl:value-of select="@StartDate" /></td>
    </tr>
    </xsl:template>

    </xsl:stylesheet>
    ---------------------------------------------------------------------

    Hth

    --
    RĂ©mi Peyronnet
     
    =?ISO-8859-1?Q?R=E9mi_Peyronnet?=, Aug 12, 2004
    #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. Kevin Flood
    Replies:
    0
    Views:
    1,052
    Kevin Flood
    Sep 8, 2004
  2. Philip
    Replies:
    1
    Views:
    537
    Kenneth Stephen
    Mar 1, 2004
  3. Replies:
    1
    Views:
    3,687
    A. Bolmarcich
    May 27, 2005
  4. =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=

    Help with XSL/XSL:FO for producing revision indicator bar in document.

    =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=, Apr 28, 2006, in forum: XML
    Replies:
    6
    Views:
    589
    =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?=
    May 3, 2006
  5. Gina_Marano

    XSL help needed for dummy

    Gina_Marano, May 30, 2007, in forum: XML
    Replies:
    2
    Views:
    403
    roy axenov
    May 31, 2007
Loading...

Share This Page