如何使用XSLT跳过列表中的第一种元素?

问题描述:

我有笔记列表,像这样:如何使用XSLT跳过列表中的第一种元素?

<Notes> 
    <Note> 
     <Type>Internal</Type> 
     <Value>STuff</Value> 
    </Note> 
    <Note> 
     <Type>External</Type> 
     <Value>Other stuff</Value> 
    </Note> 
    <Note> 
     <Type>External</Type> 
     <Value>Even More stuff</Value> 
    </Note> 
</Notes> 

我需要列出外部音符,但跳过第一外部注释。更糟糕的是,我不能总是保持内部音符的存在,所以我不一定知道第一个外部音符的位置。所以我想我需要找到第一个外部音符的位置,并将其存储在一个变量中,然后在测试中使用它。但不知道如何用变量来做到这一点?

所以我想我需要找到第一外部音符的位置 ,并存储 在一个变量,然后使用在 测试。

不,您可以使用position()。如何:

<xsl:template match="Notes"> 
    <xsl:apply-templates select="Note[Type = 'External'][position() &gt; 1]" /> 
</xsl:template> 

<xsl:template match="Note[Type = 'External']"> 
    <!-- now do something with that node --> 
    <xsl:copy-of select="." /> 
</xsl:template> 
+0

我不知道你可以这样做......谢谢! – CodeRedick 2011-03-17 19:00:21

+0

这将是推式。 – 2011-03-17 19:02:55

+0

有点冗长,因为一旦你选择了节点,其他节点将不会被应用。所以,不需要空的规则,并且可以减少另一个规则中的模式。 – 2011-03-17 19:10:03

选择包含External型第二<Note>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="Notes/Note[Type='External'][position()&gt;1]"> 
     <xsl:apply-templates select="Value"/> 
    </xsl:template> 
    <!-- suppress --> 
    <xsl:template match="Note"/> 
</xsl:stylesheet> 

,它输出时应用到你的示例XML如下:

Even More stuff 
+1

这将是拉风格,但是缺少其他'Note'的空规则......并且,不需要在属性值中编码'>'字符。 – 2011-03-17 19:04:03