2012-03-26 50 views
-3

我正在做一項工作,我應該以不同的方式展示雜貨店。我似乎無法確定xsl文件是錯誤還是xml文件,或者兩者兼而有之。
作業代碼段:
每個 庫存中的每件商品都應具有銷售價格,無論其是否在銷售中。正在銷售的商品將通過銷售屬性標識爲值「是」或「否」。 sale屬性應放置在ID標籤中。如果銷售標籤是「是」,則使用銷售價格,否則使用正常價格。

我的問題是,只有正常價格會顯示無論@銷售= 「是」 或 「否」如何讓我的xsl文件顯示銷售價格?

<body> 
    <table> 
     <xsl:for-each select="catalog/item">    

       <tr> 
        <xsl:attribute name="sale"> 
          <xsl:value-of select="id"/> 
         </xsl:attribute> 
        <td><xsl:value-of select="company"/></td> 
        <td><xsl:value-of select="product"/></td> 
        <td><xsl:value-of select="category"/></td> 
        <td><xsl:value-of select="description"/></td> 
        <xsl:choose> 
         <xsl:when test="@sale = 'yes'"> 
          <td><xsl:value-of select="sale"/></td> 
         </xsl:when> 
         <xsl:otherwise> 
          <td><xsl:value-of select="price"/></td> 
         </xsl:otherwise> 
        </xsl:choose> 
        <td><xsl:value-of select="unit"/></td> 
        <td> 
         <img> 
          <xsl:attribute name="src"> 
           <xsl:value-of select="picture"/> 
          </xsl:attribute> 
        </img> 
        </td> 
       </tr> 

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

,並用2項樣本XML:

<?xml version="1.0" encoding="ISO-8859-1"?> 
<?xml-stylesheet type="text/xsl" href="sale.xsl" xsl:import href="company.xsl" xsl:import href="category.xsl"?> 

<!DOCTYPE catalog SYSTEM "stock.dtd"> 

<catalog> 
<item> 
    <id sale="yes">801</id> 
    <company>Grocery Gateway</company> 
    <product>Organic Strawberries</product> 
    <category>Produce</category>  
    <description>Fresh and organic strawberries imported from U.S.A. This product is subject to availability</description> 
    <price>5.99</price> 
    <sale>4.99</sale> 
    <unit>454g</unit> 
    <picture>pics/M6548[1].jpg</picture> 
</item> 
<item> 
    <id sale="no">101</id> 
    <company>Nestle </company> 
    <product>Pure Life Spring Water</product> 
    <category>Beverages</category> 
    <description>Ingredients are spring water, and ozone. </description>  
    <price>5.99</price> 
    <sale>4.99</sale> 
    <unit>24x500mL</unit> 
    <picture>pics/M58629[1].jpg</picture> 
</item></catalog> 
+2

歡迎來到SO。請閱讀[常見問題]並[請]瞭解您的帖子爲什麼不符合基本準則。本質上,你不能只在這裏轉儲一堆代碼,並期望人們閱讀它併爲你調試它。將其降低到再現問題的最小示例,然後提出具體問題。 – 2012-03-26 05:13:54

回答

0

爲什麼它不能正常顯示銷售價格的原因是因爲你錯誤地引用@sale屬性。

<xsl:when test="@sale = 'yes'"> 

以上試圖尋找一個屬性,叫做對當前sale換每次迭代(在這種情況下是catalog/item,但屬性不會在你的XML此節點上存在)。您需要明確說明您正在尋找id節點,如下所示:

​​
相關問題