2013-10-09 32 views
1

我有一個XML文件,其中包含具有兩種不同品質的項目列表,我需要創建一個HTML輸出,該輸出列出兩個類別中的項目,其編號順序以兩個項目開頭。我找不到解決方案。下面是我創建至今的文件:<xsl:number>重置編號

XML

<?xml version="1.0" encoding="UTF-8"?> 
<refrigerator> 
<item> 
<quality>Good</quality> 
<item_name>eggs</item_name> 
</item> 
<item> 
<quality>Good</quality> 
<item_name>chess</item_name> 
</item> 
<item> 
<quality>Good</quality> 
<item_name>soda</item_name> 
</item> 
<item> 
<quality>Bad</quality> 
<item_name>chicken meat</item_name> 
</item> 
<item> 
<quality>Bad</quality> 
<item_name>spinach</item_name> 
</item> 
<item> 
<quality>Bad</quality> 
<item_name>potatoes</item_name> 
</item> 
</refrigerator> 

XSL

<table width="100%" border="1"> 
<tr> 
<td> 
<strong>These are the good items in the refrigerator/strong> 
<xsl:for-each select="refrigerator/item"> 
<xsl:if test="quality = 'Good'"> 
<strong><xsl:number format="a) " value="position()"/></strong> 
<xsl:value-of select="item_name"/> 
</xsl:if> 
</xsl:for-each> 

, <strong>and these are the bad ones/strong> 
<xsl:for-each select="refrigerator/item"> 
<xsl:if test="quality = 'Bad'"> 
<strong><xsl:number format="a) " value="position()"/></strong> 
<xsl:value-of select="item_name"/> 
</xsl:if> 
</xsl:for-each> 


. Some more text over here.</td> 
</tr> 
</table> 

HTML

這些都是在冰箱的好項目:一)雞蛋二)棋ç )蘇打水,這些是壞的:d)雞肉e)菠菜f)土豆。一些更多的文字在這裏。

輸出需要

這些都是在冰箱的好項目:一)雞蛋二)國際象棋C)蘇打水,這些都是壞的:1)雞肉B)菠菜C)土豆。一些更多的文字在這裏。

任何幫助,非常感謝它。

問候。

A.

+1

你不相信代碼縮進? – Tomalak

回答

1

或者:使用<xsl:for-each>正確。

<xsl:template match="refrigerator"> 
    <table width="100%" border="1"> 
    <tr> 
     <td> 
     <strong>These are the good items in the refrigerator</strong> 

     <xsl:for-each select="item[quality = 'Good']"> 
      <strong><xsl:number format="a) " value="position()"/></strong> 
      <xsl:value-of select="item_name" /> 
     </xsl:for-each> 

     <xsl:text>, <xsl:text> 
     <strong>and these are the bad ones</strong> 

     <xsl:for-each select="item[quality = 'Bad']"> 
      <strong><xsl:number format="a) " value="position()"/></strong> 
      <xsl:value-of select="item_name" /> 
     </xsl:for-each> 

     <xsl:text>. Some more text over here.</xsl:text> 
     </td> 
    </tr> 
    </table> 
</xsl:template> 

或者說,不要重複自己,不使用<xsl:for-each>在所有。

<xsl:template match="refrigerator"> 
    <table width="100%" border="1"> 
    <tr> 
     <td> 
     <strong>These are the good items in the refrigerator</strong> 

     <xsl:apply-templates select="item[quality = 'Good']" mode="numbered" /> 

     <xsl:text>, <xsl:text> 
     <strong>and these are the bad ones</strong> 

     <xsl:apply-templates select="item[quality = 'Bad']" mode="numbered" /> 

     <xsl:text>. Some more text over here.</xsl:text> 
     </td> 
    </tr> 
    </table> 
</xsl:template> 

<xsl:template match="item" mode="numbered"> 
    <div> 
    <strong><xsl:number format="a) " value="position()"/></strong> 
    <xsl:value-of select="item_name" /> 
    </div> 
</xsl:template> 

或者,,這是更加優選,使用HTML編號列表。輸出<ol><li>,並通過CSS對它們進行樣式設置,而不是輸出中的硬編碼列表編號。

+0

謝謝Tomalak您的回覆。我使用伊恩羅伯茨的選項,它工作正常。我也會嘗試你的。問候。答: – user2863560

1

您的問題是position()對您目前正在使用的節點列表非常敏感。取而代之的

<xsl:for-each select="refrigerator/item"> 
    <xsl:if test="quality = 'Good'"> 

把測試的for-each選擇表達

<xsl:for-each select="refrigerator/item[quality = 'Good']"> 

,同樣爲 「壞」 的情況。

Tomalak suggests可以保存通過將它移動到一個單獨的template並使用apply-templates代替for-each重複在兩種情況下相同的代碼。

+0

謝謝伊恩。它工作正常。問候。一個。 – user2863560