xsl-list
[Top] [All Lists]

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

2010-04-04 12:37:38
The following function returns the list of integers in the range 1 to
2nd argument that are missing from the first argument.

<xsl:function name="my:missing">
  <xsl:param name="numbers" as="xs:integer*"/>
  <xsl:param name="max" as="xs:integer"/>
  <xsl:choose>
    <xsl:when test="$max = 0">
      <xsl:value-of select="()"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of
         select="(my:missing($numbers,$max - 1),
            (if( index-of($numbers,$max) ) then () else $max))"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:function>

In your stylesheet you might call it like so:
     <xsl:text>Missing from series: </xsl:text>
     <xsl:value-of
select="my:missing(current-group()/series/@number,current-group()[last()]/series/@number)"/>

Real numbers in the 1st argument won't matter since the function tests
for integers only.

-W

On Sun, Apr 4, 2010 at 6:28 PM, Kevin Grover <kevin(_at_)kevingrover(_dot_)net> 
wrote:

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

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--