xsl-list
[Top] [All Lists]

Re: [xsl] Grouping problem

2011-08-15 17:37:10
At 2011-08-15 15:38 +0200, graham.heath wrote:
I have an xml grouping problem that has so far defeated me.
...
is it possible to group pairs such that each player occurs only once in each group?
...
I was of the opinion that some form of xsl:for-each-group would suffice but have been unable to devise a group-by expression that works..

Do you have to use groups?

Below is an XSLT 1.0 solution that assumes no duplicate pairings that I hope helps. Of course it would also work with XSLT 2.0.

It relies on the XPath comparison of two node sets walking through the members of both node sets repeatedly making comparisons: you need only look forward to a pair that doesn't include any player that is in the given pair.

. . . . . . . . Ken

t:\ftemp>type hector.xml
<?xml version="1.0" encoding="UTF-8"?>
<pairings>
<pair>
      <player>player1</player>
      <player>player2</player>
</pair>
<pair>
      <player>player1</player>
      <player>player3</player>
</pair>
<pair>
      <player>player1</player>
      <player>player4</player>
</pair>
<pair>
      <player>player2</player>
      <player>player3</player>
</pair>
<pair>
      <player>player2</player>
      <player>player4</player>
</pair>
<pair>
      <player>player3</player>
      <player>player4</player>
</pair>
</pairings>

t:\ftemp>call xslt hector.xml hector.xsl
<?xml version="1.0" encoding="utf-8"?><table><pair>
      <player>player1</player>
      <player>player2</player>
</pair><pair>
      <player>player3</player>
      <player>player4</player>
</pair></table><table><pair>
      <player>player1</player>
      <player>player3</player>
</pair><pair>
      <player>player2</player>
      <player>player4</player>
</pair></table><table><pair>
      <player>player1</player>
      <player>player4</player>
</pair><pair>
      <player>player2</player>
      <player>player3</player>
</pair></table>
t:\ftemp>type hector.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  version="1.0">
<xsl:template match="/">
  <xsl:for-each select="/*/pair">
    <xsl:if test="following-sibling::pair[not(player=current()/player)]">
      <table>
        <xsl:copy-of select="."/>
        <xsl:copy-of
          select="following-sibling::pair[not(player=current()/player)]"/>
      </table>
    </xsl:if>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>
t:\ftemp>rem Done!


--
Contact us for world-wide XML consulting & instructor-led training
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
G. Ken Holman                 mailto:gkholman(_at_)CraneSoftwrights(_dot_)com
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal


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