“greater than”可以使用,但“less than”不能使用?

15

这个可以运行:

<xsl:when test="(price &gt;= 10)">

但是这样做不会执行测试中的代码,也不会为任何行渲染以下列:

<xsl:when test="(price &lt;= 9)">

我在这里测试XML和XSL: http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_choose 为什么要测试呢?
完整的XSL在这里:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<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">
    <tr bgcolor="#9acd32">
      <th>Title</th>
      <th>Artist</th>
      <th>Price</th>
    </tr>
    <xsl:for-each select="catalog/cd">
    <tr>
      <td><xsl:value-of select="title"/></td>
      <xsl:choose>
      <xsl:when test="(price &lt; 9)">
         <td bgcolor="#00aaff">
           <xsl:value-of select="artist"/>
         </td>
      </xsl:when>
      <xsl:otherwise>
         <td><xsl:value-of select="artist"/></td>
      </xsl:otherwise>
      </xsl:choose>
      <td><xsl:value-of select="price"/></td>
      </tr>

    </xsl:for-each>
  </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

以下是XML源代码:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<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>
    <cd>
        <title>Greatest Hits</title>
        <artist>Dolly Parton</artist>
        <country>USA</country>
        <company>RCA</company>
        <price>9.90</price>
        <year>1982</year>
    </cd>
    <cd>
        <title>Still got the blues</title>
        <artist>Gary Moore</artist>
        <country>UK</country>
        <company>Virgin records</company>
        <price>10.20</price>
        <year>1990</year>
    </cd>
    <cd>
        <title>Eros</title>
        <artist>Eros Ramazzotti</artist>
        <country>EU</country>
        <company>BMG</company>
        <price>9.90</price>
        <year>1997</year>
    </cd>
    <cd>
        <title>One night only</title>
        <artist>Bee Gees</artist>
        <country>UK</country>
        <company>Polydor</company>
        <price>10.90</price>
        <year>1998</year>
    </cd>
    <cd>
        <title>Sylvias Mother</title>
        <artist>Dr.Hook</artist>
        <country>UK</country>
        <company>CBS</company>
        <price>8.10</price>
        <year>1973</year>
    </cd>
    <cd>
        <title>Maggie May</title>
        <artist>Rod Stewart</artist>
        <country>UK</country>
        <company>Pickwick</company>
        <price>8.50</price>
        <year>1990</year>
    </cd>
    <cd>
        <title>Romanza</title>
        <artist>Andrea Bocelli</artist>
        <country>EU</country>
        <company>Polydor</company>
        <price>10.80</price>
        <year>1996</year>
    </cd>
    <cd>
        <title>When a man loves a woman</title>
        <artist>Percy Sledge</artist>
        <country>USA</country>
        <company>Atlantic</company>
        <price>8.70</price>
        <year>1987</year>
    </cd>
    <cd>
        <title>Black angel</title>
        <artist>Savage Rose</artist>
        <country>EU</country>
        <company>Mega</company>
        <price>10.90</price>
        <year>1995</year>
    </cd>
    <cd>
        <title>1999 Grammy Nominees</title>
        <artist>Many</artist>
        <country>USA</country>
        <company>Grammy</company>
        <price>10.20</price>
        <year>1999</year>
    </cd>
    <cd>
        <title>For the good times</title>
        <artist>Kenny Rogers</artist>
        <country>UK</country>
        <company>Mucik Master</company>
        <price>8.70</price>
        <year>1995</year>
    </cd>
    <cd>
        <title>Big Willie style</title>
        <artist>Will Smith</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>9.90</price>
        <year>1997</year>
    </cd>
    <cd>
        <title>Tupelo Honey</title>
        <artist>Van Morrison</artist>
        <country>UK</country>
        <company>Polydor</company>
        <price>8.20</price>
        <year>1971</year>
    </cd>
    <cd>
        <title>Soulsville</title>
        <artist>Jorn Hoel</artist>
        <country>Norway</country>
        <company>WEA</company>
        <price>7.90</price>
        <year>1996</year>
    </cd>
    <cd>
        <title>The very best of</title>
        <artist>Cat Stevens</artist>
        <country>UK</country>
        <company>Island</company>
        <price>8.90</price>
        <year>1990</year>
    </cd>
    <cd>
        <title>Stop</title>
        <artist>Sam Brown</artist>
        <country>UK</country>
        <company>A and M</company>
        <price>8.90</price>
        <year>1988</year>
    </cd>
    <cd>
        <title>Bridge of Spies</title>
        <artist>T`Pau</artist>
        <country>UK</country>
        <company>Siren</company>
        <price>7.90</price>
        <year>1987</year>
    </cd>
    <cd>
        <title>Private Dancer</title>
        <artist>Tina Turner</artist>
        <country>UK</country>
        <company>Capitol</company>
        <price>8.90</price>
        <year>1983</year>
    </cd>
    <cd>
        <title>Midt om natten</title>
        <artist>Kim Larsen</artist>
        <country>EU</country>
        <company>Medley</company>
        <price>7.80</price>
        <year>1983</year>
    </cd>
    <cd>
        <title>Pavarotti Gala Concert</title>
        <artist>Luciano Pavarotti</artist>
        <country>UK</country>
        <company>DECCA</company>
        <price>9.90</price>
        <year>1991</year>
    </cd>
    <cd>
        <title>The dock of the bay</title>
        <artist>Otis Redding</artist>
        <country>USA</country>
        <company>Atlantic</company>
        <price>7.90</price>
        <year>1987</year>
    </cd>
    <cd>
        <title>Picture book</title>
        <artist>Simply Red</artist>
        <country>EU</country>
        <company>Elektra</company>
        <price>7.20</price>
        <year>1985</year>
    </cd>
    <cd>
        <title>Red</title>
        <artist>The Communards</artist>
        <country>UK</country>
        <company>London</company>
        <price>7.80</price>
        <year>1987</year>
    </cd>
    <cd>
        <title>Unchain my heart</title>
        <artist>Joe Cocker</artist>
        <country>USA</country>
        <company>EMI</company>
        <price>8.20</price>
        <year>1987</year>
    </cd>
</catalog>

1
定义“有效”和“无效”。但是,您可能需要将值转换为数字才能进行转换。 - Jon Egerton
2
你尝试过这个吗:<xsl:when test="number(price) <= 1)">? - Pierre
此外,顶部的示例与您的完整XSL不匹配,后者引用了大于10的内容。 - Jon Egerton
1
@JonEgerton,不确定您所说的“refers to > 10”是什么意思。您是指未编码吗?现在已经编码了,结果是一样的... - Fabio Milheiro
1
再次定义“不起作用”的含义。这个问题很混乱,因为您无法表达出期望的输出结果。请展示您从样例输入中期望得到的输出,并注意不要犯任何编辑错误。 - Sean B. Durkin
显示剩余5条评论
4个回答

14

有许多可以下载和安装的XSLT处理器,例如用于XSLT 1.0的Saxon 6.5.5(http://saxon.sourceforge.net/#F6.5.5)或用于XSLT 2.0的Saxon 9.4(http://saxon.sourceforge.net/#F9.4HE),或者像AltovaXML(http://www.altova.com/altovaxml.html)这样的处理器可用于XSLT 1.0或2.0。此外,一些操作系统内置了XSLT 1.0处理器(例如LINUX上的xsltproc,Windows上的MSXML)。建议您尝试使用其中一个来运行和测试您的XSLT代码。

http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_choose基本上是有问题的,它不能正确地处理小于符号<,因为它基本上是从HTML文本区域中读取您输入的代码,然后将其放入另一个文本区域中,再传递该内容给XML解析器,但是忘记了正确地转义两次<符号(因为&lt;是HTML和XML的转义机制)。


2
你说得对。W3学校网站出了问题。我试了Stylus Studio,效果很好。但现在我在想……我本可以只用浏览器,通过引用样式表来测试渲染效果。谢谢! - Fabio Milheiro
4
如果您打算继续使用w3fools.com网站,您可以通过以下测试解决该错误... <xsl:when test="price &amp;lt; 10"> - Sean B. Durkin
1
啊哈哈,是的,我认为这个网站更好。你发现了一个很棒的东西!对 & 进行编码,真是聪明的想法 :) 我会开始使用 Stylus Studio 或者浏览器。我们永远不知道什么时候会出现其他像这样愚蠢的问题!谢谢,伙计。 - Fabio Milheiro

6

我曾经遇到过同样的问题,但是为了不破坏代码的“美观”,我并没有使用繁琐的转义方式。相反,将<xsl:when test="$value <= 10">替换为<xsl:when test="not($value > 10)">更加简单易行。


-1

我不确定有4个人回答了一个关于在xslt xpath表达式中无法使用<的问题,却写了一大堆段落,却没有提到如果你想要使用小于号,你可以简单地输入:

lt

如下所示:

$these-answers-are lt $complete

此问题标记为 xslt-1.0。您的答案需要 XSLT 2.0 或更高版本,并且在 XSLT 1.0 中会产生错误。此外,您关于“如果要使用小于号,则可以简单地键入:lt”的说法是不正确的(或者至少是不完整的),即使在 XSLT 2.0 中也是如此。XPath 2.0 规范定义了两组比较运算符:lt 运算符用于值比较,而 < 运算符仍然作为一般比较运算符。... - michael.hor257k
在XPath 1.0中,只有<运算符 - 在XSLT中必须转义为&lt;。这种形式在问题中显示正确,因此 - 正如几条评论和被接受的答案所指出的那样 - 问题出在其他地方。也许在批评之前你应该更仔细地看一下。 - michael.hor257k
@michael.hor257k,好的,我的批评让你比我在其他任何地方找到的更简明、完整和易懂地解释了它。所以这是值得的。现在我明白了。谢谢。 - toddmo

-2

你的测试数据集似乎没有任何价格低于7.20,因此没有数据触发该情况


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接