Yin How
Yin How

Reputation: 99

Greater than and lesser than condition in xsl

I would like to have greater than and lesser than condition in xsl.

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
    <cd>
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>1985</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>UK</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
</catalog>

xslt:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <html>
            <body>
                <h2>My CD Collection</h2>
                <table border="1">
                    <xsl:for-each select="catalog/cd">
                        <tr>
                            <td>
                                <xsl:value-of select="title"/>
                            </td>
                            <xsl:choose>
                                <xsl:when test="price &gt; '9' and price &lt; '10'">
                                    <td bgcolor="#B22222">
                                        <xsl:value-of select="artist"/>
                                    </td>
                                </xsl:when>
                            </xsl:choose>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet> 

I tried

<xsl:when test="price &gt; 9 and price &lt; 10">.

But is not working.

Expected result: Display records which price is between 9 and 10. Actual result : Nothing display

Upvotes: 0

Views: 514

Answers (2)

Ed Bangga
Ed Bangga

Reputation: 13006

I added the xsl namespace, and its working fine.

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <html>
            <body>
                <h2>My CD Collection</h2>
                <table border="1">
                    <xsl:for-each select="catalog/cd[price &gt; '9' and price &lt; '10']">
                        <tr>
                            <td>
                                <xsl:value-of select="title"/>
                            </td>
                            <td bgcolor="#B22222">
                                <xsl:value-of select="artist"/>
                            </td>
                        </tr>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet> 

Output.

<html>
   <body>
      <h2>My CD Collection</h2>
      <table border="1">
         <tr>
            <td>Hide your heart</td>
            <td bgcolor="#B22222">Bonnie Tyler</td>
         </tr>
      </table>
   </body>
</html>

Upvotes: 1

Ajeet Singh
Ajeet Singh

Reputation: 1076

I think are you want following:-

<xsl:template match="catalog/cd">
<xsl:choose>
<xsl:when test="price &gt; 9 and price &lt; 10">
<xsl:copy-of select="*"/>
</xsl:when>
</xsl:choose>
</xsl:template>

OutPut:-

<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>

Upvotes: 0

Related Questions