2016-04-15 40 views
0

我有XML像下面,如何在通過每個xsl迭代時識別新項目?

<employees> 
<employee> 
    <id>1</id> 
    <name>AAA</name> 
</employee> 
<employee> 
    <id>1</id> 
    <name>AAA</name> 
</employee> 
<employee> 
    <id>2</id> 
    <name>AAA</name> 
</employee> 
<employee> 
    <id>2</id> 
    <name>AAA</name> 
</employee> 
</employees> 

我迭代通過XSL上述XML:的foreach。我有一個要求是顯示id'1'的記錄後,我必須保留一個空行。在XSL中,我們不能存儲前一個項目的id值與當前元素進行比較。那麼我們如何才能用新ID識別該項目? 如何通過使用來實現這一點?

輸入代碼在這裏

感謝, Santhosh。

+0

*「我正在通過xsl:foreach。」*迭代上述XML。發佈您的XSL的相關部分和您期望的輸出 – har07

回答

0

問:我有一個要求是顯示id'1'的記錄後,我必須保留一個空行。
您可以使用之前的兄弟姐妹::來檢查「上一個項目」。

<xsl:variable name="this" select="." /> 
    <xsl:if test="$this/id != preceding-sibling::employee[1]/id" > 
     <xsl:text>&#10;</xsl:text> 
    </xsl:if> 

我不會在這裏使用for-each(但allo possible)可以考慮使用模板。嘗試這樣的:

<xsl:template match="employee"> 
     <xsl:variable name="this" select="." /> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()" /> 
     </xsl:copy> 
     <xsl:if test="$this/id != preceding-sibling::employee[1]/id" > 
      <xsl:text>&#10; --------------;</xsl:text> 
     </xsl:if> 
    </xsl:template> 
相關問題