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

Trying to extract a subset of elements but only text

Subject: Trying to extract a subset of elements but only text nodes are appearing in the output.
From: Rashmi Rubdi <dev_subscriptions@xxxxxxxxx>
Date: Mon, 16 Oct 2006 22:08:31 -0700 (PDT)
xslt select only text
Hello,

I'm new to XSLT so please bear with me if this question has been
answered previously. I'm not able to figure this one out after several hours
of trying.

I have a set of product nodes with a price and I'm trying to
extract only those product nodes with price within a specified range
(lower_bound and upper_bound)

I would like to see the elements with their
attributes and element values (text nodes). However, only text nodes are
appearing. 

Following is a sample of the xml and the two approaches in xslt
that I tried.

<?xml version="1.0" encoding="UTF-8"?>
<products>
    <product>
<id>abcd</id>
        <price>500</price>
    </product>
    <product>
<id>1234</id>
        <price>1000</price>
    </product>
    <product>
<id>xyz</id>
        <price>2000</price>
    </product>
    <product>
<id>pqr</id>
        <price>3000</price>
    </product>
</products>

In my
first approach I tried to get all product elements with price within the given
lower and upper bounds and then tried to print the exact copy of the price
element and it's children as follows:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml"
version="1.0" omit-xml-declaration="no"/>
    <xsl:param name="lower_bound"
select="0"/>
    <xsl:param name="upper_price_range" select="2000"/>
<xsl:template match="/">
        <products>
            <xsl:apply-templates
select="products/product[price &gt;= $lower_bound and price &lt;=
$upper_price_range]"/>
        </products>
    </xsl:template>
<xsl:template match="products/product[price &gt;= $lower_bound and price &lt;=
$upper_price_range]">
        <xsl:copy-of select="."/>
    </xsl:template>
</xsl:stylesheet>

In my second approach I tried the identity template and
then tried to suppress all nodes that are outside the price range.
<xsl:template match="@*|node()">
        <xsl:copy>
<xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
</xsl:template>
    <xsl:template
match="products/product[sellers/seller[1]/price &lt; $lower_bound and
sellers/seller[1]/price &gt;= $upper_price_range]"/>

Both of these approaches
result in only text nodes as in abcd 500 1234 1000 xyz 2000

I was hoping to
get this output:
<?xml version="1.0" encoding="UTF-8"?>
<products>
<product>
        <id>abcd</id>
        <price>500</price>
    </product>
<product>
        <id>1234</id>
        <price>1000</price>
    </product>
<product>
        <id>xyz</id>
        <price>2000</price>
    </product>
</products>

My environment setup: Performing transformation inside a JSP page
with JSTL XML Tags which uses Xalan http://xml.apache.org/xalan-j/

Any help
is appreciated.

-Thank you
Rashmi

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.