[xsl] I'm trying to get the last item in a group (but getting the first), xslt v2

Subject: [xsl] I'm trying to get the last item in a group (but getting the first), xslt v2
From: "Siddhi Thakkar" <siddhi.thakkar@xxxxxxxxxxxxxx>
Date: Mon, 5 Apr 2010 14:33:01 +0530
Hi Kevin,
I hereby prpose a solution to the problem you posted on mulberry.
In your xslt, use <xsl:value-of select="following-sibling::book[last()]/title"/> and <xsl:value-of select="following-sibling::book[last()]/series/@number"/> instead of <xsl:value-of select=".[last()]/title"/> and xsl:value-of select=".[last()]/series/@number"/> respectively.


You need to do this because when you write <xsl:for-each-group select="book[series and date-read]" group-by="series/@title"> your context node becomes that book element whose series/@title value is unique from the values processor found earlier. Hence, not all book elements will enter this loop (I think this is what you thought when you wrote . instead of following-sibling), only two book nodes will enter this for-each-group loop i.e. the first book element whose series/@title value is Hitchhiker Series and the first book element whose series/@title value is The Magic of Xanth and all other members in their corressponding groups become the context book node's following sibling.

Can you let me know what do you exactly want to do with book numbers with an example and where do you want to write this condition, inside the for-each-group loop or somewhere else? Will definitely try to help you out.



Date: Sun, 4 Apr 2010 11:28:46 -0500
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
From: Kevin Grover <kevin@xxxxxxxxxxxxxxx>
Subject: I'm trying to get the last item in a group (but getting the first),
xslt v2
Message-ID: <o2t4dd6a8f01004040928g600e1a5bk39aca963259752b2@xxxxxxxxxxxxxx>

Using XSLT 2, I'm trying to read through a list of books that I keep
in XML.  I want to get
the last book that I've read in each series.

I tried to use XML groups and get the last() item, but it's not
working as I expected: it always comes up as the first book in the
series.

What I'm a going wrong?

As an aside, I eventually also want to look through the books for
missing books (starting with 1 and ignoring any series with a
non-integer number, like "3.5" -- which I use to represent a short
story that comes in the storyline at that point.)  NOTE: Since I could
not get the last number in the series, I have not even attempted this
in the XSLT file yet.

I've included some sample files at the end of the text: text.xml (the
input) and toread.xsl (the XSLT).

This is the command I'm using:

java -jar saxon9he.jar -s test.xml -o toread.txt toread.xsl

I've included the received output (toread.txt) and the expected output
(toread-expected.txt).

Any pointers appreciated. Thanks.

Files (preceded by '***** FILENAME')

***** text.xml
<?xml version="1.0" encoding='utf-8'?>
<?oxygen RNGSchema="schema/books.rnc" type="compact"?>
<booklist owner="Kevin Grover" cre-date="1991-01-05" mod-date="2010-04-04">

<author>
 <name file-as="Adams, Douglas">Douglas Adams</name>

 <book>
   <title>Hitchhiker's Guide to the Galaxy</title>
   <date>1980</date>
   <owner>KOG</owner>
   <date-read>1984</date-read>
   <date-read>1999-06</date-read>
   <genre>Comedy</genre>
   <genre>SciFi</genre>
   <type>book/paperback</type>
   <series title="Hitchhiker Series" number="1" />
   <comment>Funny!</comment>
 </book>

 <book>
   <title>The Restaurant at the End of the Universe</title>
   <date>1980</date>
   <owner>KOG</owner>
   <date-read>1984</date-read>
   <genre>Comedy</genre>
   <genre>SciFi</genre>
   <series title="Hitchhiker Series" number="2" />
   <comment>Funny!</comment>
 </book>

 <book>
   <title>Life, the Universe and Everything</title>
   <date>1982</date>
   <owner>KOG</owner>
   <date-read>1984</date-read>
   <genre>Comedy</genre>
   <genre>SciFi</genre>
   <series title="Hitchhiker Series" number="3" />
   <comment>Funny!</comment>
 </book>

 <book>
   <title>So Long and Thanks for all the Fish</title>
   <date>1999</date>
   <owner>KOG</owner>
   <date-read>1999</date-read>
   <genre>Comedy</genre>
   <genre>SciFi</genre>
   <isbn>0-345-39183-7</isbn>
   <series title="Hitchhiker Series" number="4" />
 </book>

