[xsl] Re: Re: Re: Re: Unbounded element grouping/concatenation

Subject: [xsl] Re: Re: Re: Re: Unbounded element grouping/concatenation
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Sat, 13 Dec 2003 08:47:04 +0100
> Below is the next version, which is speeded up 10-30%. The change is that
if
> you knoe in advance that the maximum of possible immediate continuation
> siblings is a number with N digits, then all position numbers in the
string
> of positions are coded with this fixed length. This allows not to use
> delimiters between every two positions and not to search for a substring.
> Instead, the position is found using direct addressing.

I was contacted by Raman off-list to point to a bug in the current
stylesheets -- they leave out the case when the last normal record has
continuation records. This was fixed by Raman for the transformation, which
constructs a string of positions separated by a delimiter. I fixed the
solution, which constructs a string of positions with a fixed length.

Also, my statement that the second solution speeds up the first with only
10-30% was inaccurate. The increase in speed may be very significant when
there are many normal records each having continuation records and the
string with their positions becomes long.

The current test case has only one such record and this didn't allow to show
the real gain in speed of the second algorithm.

Here are the corrected stylesheets:

I. Using delimiters in the string of positions:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
 >

  <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <xsl:variable name="vPosArray">
    <xsl:value-of select="'|'"/>
    <xsl:for-each select="/*/record">
      <xsl:if test="@type = 'normal'">
        <xsl:value-of select="concat(position(), '|')"/>
      </xsl:if>
    </xsl:for-each>
  </xsl:variable>

  <xsl:template match="@* | node()" name="identity">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="records">
    <records>
      <xsl:apply-templates select="record"/>
    </records>
  </xsl:template>

  <xsl:template match="record">
    <xsl:variable name="vPos" select="position()"/>
    <xsl:variable name="vPosNext" select=
    "substring-before(substring-after($vPosArray,
                                      concat('|',
                                              position(),
                                             '|'
                                             )
                                      ),
                                      '|'
                      )"/>

    <xsl:choose>
      <xsl:when test="not($vPosNext)">
        <xsl:copy>
          <xsl:copy-of select="@* | node()"/>
          <xsl:copy-of select="following-sibling::record"/>
        </xsl:copy>
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="vNumNested"
             select="$vPosNext - position() - 1"/>
        <xsl:copy>
          <xsl:copy-of select="@* | node()"/>
          <xsl:if test="$vNumNested > 0">
            <xsl:copy-of select=
            "following-sibling::record
                               [position() &lt;= $vNumNested]"/>
          </xsl:if>
        </xsl:copy>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="record[not(@type='normal')]"/>

</xsl:stylesheet>


II. Using a fixed length for any position:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
 >

  <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <xsl:param name="pFormatPos" select="string('0001')"/>
  <xsl:variable name="vNumLength"
          select="string-length($pFormatPos)"/>

  <xsl:variable name="vTwiceNumLength"
                select="2 * $vNumLength"/>

  <xsl:variable name="vposArray">
    <xsl:for-each select="/*/record">
      <xsl:if test="@type = 'normal'">
        <xsl:number value="position()" format="{$pFormatPos}"/>
      </xsl:if>
    </xsl:for-each>
  </xsl:variable>



  <xsl:template match="@* | node()" name="identity">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="records">
    <records>
      <xsl:apply-templates select="record[@type='normal']"/>
    </records>
  </xsl:template>

  <xsl:template match="record">
      <xsl:if test="@type='normal'">
        <xsl:variable name="vThisAndNext"
         select="substring($vposArray,
                           (position() - 1)*$vNumLength + 1,
                           $vTwiceNumLength
                           )"/>

         <xsl:variable name="vNextPos"
          select="substring($vThisAndNext, $vNumLength + 1)"/>

         <xsl:variable name="vNumNested"
           select="$vNextPos
                  -
                   substring($vThisAndNext, 1, $vNumLength)
                  - 1"/>
         <xsl:copy>
           <xsl:copy-of select="@* | node()"/>
           <xsl:choose>
             <xsl:when test="$vNumNested > 0">
               <xsl:copy-of select=
                 "following-sibling::record
                             [position() &lt;= $vNumNested]"/>
             </xsl:when>
             <xsl:when test="not($vNextPos)">
               <xsl:copy-of select="following-sibling::record"/>
             </xsl:when>
           </xsl:choose>
         </xsl:copy>
      </xsl:if>
  </xsl:template>

</xsl:stylesheet>



=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL




 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread