大于和小于xsl中的条件

时间:2019-06-25 03:42:16

标签: xslt

我希望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> 

我尝试了

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

但是不起作用。

预期结果:显示价格在9到10之间的记录。 实际结果:无显示

2 个答案:

答案 0 :(得分:1)

我添加了xsl命名空间,并且可以正常工作。

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

输出。

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

答案 1 :(得分:0)

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>

输出:-

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