2013-03-03 77 views
-3

我需要修改XSL文件,使得促销中的CD的价格以红色显示。xml和xslt错误

,这里是我的xml

<?xml version="1.0"?> 
<?xml-stylesheet type="text/xsl" href="catalog.xsl"?> 
<catalog> 
    <cd promotion="Yes"> 
     <title>Empire Burlesque</title> 
     <artist>Bob Dylan</artist> 
     <country>USA</country> 
     <company>Columbia</company> 
     <price>110</price> 
     <year>1985</year> 
    </cd> 
    <cd promotion="No"> 
     <title>Hide your heart</title> 
     <artist>Bonnie Tyler</artist> 
     <country>UK</country> 
     <company>CBS Records</company> 
     <price>99</price> 
     <year>1988</year> 
    </cd> 
    <cd promotion="Yes"> 
     <title>Greatest Hits</title> 
     <artist>Dolly Parton</artist> 
     <country>USA</country> 
     <company>RCA</company> 
     <price>75</price> 
     <year>1982</year> 
    </cd> 
</catalog> 

这里是我的XSL文件。我试图使用选择和时间,但它不工作。我已评论这部分。

<?xml version="1.0"?> 
<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> 
     <th>Year</th> 
     </tr> 
     <xsl:for-each select="catalog/cd"> 
     <tr> 
      <td><xsl:value-of select="title"/></td> 
      <td><xsl:value-of select="artist"/></td> 
      <td><xsl:value-of select="price"/></td> 
      <!--<xsl:choose> 
       <xsl:when> 
        <td bgcolor="#ff00ff"><xsl:value-of select="price"/></td> 
        <xsl:otherwise> 
          <td><xsl:value-of select="price"/></td> 
        </xsl:otherwise> 
       </xsl:when> 
      </xsl:choose>!--> 
      <td><xsl:value-of select="year"/></td> 
     </tr> 
     </xsl:for-each> 
    </table> 
    </body> 
    </html> 
</xsl:template> 
</xsl:stylesheet> 
+1

在哪里的问题?我没看到一个。你忘了提供转换的想要的输出。在目前的状态下,这很难被称为问题。请编辑并提供重要的缺失信息。 – 2013-03-03 06:17:12

+1

另外,如果您有关于错误的具体问题(如标题所示),则应该发布错误消息。 – 2013-03-03 07:59:15

回答

0

来解决它的最好方法是使用xsl:apply-templates和使用模板来匹配具有父与属性promotional="Yes"price元素。

只是价格改变td到:

<td><xsl:apply-templates select="price"/></td> 

,并添加这个模板:

<xsl:template match="price[../@promotion='Yes']"> 
    <xsl:attribute name="bgcolor">#ff00ff</xsl:attribute> 
    <xsl:value-of select="."/> 
</xsl:template>