2013-03-03 72 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>