Re: [xsl] Rewrite xpath to not use variable references

Subject: Re: [xsl] Rewrite xpath to not use variable references
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Thu, 07 Feb 2008 17:08:49 -0500
At 2008-02-07 16:01 -0600, Angela Williams wrote:
Moving on, how do I need to change my xpath to use pure xpath - no
variables? I want to visit each /root/w node and print the correct
/root/a/b/c value.  I am getting the correct output, but have had to
rely on variable references.

The XSLT current() function points to the current node at the start of the XPath expression.


I hope the example below helps.

. . . . . . . . . Ken

t:\ftemp>type angela.xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
    <w x="1" y="AA" z="BB">apple</w>
    <w x="1" y="AA" z="CC">orange</w>

    <a ID="1">
        <b>
            <c d="AA">apple</c>
            <c d="BB">red</c>
            <c d="CC">crisp</c>
            <c d="DD">fruit</c>
        </b>
        <b>
            <c d="AA">orange</c>
            <c d="BB">orange</c>
            <c d="CC">juicy</c>
            <c d="DD">fruit</c>
        </b>
        <b>
            <c d="AA">banana</c>
            <c d="BB">yellow</c>
            <c d="CC">soft</c>
            <c d="DD">fruit</c>
        </b>
    </a>
    <a ID="2">
        <b>
            <c d="AA">potato</c>
            <c d="BB">red</c>
            <c d="CC">crisp</c>
            <c d="DD">tuber</c>
        </b>
    </a>
</root>

t:\ftemp>type angela.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:xs="http://www.w3.org/2001/XMLSchema";
                version="2.0">

<xsl:output method="text"/>

<xsl:key name="fruit" match="/root/a" use="@ID"/>

    <xsl:template match="/">
      <xsl:apply-templates select="*/w"/>
    </xsl:template>

    <xsl:template match="w">
        Value: <xsl:value-of select="key('fruit', @x)/b/
        c[@d = current()/@y and .=current()]/../c[@d eq current()/@z]"/>.
    </xsl:template>

    <xsl:template match="c"/>
</xsl:stylesheet>

t:\ftemp>xslt2 angela.xml angela.xsl con

Value: red.

Value: juicy.

t:\ftemp>

--
World-wide corporate, govt. & user group XML, XSL and UBL training
RSS feeds:     publicly-available developer resources and training
G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal

Current Thread