[Home] [By Thread] [By Date] [Recent Entries]
Hi Heiko (or Hi Ko, as we say here b sorry, I should avoid lame jokes
about names b OTOH, this rule is not written in the posting guidelines,
Tommie!),
That being said, I must admit that I somehow managed to ignore your preceding post when I posted my solution this afternoon. My solution suffered from the same shortcoming as yours. I now inserted a new second pass that will insert more ranges if need be. It feels clumsier and clumsier, but it covers all test cases known so farb& Input: <ranges> <range start="100" end="300" /> <range start="150" end="190" /> <range start="200" end="280" /> </ranges> XSLT: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> <xsl:output indent="yes"/> <xsl:template match="/ranges"> <xsl:variable name="pass1" as="element(pass1)"> <pass1> <!-- eliminates duplicate start/end attributes and creates a new range for each distinct start and end value: --> <xsl:for-each-group select="range/(@start|@end)" group-by="string-join((name(), .), '=')"> <xsl:sort select="." data-type="number"/> <range> <xsl:sequence select="."/> <!-- adds a start attribute to the end attribute iff the start attribute's value is smaller than the end attribute's: --> <xsl:sequence select="../@start[. < current()]"/> </range> </xsl:for-each-group> </pass1> </xsl:variable> <xsl:variable name="pass2" as="element(pass2)"> <pass2> <!-- Fill gaps with new ranges (Heikobs test case) --> <xsl:apply-templates select="$pass1/range" mode="pass2"/> </pass2> </xsl:variable> <xsl:variable name="pass3" as="element(pass3)"> <pass3> <!-- Replaces the existing start attribute with the nearest value available, looking behind (template with xml:id="B" below). Ranges without an end attribute will be eliminated by virtue of the built-in template. Except for the first range (template with xml:id="A" below): If it doesn't have an end attribute, and if its start attribute is not equal to the next range's start attribute, the next suitable end attribute will be added. --> <xsl:apply-templates select="$pass2/range" mode="pass3"/> </pass3> </xsl:variable> <result> <xsl:sequence select="$pass1"/> <xsl:sequence select="$pass2"/> <xsl:sequence select="$pass3"/> </result> </xsl:template> <xsl:template match="range" mode="pass2">
<xsl:copy-of select="."/>
</xsl:template><xsl:template mode="pass2" match="range[@end] [following-sibling::range[1]/@start > current()/@end + 1] [following-sibling::range/@start < current()/@end]"> <xsl:copy-of select="."/> <range start="{@end + 1}" end="{following-sibling::range[1]/@start - 1}"/> </xsl:template> <xsl:template mode="pass3" xml:id="A"
match="range[1][not(@start = following-sibling::*[1]/@start)]">
<xsl:copy>
<xsl:copy-of select="@start"/>
<xsl:attribute name="end"
select="following-sibling::range[@start][1]/@start - 1"/>
</xsl:copy>
</xsl:template> <xsl:template match="range[@end]" mode="pass3" xml:id="B">
<xsl:copy>
<xsl:attribute name="start"
select="max((@start,
preceding-sibling::*[@start][1]/@start,
preceding-sibling::*[@end][1]/@end + 1))"/>
<xsl:copy-of select="@end"/>
</xsl:copy>
</xsl:template></xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?>
<result>
<pass1>
<range start="100"/>
<range start="150"/>
<range end="190" start="150"/>
<range start="200"/>
<range end="280" start="200"/>
<range end="300" start="100"/>
</pass1>
<pass2>
<range start="100"/>
<range start="150"/>
<range end="190" start="150"/>
<range start="191" end="199"/>
<range start="200"/>
<range end="280" start="200"/>
<range end="300" start="100"/>
</pass2>
<pass3>
<range start="100" end="149"/>
<range start="150" end="190"/>
<range start="191" end="199"/>
<range start="200" end="280"/>
<range start="281" end="300"/>
</pass3>
</result>Gerrit On 18.11.2014 19:03, Heiko Niemann kontakt@xxxxxxxxxxxxxxxx wrote: Hi again,
Registergericht / Commercial Register: Amtsgericht Leipzig Registernummer / Registration Number: HRB 24930 GeschC$ftsfC<hrer: Gerrit Imsieke, Svea Jelonek, Thomas Schmidt, Dr. Reinhard VC6ckler
|

Cart



