xsl-list
[Top] [All Lists]

Re: [xsl] selecting nodes based on sibling values

2010-11-23 09:09:34
Fabien Tillier wrote:

<data>
        <row>
                <level>40</level>
                <keycode>1254.45.12</keycode>
        </row>
        <row>
                <level>50</level>
                <keycode>1254.45.12.7</keycode>
        </row>
        <row>
                <level>50</level>
                <keycode>1254.45.12.8</keycode>
        </row>
        <row>
                <level>50</level>
                <keycode>1254.45.12.9</keycode>
        </row>

        <row>
                <level>40</level>
                <keycode>99.25.6</keycode>
        </row>
        <row>
                <level>50</level>
                <keycode>99.25.6.45</keycode>
        </row>
        <row>
                <level>50</level>
                <keycode>99.25.6.46</keycode>
        </row>
</data>

What could be the XPath expression to get the maximum number of nodes of
level=50 in data those keycode starts like the level=40 line ?
Here the answer would be 3 as the maximum number of level = 50 nodes for
a given level = 40 is 3

The following outputs "3" for your sample:

<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  version="2.0">

  <xsl:param name="l1" select="'50'"/>
  <xsl:param name="l2" select="'40'"/>

  <xsl:key name="k1" match="row" use="level"/>

  <xsl:template match="/">
    <xsl:variable name="groups" as="element(group)*">
      <xsl:for-each select="key('k1', $l2)">
        <group>
<xsl:copy-of select="key('k1', $l1)[starts-with(keycode, current()/keycode)]"/>
        </group>
      </xsl:for-each>
    </xsl:variable>
    <xsl:value-of select="max($groups/count(row))"/>
  </xsl:template>

</xsl:stylesheet>

--

        Martin Honnen
        http://msmvps.com/blogs/martin_honnen/

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