[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Rewrite xpath to not use variable references

Subject: Rewrite xpath to not use variable references
From: "Angela Williams" <Angela.Williams@xxxxxxxxxxxxxxxxxx>
Date: Thu, 7 Feb 2008 16:01:01 -0600
 Rewrite xpath to not use variable references
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.