Subject: RE: merging sequences
From: Fabien Tillier <ftillier@xxxxxxxx>
Date: Mon, 16 Jan 2012 08:47:35 +0000
|
Hi Ken, Andrew and David.
First of all, thank you very much for these smart answers.
I think I will learn much from them.
Regarding my specific needs, Ken's solution fits better since the order is
exactly as expected
N112,N100,N107,P2010,N109,P2014,P2015,N108,N203,N306,N206,N307,N311
When David's output is
N112 N100 N107 N306 P2010 N109 P2014 P2015 N108 N203 N206 N307 N311
I will now put it in a real life conditions and see if with other inputs it
still performs well.
Thanks again to you all since it was really (at least for me) a tough problem
:)
Kind regards,
Fabien
-----Message d'origine-----
De : G. Ken Holman [mailto:gkholman@xxxxxxxxxxxxxxxxxxxx]
Envoyi : vendredi 13 janvier 2012 17:28
@ : xsl-list@xxxxxxxxxxxxxxxxxxxxxx; xsl-list@xxxxxxxxxxxxxxxxxxxxxx;
xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Objet : Re: merging sequences
At 2012-01-13 10:48 -0500, I wrote:
>At 2012-01-13 14:41 +0000, Fabien Tillier wrote:
>>I have the two following sequences
>>N112 N100 N107 P2010 N109 P2014 P2015 N108 N203 N206 N307 N311
>>And
>>N112 N100 P2014 P2015 N108 N203 N306 N206 N307 N311
>>...
>>I would like to align these so that the resulting sequence contains
>>all items, but keeping the order, thus
>>...
>>N112 N100 N107 P2010 N109 P2014 P2015 N108 N203 N306 N206 N307 N311
>
>I hope the example below helps. It bases a sort on expressing each
>item's location in its own sequence and the other sequence.
I didn't give you the result as a variable, only as output. The
example below gives you a variable of strings you can then work with.
I hope this helps.
. . . . . . 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"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
version="2.0">
<xsl:output method="text"/>
<xsl:template match="/">
<!--input sequences-->
<xsl:variable name="seq1" select="tokenize('N112 N100 N107 P2010
N109 P2014 P2
015 N108 N203 N206 N307 N311','\s+')"/>
<xsl:variable name="seq2" select="tokenize('N112 N100 P2014 P2015
N108 N203 N3
06 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>
<xsl:variable name="seq" as="xsd:string*">
<!--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:sequence select="."/>
</xsl:for-each>
</xsl:variable>
<!--reveal inputs and outputs-->
<xsl:value-of select="$seq1" separator=","/><xsl:text>
</xsl:text>
<xsl:value-of select="$seq2" separator=","/><xsl:text>
</xsl:text>
<xsl:value-of select="$seq" separator=","/><xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
T:\ftemp>
--
Contact us for world-wide XML consulting and instructor-led training
Free 5-hour video lecture: XSLT/XPath 1.0 & 2.0 http://ude.my/uoui9h
Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/
G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Google+ profile: https://plus.google.com/116832879756988317389/about
Legal business disclaimers: http://www.CraneSoftwrights.com/legal
|