xsl-list
[Top] [All Lists]

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

2008-02-07 15:09:48
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[(_at_)d = current()/@y and .=current()]/../c[(_at_)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(_at_)CraneSoftwrights(_dot_)com
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


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>