[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Creating new, distinct groups of ranges from an aggregation of individual ranges

Subject: Re: [xsl] Creating new, distinct groups of ranges from an aggregation of individual ranges
From: "Imsieke, Gerrit, le-tex gerrit.imsieke@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 19 Nov 2014 10:27:48 -0000

Hi Heiko & Michael,

On 19.11.2014 02:20, Heiko Niemann kontakt@xxxxxxxxxxxxxxxx wrote:
Hi Gerrit,

since my first name is not very international I listen to a whole range of
names: from Heeeko to Hayoko. :)

Talking about ranges. I had the same question about 219, Michael clarified
and since your code covers it already I would suggest Michael to follow
your approach. They are similar anyhow.

I noticed two things though:

1) parsing the ranges does not cover missing leading zeros (if you e.g.
add 8880889 to a aircraft-range it won't show in the result, 08880889
will); first I also looked at using analyze-string but dropped it so I use
following code now (adjusted to the elements you use):

I emitted an xsl:message that would report any input token that doesnbt match '^\d{4}(\d{4})?$'. It might be a better idea to add terminate="yes" to that message instead of heuristically fixing the data, but Ibm not the one to make a call on this.

2) If you add e.g. 0200 to a list the result will show:

     <range start="200" end="200"/>
     <range start="200" end="200"/>
     <range start="201" end="204"/>

and if you add 0201 instead the result is:

       <range start="201" end="200"/>
       <range start="201" end="201"/>
       <range start="202" end="204"/>

Youbre right, therebs still a bug. The matching pattern in the template with xml:id="A" should be changed from
match="range[1][not(@start = following-sibling::*[1]/@start)]"
match="range[1][not(@start = following-sibling::*[1]/(@start|@end))]"

Letbs see which other bugs are left uncovered by our anecdotal test cases. Ibm waiting for the FAA to knock on my door in order to investigate a future fatal incident caused by flaky XSLT codeb&

Ibm posting the complete revised code below. There are no templates in #default mode any more which should help in integrating the code. Ibve added a debug parameter. Apply aircrafts.xsl with debug=yes to get the intermediate stepsb results in /ranges/debug in the output.




<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  <xsl:output indent="yes"/>

<xsl:param name="debug" select="'yes'"/>

<xsl:template match="ranges" mode="ranges">
<xsl:variable name="pass1" as="element(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"/>
<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[. &lt; current()]"/>
<xsl:variable name="pass2" as="element(pass2)">
<!-- Fill gaps with new ranges (Heikobs test case) -->
<xsl:apply-templates select="$pass1/range" mode="ranges_pass2"/>
<xsl:variable name="pass3" as="element(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="ranges_pass3"/>
<xsl:if test="$debug = 'yes'">
<xsl:sequence select="."/>
<xsl:sequence select="$pass1"/>
<xsl:sequence select="$pass2"/>
<xsl:sequence select="$pass3/*"/>


  <xsl:template match="range" mode="ranges_pass2">
    <xsl:copy-of select="."/>

<xsl:template mode="ranges_pass2"
[following-sibling::range[1]/@start &gt; current()/@end + 1]
[following-sibling::range/@start &lt; current()/@end]">
<xsl:copy-of select="."/>
<range start="{@end + 1}" end="{following-sibling::range[1]/@start - 1}"

  <xsl:template mode="ranges_pass3" xml:id="A"
    match="range[1][not(@start = following-sibling::*[1]/(@start|@end))]">
      <xsl:copy-of select="@start"/>
      <xsl:attribute name="end"
        select="following-sibling::range[@start][1]/@start - 1"/>

  <xsl:template match="range[@end]" mode="ranges_pass3" xml:id="B">
      <xsl:attribute name="start"
                     preceding-sibling::*[@end][1]/@end + 1))"/>
      <xsl:copy-of select="@end"/>




<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

<xsl:import href="ranges.xsl"/>

<xsl:param name="debug" select="'no'"/>

  <xsl:template match="group[aircraft-range]">
    <xsl:variable name="parse" as="element(ranges)">
        <xsl:apply-templates mode="parse"/>
    <xsl:apply-templates select="$parse" mode="ranges"/>

<xsl:template match="aircraft-range" mode="parse">
<xsl:for-each select="tokenize(., '\s+')">
<xsl:analyze-string select="." regex="^(\d{{4}})(\d{{4}})?$">
<range start="{number(regex-group(1))}" end="{number((regex-group(2)[normalize-space()], regex-group(1))[1])}"/>
<xsl:message select="'Could not parse ', ."/>


Current Thread