xsl-list
[Top] [All Lists]

[xsl] Rewrite xpath to not use variable references

2008-02-07 15:01:43
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[(_at_)d = $y and
.=$w]/../c[(_at_)d eq $z]"/>.

    </xsl:template>

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

Thanks!
Angela


--~------------------------------------------------------------------
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>