[Home] [By Thread] [By Date] [Recent Entries]
At 2012-01-13 14:41 +0000, Fabien Tillier wrote:
I have the two following sequences I hope the example below helps. It bases a sort on expressing each item's location in its own sequence and the other sequence. . . . . . . . . . Ken T:\ftemp>xslt2 fabien.xsl fabien.xsl N112,N100,N107,P2010,N109,P2014,P2015,N108,N203,N206,N307,N311 N112,N100,P2014,P2015,N108,N203,N306,N206,N307,N311 N112,N100,N107,P2010,N109,P2014,P2015,N108,N203,N306,N206,N307,N311 T:\ftemp>type fabien.xsl <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> <xsl:output method="text"/> <xsl:template match="/"> <!--input sequences--> <xsl:variable name="seq1" select="tokenize('N112 N100 N107 P2010 N109 P2014 P2015 N108 N203 N206 N307 N311','\s+')"/> <xsl:variable name="seq2" select="tokenize('N112 N100 P2014 P2015 N108 N203 N306 N206 N307 N311','\s+')"/> <!--order them with information about their position in other sequence-->
<xsl:variable name="items1">
<xsl:for-each select="$seq1">
<item index="{position()}" other="{index-of($seq2,.)[1]}">
<xsl:value-of select="."/>
</item>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="items2">
<xsl:for-each select="$seq2">
<item index="{position()}" other="{index-of($seq1,.)[1]}">
<xsl:value-of select="."/>
</item>
</xsl:for-each>
</xsl:variable> <!--reveal inputs-->
<xsl:value-of select="$seq1" separator=","/><xsl:text>
</xsl:text>
<xsl:value-of select="$seq2" separator=","/><xsl:text>
</xsl:text>
<!--order the result based on relative positions-->
<xsl:for-each select="$items1/item,$items2/item[@other='']">
<xsl:sort select="(:the relative order is latest of self or other:)
max((number(@index),
if(@other='') (:if no other, then use previous other:)
then preceding-sibling::*[@other!=''][1]/number(@other)
else number(@other)))"/>
<xsl:if test="position()>1">,</xsl:if>
<xsl:value-of select="."/>
</xsl:for-each></xsl:template> </xsl:stylesheet> T:\ftemp>
|

Cart



