Re: [xsl] create a new node-set and then reference a key problem

Subject: Re: [xsl] create a new node-set and then reference a key problem
From: David Carlisle <davidc@xxxxxxxxx>
Date: Tue, 20 Jul 2004 12:29:27 +0100
I'm confused a bit by the problem statement, I _think_ you are only
trying to select one node, but your stylesheet was generating a table?

  I want to display the roomType whose first occurence
  has a higher "count" value than the other first
  occurences of roomType and whose subsequent "count"
  values are all greater than zero i.e. type 2D in the
  above XML source. I only want to display this room
  type once.

If I ignore "and whose subsequent "count"  values are all greater than
zero" bit then this is selecting the node which is the 1st occurrence of
its id value and no other first occurrence has a greater count, that
could be

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">
<xsl:output indent="yes"/>
<xsl:key name="roomID" match="roomType" use="@id"/>

<xsl:variable name="first"
select="/rooms/roomType[generate-id()=generate-id(key('roomID',@id))]"/>

<xsl:template match="/rooms">
<xsl:copy-of select="$first[not($first/@count &gt; @count)]
 "/>
</xsl:template>

</xsl:stylesheet>

which produces:

$ saxon rooms.xml rooms.xsl
<?xml version="1.0" encoding="utf-8"?>
<roomType id="2" count="6" type="2D"/>


but type2 has a count of zero so you don't want that so...
filter those out:

<xsl:variable name="first"
select="/rooms/roomType[not(key('roomID',@id)/@count=0)]
                       [generate-id()=generate-id(key('roomID',@id))]"/>

which produces


$ saxon rooms.xml rooms.xsl
<?xml version="1.0" encoding="utf-8"?>
<roomType id="3" count="4" type="3D"/>

changing the copy-of to apply templates hopefully lets you produce what
you want.

David

________________________________________________________________________
This e-mail has been scanned for all viruses by Star Internet. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

Current Thread