问题 “大于”工作,但“小于”不工作?


这有效:

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

7324
2017-10-02 10:36


起源

定义“工作”和“不工作”。怀疑你需要将你的值转换为数字来进行转换。 - Jon Egerton
你尝试过:<xsl:when test =“number(price)&lt; = 1)”>? - Pierre
此外,顶部的示例与您的完整XSL不匹配,后者指的是> 10。 - Jon Egerton
@JonEgerton,不确定你的意思是“指> 10”。你的意思是不编码?现在是,结果是一样的...... - Fabio Milheiro
再一次,定义“不起作用”。这个问题很乱,因为你无法清楚表达你的预期输出。请从示例输入中显示您的预期输出,并注意不要出现任何编辑错误。 - Sean B. Durkin


答案:


您可以下载和安装许多XSLT处理器,例如Saxon 6.5.5 for XSLT 1.0(http://saxon.sourceforge.net/#F6.5.5)或Saxon 9.4(http://saxon.sourceforge.net/ #F9.4HE)适用于XSLT 2.0或类似于AltovaXML(http://www.altova.com/altovaxml.html)适用于XSLT 1.0或2.0。另外一些操作系统附带一个XSLT 1.0处理器(例如我认为LINUX上的xsltproc,带有MSXML的Windows)。我建议你尝试使用其中一个来运行和测试你的XSLT代码。

http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_choose 基本上是坏了,它没有正确使用少于标志 < 因为它基本上从HTML文本区域中读出您输入的代码,然后将其放入另一个,然后将该内容传递给XML解析器,但是忘记正确地逃避 < 签署两次(因为这是必要的 &lt; 作为HTML和XML的转义机制)。


11
2017-10-02 11:08



你是对的。 W3学校坏了。我尝试过Stylus Studio,它创造了奇迹。但现在我在想...我可以只使用浏览器并通过引用样式表来测试渲染。谢谢! - Fabio Milheiro
另请注意M.Kay的回答: stackoverflow.com/questions/12280319 - Sean B. Durkin
如果您打算继续使用w3fools.com网站,您可以使用类似的测试来处理错误... <xsl:when test="price &amp;lt; 10"> 。 - Sean B. Durkin
啊哈哈,我认为这个网站更好。你发现了惊人的发现!编码&符号。聪明的thiking :)我将开始使用手写笔工作室或浏览器。我们永远不知道什么时候会出现像这样愚蠢的东西!谢了哥们 - Fabio Milheiro


我有同样的问题,但不是痛苦的逃避和破坏代码的“漂亮”,它更容易更换 <xsl:when test="$value <= 10"> 同 <xsl:when test="not($value > 10)">


4
2018-04-06 11:33





您的测试数据集似乎都没有任何价格低于 7.20 所以没有数据可以触发案件


-2
2017-10-02 10:40