2017-04-12 46 views
0

我试图做的是改变背景颜色快速XSL文件,如果categorie将包含一个特定的词,但我不知道为什么不工作:(XSL:IF颜色没有更新XSLT

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="catalogo1.xsl"?> 

<tienda> 
<articulos> 
    <articulo> 
     <referencia ID="1">8354268</referencia> 
     <descripcion>Zapatillas de montaña Arpenaz 50 Negro</descripcion> 
     <departamento>Hombre</departamento> 
     <Categoria >Montaña</Categoria> 
     <colores>Negro Carbon/Gris Roca</colores> 
     <tallas>De 39 a 48</tallas> 
     <precio>Antes 19,99€ ahora 17,99€</precio> 
     <foto>images/8354268.jpg</foto> 
     <url>https://www.decathlon.es/zapatillas-de-montaa-arpenaz-50-negro--id_8354268.html</url> 
    </articulo> 
    <articulo> 
     <referencia ID="2">8383624</referencia> 
     <descripcion>CAMISETA DE DEPORTES DE RAQUETA ASICS SHOCKING NARANJA ASICS</descripcion> 
     <departamento>Hombre</departamento> 
     <Categoria>Fitness</Categoria> 
     <colores>Azul, verde, naranja</colores> 
     <tallas>S, M, L, XL</tallas> 
     <precio>34,99€</precio> 
     <foto>images/8383624.jpg</foto> 
     <url>https://www.decathlon.es/camiseta-shocking-naranja-id_8383624.html</url> 
    </articulo> 
    <articulo> 
     <referencia ID="3">8222527</referencia> 
     <descripcion>CHAQUETA SOFTSHELL SIBIR 500 CAMOFLUO SOLOGNAC</descripcion> 
     <departamento>Hombre</departamento> 
     <Categoria>Montaña</Categoria> 
     <colores>Estampado Naranja</colores> 
     <tallas>S, M, L, XL</tallas> 
     <precio>34,99€</precio> 
     <foto>images/8222527.jpg</foto> 
     <url>https://www.decathlon.es/polar-de-caza-de-hombre-softshell-sibir-500-id_8222527.html</url> 
    </articulo> 
</articulos> 

,这将是XSL文件:

<?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> 
    <head></head> 


    <body>          
    <h1>Test</h1> 

    <table border="1">         
     <tr bgcolor="green">         
      <th>Reference</th> 
      <th>Description</th> 
      <th>Departament</th> 
      <th>Categorie</th> 
     </tr>        
    <xsl:for-each select="tienda/articulos/articulo"> 

     <!--- Error 
     <xsl:if test="Categoria = Montaña"> 
     <td bgcolor="#00FF00"> 
     Error --> 

     <xsl:sort select="departamento"/> 
     <tr>         
      <td> 
       <xsl:value-of select="referencia"/> 
      </td> 
      <td> 
       <xsl:value-of select="descripcion"/> 
      </td> 
      <td> 
       <xsl:value-of select="departamento"/> 
      </td> 
      <td> 
       <xsl:value-of select="Categoria"/> 
      </td>    
     </tr> 

     <!--- Error 
     </td> 
     </xsl:if> 
     Error --> 

    </xsl:for-each> 
    </table>         

    </body> 
</html> 

有没有人知道我为什么得到这个代码并且出错,而不是改变td的颜色?

enter image description here

提前许多感谢您的帮助

+0

你究竟想在这里完成什么?抛开错误,将整行放在'td'元素中是没有意义的。 –

+0

嗨@ michael.hor257k我试图做的是在字段类别中包含单词“Montaña”的行变成绿色?在这个XML文件中有三行,我希望1º和3ª行从白色变为绿色:) –

回答

0

如果我猜测正确,你想做的事:

XSLT如果1.0

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

<xsl:template match="/"> 
    <html> 
     <head/> 
     <body>         
      <h1>Test</h1> 
      <table border="1">         
       <tr bgcolor="green">         
        <th>Reference</th> 
        <th>Description</th> 
        <th>Departament</th> 
        <th>Categorie</th> 
       </tr>        
       <xsl:for-each select="tienda/articulos/articulo"> 
        <xsl:sort select="departamento"/> 
        <tr>  
         <xsl:if test="Categoria='Montaña'"> 
          <xsl:attribute name="bgcolor">#00FF00</xsl:attribute> 
         </xsl:if> 
         <td> 
          <xsl:value-of select="referencia"/> 
         </td> 
         <td> 
          <xsl:value-of select="descripcion"/> 
         </td> 
         <td> 
          <xsl:value-of select="departamento"/> 
         </td> 
         <td> 
          <xsl:value-of select="Categoria"/> 
         </td>    
        </tr> 
       </xsl:for-each> 
      </table>         
     </body> 
    </html> 
</xsl:template> 

</xsl:stylesheet> 

该测试Categoria的值是字符串"Montaña"(注意添加的单引号),并在测试返回true时将bgcolor属性添加到整行。


有谁知道为什么我得到和错误与此代码,而不是改变 TD的颜色?

因为xsl:sort必须是其父元素的第一个子元素。这并不是说如果你改变指令的顺序,它就会起作用。正如我在评论中所说,尝试在td元素中包装整行是没有意义的。只有当条件为真时,它也会输出行。