[Home] [By Thread] [By Date] [Recent Entries]
At 2011-08-15 15:38 +0200, graham.heath wrote:
I have an xml grouping problem that has so far defeated me. 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!
|

Cart



