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

Subject: Re: [xsl] 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 23:07:14 -0700 (PDT)
Just wanted to let everyone know that this problem is fixed.  

The XSLT
approaches mentioned below worked as intended, but I was getting confused by
the
output of a JSTL tag <x:out select="$xml2"/> which was only displaying the
text nodes and not the elements themselves. 

But after using other JSTL XML
tags to work with the variable xml2 (transformed xml) in my JSP page, the
other tags were able to properly parse the
transformed xml which means that
the first transform resulted in elements.  

<c:import url="/products.xml"
var="xml"/>
<c:import url="/filter_by_price.xsl" var="xsl_filter"/>
<x:transform doc="${xml}" xslt="${xsl_filter}" var="xml2">

<c:import
url="/advanced_list_items.xsl" var="xslt"/>
<x:transform doc="${xml2}"
xslt="${xslt}"/>

Sorry about this.

Rashmi
----- Original Message ----
From:
Rashmi Rubdi <dev_subscriptions@xxxxxxxxx>
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Sent: Tuesday, October 17, 2006 1:08:31 AM
Subject: [xsl] Trying to extract a
subset of elements but only text nodes are appearing in the output.


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