Getting the position of an element in XSLT
我想获取元素在XSLT中的位置。
输入:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <parent_root> <root> <ele> <fig/> </ele> <got/> <ele> <fig/> </ele> <got/> <got/> <ele> <fig/> </ele> <got/> </root> </parent_root> |
输出应为:
1 2 3 4 | fig_1 fig_2 fig_3 fig_4 |
尝试代码:
1 2 3 4 5 6 | <xsl:template match="fig"> <xsl:when test="parent::ele/following-sibling::got"> <xsl:variable name="ID2" select="parent::ele/following-sibling::got/position()"/> <xsl:value-of select="concat('fig_','$ID2')"/> </xsl:when> </xsl:template> |
我得到的错误:
我该如何解决?我正在使用XSLT 2.0。谢谢
1 2 3 | <xsl:template match="fig"> <xsl:value-of select="concat('fig_',count(ancestor::root/preceding-sibling::root)+1,' ')" /> </xsl:template> |
格式正确的输出
1 2 3 | fig_1 fig_2 fig_3 |
EDIT,因为问题已更改:
要获取所有
1 2 3 | <xsl:template match="got"> <xsl:value-of select="concat('fig_',count(preceding::got)+1,' ')" /> </xsl:template> |
与所有
1 2 3 4 | fig_1 fig_2 fig_3 fig_4 |
如果匹配