2016-04-15 95 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> 
相关问题