使用xslt进行整数比较操作

时间:2014-04-16 16:45:01

标签: xml xslt

我有这个xml文件:

<?xml version="1.0" ?>
<!--?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?-->

<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>The very best of</TITLE>
    <ARTIST>Cat Stevens</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Island</COMPANY>
    <PRICE>11.90</PRICE>
    <YEAR>1994</YEAR>
  </CD>-----so on

我正在尝试使用另一个类似

的xml搜索此xml doc
<search_conditions>
    <condition>
        <price min="20" max="45"/>
    </condition>
</search_conditions>

这是我写的代码:

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

    <xsl:template match="/">
        <xsl:call-template name="query1"/>
    </xsl:template>

    <xsl:template name="query1">
        <xsl:variable name="minPrice" select="document('search.xml')/search_conditions/condition/price/@min" />
        <xsl:variable name="maxPrice" select="document('search.xml')/search_conditions/condition/price/@max" />

        <table align="center" border="1">
            <tr>
                <th>Title</th>
                <th>Artist</th>
                <th>Company</th>
                <th>Year</th>
            </tr>

            <xsl:for-each select="//CD">

            <xsl:if test="PRICE &gt; $minPrice" >
                <xsl:if test="PRICE &lt; $maxPrice" >
                <tr>
                    <td>
                        <xsl:value-of select="TITLE" />
                    </td>
                    <td>
                        <xsl:value-of select="ARTIST" />
                    </td>
                    <td>
                        <xsl:value-of select="COMPANY" />
                    </td>
                    <td>
                        <xsl:value-of select="YEAR" />
                    </td>
                </tr>
                </xsl:if>
            </xsl:if>
        </xsl:for-each>
    </table>
    </xsl:template>

但是当我试图运行这个东西时,什么都没有打印出来。为什么?我在这做错了什么?有人可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

要使用的表达式是:

<xsl:variable name="minPrice" select="document('search.xml')/search_conditions/condition/price/@min" />
<xsl:variable name="maxPrice"  select="document('search.xml')/search_conditions/condition/price/@max" />