[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

I'm trying to get the last item in a group (but gettin

Subject: I'm trying to get the last item in a group (but getting the first), xslt v2
From: Kevin Grover <kevin@xxxxxxxxxxxxxxx>
Date: Sun, 4 Apr 2010 11:28:46 -0500
 I'm trying to get the last item in a group (but gettin
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.