xsl-list
[Top] [All Lists]

Re: xsl:key grouping problem

2004-11-17 14:06:18
Thank you for the detialed explanation. It was very
helpful. One last thing. I would like to display my
results in 2 or 3 table columns. The way it is right
now, it will only display one column. Please see xsl
below. Thanks again.

  <xsl:key name="groups" match="country"
use="concat(country_group, 
@year, @num)"/>

  <xsl:template match="myroot">
    <table>
    <xsl:for-each select="country[generate-id() =
generate-id(key('groups', concat(country_group,
'2004', '2')))]">
      <tr><td>
         <xsl:value-of select="country_group"/>
      </td></tr>
    </xsl:for-each>
    </table>
  </xsl:template>
  
Thanks again.
Mark


--- David Carlisle <davidc(_at_)nag(_dot_)co(_dot_)uk> wrote:



Using xsl keys seems like a tough concept to
understand(atleast for me). Are there other
novices

keys themselves are simple enough, but using them
(as here) for grouping
isn't at all obvious and is normally called (here at
least) Muenchian
grouping after Steve Muench who first thought of
this.
Jeni's site is the usual reference for a tutorial.

http://www.jenitennison.com/xslt/grouping/index.html

 How does generate-id on country be equal to
 generate-id on the key? That's where I am
confused.
 country[generate-id() = generate-id(key('groups',
 concat(country_group, '2004', '2'))) ??


The trick here is that if you are on a country then
concat(country_group, '2004', '2')
is the key string you want to use and so

key('groups', concat(country_group, '2004', '2'))

will return all the nodes that we want to consider
to be grouped with
this country.

Now the test is trying to say "is this node the
first node in this
group" (if so start processing the group, if not do
nothing as this node
was already handled when the rest of the group was
handled, on the first
node)

so this node is .

and the first node in the current group is

key('groups', concat(country_group, '2004', '2'))[1]

so you want to know if

. is key('groups', concat(country_group, '2004',
'2'))[1]

now in XPath 2 (draft) there is an "is" operator
that tests node
identity and the above line is actually valid Xpath2
but in Xpath 1
there is no operator. You can't "is". You can't use
use = as


. = key('groups', concat(country_group, '2004',
'2'))[1]

would test if the string value of the current node
was equal to the
string value of the first node in the group, which
isn't what we want.

however generate-id returns a unique string for each
node so you can say

generate-id(.) = generate-id(key('groups',
concat(country_group, '2004', '2'))[1])

and that will be true just if the current node is
the first node in the
group.

This idiom isn't at all obvious but it is a FAQ and
when you've seen it
most days on this list for 5 years you tend to take
some syntactic
shortcuts:

generate-id(.)

can be written

generate-id() because . is implied if you supply no
argument.

generate-id(key('groups', concat(country_group,
'2004', '2'))[1])

can be written

generate-id(key('groups', concat(country_group,
'2004', '2')))

because as for most string generating functions in
XSLT 1, if you supply
a node set as an argument it will take the first
node in teh set in
document order and use that and silently ignore any
other nodes, in
other words [1] is implictly applied.

David


________________________________________________________________________
This e-mail has been scanned for all viruses by
Star. 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

________________________________________________________________________


--~------------------------------------------------------------------
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>
--~--




__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.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>
--~--



<Prev in Thread] Current Thread [Next in Thread>