Re: [xsl] Predicates vs. Axes

Subject: Re: [xsl] Predicates vs. Axes
From: Peter Davis <pdavis152@xxxxxxxxx>
Date: Mon, 3 Jun 2002 14:20:06 -0700
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Monday 03 June 2002 12:49, Joerg Heinicke wrote:
> use preceding-sibling and following-sibling axes.

and Michael Peet wrote:
> >        <node id="5"/>
> >        <node id="6"/>
> >        <d>
> >          <node id="7"/>
...
> > For example, if node 6 is marked current, 5 and 7 will be 
> > preceding and following, respectively.

Unfortunately, Michael, you will have to revise this requirement if you want 
to use preceding-sibling and following-sibling axes.  It might be that the 
only way to do it is with a recursive template like this:

<xsl:template match="node[current='true']">
  <xsl:call-template name="find-current-preceding"/>
  <xsl:call-template name="find-current-following"/>
</xsl:template>

<xsl:template name="find-current-preceding">
  <xsl:variable name="preceding" select="parent::*[1] | preceding::*[1]"/>
  <xsl:choose>
    <xsl:when test="$preceding[last()]/self::b">
      Found no preceding node.
    </xsl:when>
    <xsl:when test="$preceding[last()]/self::node">
      Found preceding node: <xsl:copy-of select="$preceding[last()]"/>
    </xsl:when>
    <xsl:otherwise>
      <!-- change the context node to 
        $preceding[last()] for the call-template -->
      <xsl:for-each select="$preceding[last()]">
        <xsl:call-template name="find-current-preceding"/>
      </xsl:for-each>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template name="find-current-following">  
  <xsl:choose>
    <xsl:when test="following::*[1]/self::b">
      Found no following node.
    </xsl:when>
    <xsl:when test="following::*[1]/self::node">
      Found following node: <xsl:copy-of select="following::*[1]"/>
    </xsl:when>
    <xsl:otherwise>
      <!-- change the context node to 
        $preceding[last()] for the call-template -->
      <xsl:for-each select="following::*[1]">
        <xsl:call-template name="find-current-following"/>
      </xsl:for-each>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

Note that I haven't tested this at all, so I hope I haven't made some 
fundamental error in logic.

The hard part is with the preceding axis, since it does not include nodes on 
the ancestor axis.  I'm attempting (hopefully successfully, again I haven't 
tested this) to combine the nearest elements on the preceding and parent axes 
and then test the element that comes last in document order.

Once you get the preceding|parent and following elements, just test whether 
the element is a <b> or not.  If it is a <b>, then stop.  If it is a <node>, 
then output success.  If it is anything else (like a <c>, <d>, or <e>), then 
try again.

- -- 
Peter Davis
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE8+92GNSZCJx7tYycRAss+AJ9e43gP2GaAC7uFc7avRgpn+10XjwCgrNcQ
SPmhyyJIjo+78dPiP3pH8GM=
=bp/H
-----END PGP SIGNATURE-----


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


Current Thread