[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] Counting matching elements across two documents


Subject: [xsl] Counting matching elements across two documents
From: cknell@xxxxxxxxxx
Date: Mon, 17 Jan 2005 09:18:34 -0500

I process two documents with this stylesheet. The main document is a file called "matches.xml". The secondary document is a file called "roster.xml" (accessed via the document() function). The structures of the two input documents are:

matches.xml
===========
<matches>
  <week>
    <number>1</number>
    <match>
      <race-to>60</race-to>
      <player>
        <number>1</number>
        <spot>0</spot>
        <score>51</score>
        <lag>L</lag>
        <paid>2.50</paid>
      </player>
      <player>
        <number>2</number>
        <spot>16</spot>
        <score>60</score>
        <lag>W</lag>
        <paid>5</paid>
      </player>
    </match>
    &lt;!-- More matches here --&gt;
  <week>
  &lt;!-- More weeks here --&gt;
</matches>

roster.xml
===========
<players>
  <player established="yes" >
    <number>5</number>
    <lname>Knell</lname>
    <fname>Charles</fname>
    <handy-cap-history>
      <rating week-num="1">55.0</rating>
    </handy-cap-history>
  </player>
  &lt;!-- More players here --&gt;
</players>

The section of the stylesheet that's giving me problems is this:
================================================================
<xsl:for-each select="$roster/players/player">
  <xsl:sort select="lname" data-type="text" order="ascending" />
  <tr><td><xsl:value-of select="concat(lname,', ', fname)" /></td><td><xsl:value-of select="count(//player[number = ./number])" /></td></tr>
</xsl:for-each>

The shorthand way of saying what I'm looking for is "I want to count all the W's for each player." 

A more detailed, but more long-winded explanation is, I am trying to count all the "/matches/week/match/player/lag" elements with a value of "W" for each "$roster/players/player" element where the value of the "number" child of the current "$roster/players/player" matches any "/matches/week/match/player/number" element, but so far I can't work out the proper XPath expression. I have been successful in returning the total number of players in the roster or the total number of matches played, unfortunately, neither is what I'm looking for.

Can someone point out the correct XPath expression, please?

-- 
Charles Knell
cknell@xxxxxxxxxx - email


Current Thread
Keywords