</author>

<author>
 <name file-as="Anderson, Kevin J.">Kevin J. Anderson</name>
 <book>
   <title>Identity Crisis</title>
   <date>2000</date>
   <owner>FWS</owner>
   <date-read>2002-03</date-read>
   <genre>SciFi</genre>
   <comment>Good.  Downloaded from Fictionwise.com</comment>
 </book>

 <book>
   <title>Hopscotch</title>
   <date>2003</date>
   <owner>GET</owner>
   <genre>SciFi</genre>
   <comment>Elaboration of 'Identity Crisis' theme: personality
transference.</comment>
 </book>

</author>

<author>
 <name file-as="Anthony, Piers">Piers Anthony</name>

 <book>
   <title>The Source of Magic</title>
   <date>1979</date>
   <owner>KOG</owner>
   <date-read>1988</date-read>
   <genre>Fantasy</genre>
   <type>book/paperback</type>
   <series title="The Magic of Xanth" number="2" />
 </book>

 <book>
   <title>Castle Roogna</title>
   <date>1979</date>
   <owner>KOG</owner>
   <date-read>1988</date-read>
   <genre>Fantasy</genre>
   <type>book/paperback</type>
   <series title="The Magic of Xanth" number="3" />
 </book>

 <book>
   <title>Ogre, Ogre</title>
   <date>1982</date>
   <owner>KOG</owner>
   <date-read>1988</date-read>
   <genre>Fantasy</genre>
   <type>book/paperback</type>
   <series title="The Magic of Xanth" number="5" />
 </book>

 <book>
   <title>Night Mare</title>
   <date>1982</date>
   <owner>KOG</owner>
   <date-read>1988</date-read>
   <genre>Fantasy</genre>
   <type>book/paperback</type>
   <series title="The Magic of Xanth" number="6" />
 </book>

</author>

</booklist>

<!--
Local Variables:
mode: nxml
mode: auto-fill
mode: flyspell
End:
-->

***** toread.xsl
<?xml version="1.0"?>

<!-- from article: http://www.xml.com/pub/a/2003/11/05/tr.html -->

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  version="2.0">

<xsl:output method="text"/>

<!-- ********************************************* -->

 <xsl:template match="/">
   <xsl:text>Last books read in each series.

</xsl:text>
   <!-- TODO: Add date(file), date(current), vcsversion, ..? -->
   <xsl:apply-templates select="/booklist/author"/>
 </xsl:template>

<!-- ********************************************* -->

 <xsl:template match="author">
    <xsl:for-each-group
select="book[series and date-read]"
group-by="series/@title">
     <xsl:text>
-------------------------------------------------------
</xsl:text>
     <xsl:text>Series: </xsl:text>
     <xsl:value-of select="current-grouping-key()"/>
     <xsl:text>; Author: </xsl:text>
     <xsl:value-of select="../name/text()"/>
     <xsl:text>
</xsl:text>
     <!-- Last book in series -->
     <!-- TODO: this is incorrect, it's the first book in the series -->
     <xsl:text>Last Book Read: </xsl:text>
     <xsl:value-of select=".[last()]/title"/>
     <xsl:text>, #</xsl:text>
     <!-- Number in series -->
     <!-- TODO: This is incorrect, it always 1 -->
     <xsl:value-of select=".[last()]/series/@number"/>
     <!-- TODO: if there are missing books in the series, put them here.
                e.g. if I read 3-6 then last read is 6 and missing are
                1 and 2 -->
     <xsl:text>
</xsl:text>
   </xsl:for-each-group>
 </xsl:template>

</xsl:stylesheet>

***** toread.txt
Last books read in each series.

-------------------------------------------------------
Series: Hitchhiker Series; Author: Douglas Adams
Last Book Read: Hitchhiker's Guide to the Galaxy, #1

-------------------------------------------------------
Series: The Magic of Xanth; Author: Piers Anthony
Last Book Read: A Spell for Chameleon, #1

***** toread-expected.txt
Last books read in each series.

-------------------------------------------------------
Series: Hitchhiker Series; Author: Douglas Adams
Last Book Read: So Long and Thanks for all the Fish, #4

-------------------------------------------------------
Series: The Magic of Xanth; Author: Piers Anthony
Last Book Read: Night Mare, #6

Missing: #1, #4

------------------------------

Current Thread