[xsl] Rewrite xpath to not use variable references

Subject: [xsl] Rewrite xpath to not use variable references
From: "Angela Williams" <Angela.Williams@xxxxxxxxxxxxxxxxxx>
Date: Thu, 7 Feb 2008 16:01:01 -0600
Hello again!

With help from the members of this list over the past year, we were able
to put our first release into production. Thank you so much for your
generosity!

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.

Desired output:
Value: red.
Value: juicy.

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>

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="w">
        <xsl:variable name="w" select="." as="xs:string"/>
        <xsl:variable name="y" select="@y" as="xs:string"/>
        <xsl:variable name="z" select="@z" as="xs:string"/>

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

    </xsl:template>

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

Thanks!
Angela

Current Thread