XSLT: sorting and grouping

Discussion in 'XML' started by Christian Ludwig, Nov 24, 2003.

  1. Hello,

    I have the following problem. Suppose you habe an XML File (containing
    Bibliography-Data) of the form:

    <bib>
    <bibentry>
    <title>Booktitle 1</title>
    <year>1990</year>
    <!-- some more elements -->
    </bibentry>

    <bibentry>
    <title>Booktitle 2</title>
    <year>1991</year>
    <!-- more elements -->
    </bibentry>

    <!-- etc. -->

    </bib>

    Now, if I want to show all entries sorted by year, I do something like:

    <xsl:for-each select="bib/bibentry">
    <xsl:sort select="year" data-type="number" order="ascending" />
    <p><xsl:value-of select="year"/>: <xsl:value-of select="title"/></p>
    </xsl:for-each>

    Now the problem:
    How can I add (e.g.) extra space, when a new year starts: If I have 10
    Books in the year 1990 and 5 books in the year 1991, I want to place a
    special element in the output between the last book of 1990 and the
    first book of 1991 in order to have a group per year.

    To put it in a more abstract form:
    How to implement decisions (conditionals) in the xsl-File, where the
    condition depends not only on the current node, but also on the last node?


    Can anybody help?

    C. Ludwig
     
    Christian Ludwig, Nov 24, 2003
    #1
    1. Advertising

  2. Christian Ludwig <> wrote:

    > How can I add (e.g.) extra space, when a new year starts: If I have 10
    > Books in the year 1990 and 5 books in the year 1991, I want to place a
    > special element in the output between the last book of 1990 and the
    > first book of 1991 in order to have a group per year.


    <xsl:template match="/">

    <!-- consider all years, one by one, sorted, no double -->
    <xsl:for-each select="/bib/bibentry/year
    [not(../preceding-sibling::bibentry/year = .)]">
    <xsl:sort/>

    <xsl:call-template name="handle-year">
    <xsl:with-param name="year" select="."/>
    </xsl:call-template>

    </xsl:for-each>

    </xsl:template>


    <!-- display all bibentries for a give year -->
    <xsl:template name="handle-year">
    <xsl:param name="year"/>

    <xsl:variable name="bibentries" select="/bib/bibentry[year = $year]"/>

    <H1>
    Year <xsl:value-of select="$year"/> starts here,
    it contains <xsl:value-of select="count($bibentries)"/> books.
    </H1>

    <xsl:for-each select="$bibentries">
    <xsl:value-of select="title"/>
    <br/>
    </xsl:for-each>

    <H1>
    Year <xsl:value-of select="$year"/> ends here.
    </H1>

    </xsl:template>


    --
    David
     
    David Andriana, Nov 26, 2003
    #2
    1. Advertising

  3. >
    > <xsl:template match="/">
    >
    > <!-- consider all years, one by one, sorted, no double -->
    > <xsl:for-each select="/bib/bibentry/year
    > [not(../preceding-sibling::bibentry/year = .)]">
    > <xsl:sort/>
    >
    > <xsl:call-template name="handle-year">
    > <xsl:with-param name="year" select="."/>
    > </xsl:call-template>
    >
    > </xsl:for-each>
    >
    > </xsl:template>
    >


    Thank you very much, works great.
    C. Ludwig
     
    Christian Ludwig, Nov 26, 2003
    #3
    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. Jack Wayne
    Replies:
    0
    Views:
    475
    Jack Wayne
    Jan 24, 2005
  2. John Galenski

    XSLT Grouping and Counting Question

    John Galenski, Feb 6, 2009, in forum: XML
    Replies:
    1
    Views:
    1,974
    Martin Honnen
    Feb 6, 2009
  3. Replies:
    6
    Views:
    145
    Florian GroƟ
    Jun 27, 2005
  4. Howard Roberts

    Grouping/Sorting an Array of objects

    Howard Roberts, Jul 21, 2008, in forum: Ruby
    Replies:
    5
    Views:
    165
    Erik Veenstra
    Jul 22, 2008
Loading...

Share This Page