xsl-list
[Top] [All Lists]

[xsl] Trying to extract a subset of elements but only text nodes are appearing in the output.

2006-10-16 22:08:51
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>
roducts/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



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