题
如何获得一个反内部xsl:为每个循环将反映数量的电元件的处理。
例如,我源XML
<books>
<book>
<title>The Unbearable Lightness of Being </title>
</book>
<book>
<title>Narcissus and Goldmund</title>
</book>
<book>
<title>Choke</title>
</book>
</books>
什么我想要得到的是:
<newBooks>
<newBook>
<countNo>1</countNo>
<title>The Unbearable Lightness of Being </title>
</newBook>
<newBook>
<countNo>2</countNo>
<title>Narcissus and Goldmund</title>
</newBook>
<newBook>
<countNo>3</countNo>
<title>Choke</title>
</newBook>
</newBooks>
XSLT修改:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<newBooks>
<xsl:for-each select="books/book">
<newBook>
<countNo>???</countNo>
<title>
<xsl:value-of select="title"/>
</title>
</newBook>
</xsl:for-each>
</newBooks>
</xsl:template>
</xsl:stylesheet>
所以问题是什么放在地方的???.是否有任何标准的关键字或者我只是必须宣布一个变量,并增加它的内部循环?
作为该问题很长时间我可能应该期望一个线或一个词回答:)
解决方案
position()
.E.G.:
<countNo><xsl:value-of select="position()" /></countNo>
其他提示
试着插入 <xsl:number format="1. "/><xsl:value-of select="."/><xsl:text>
在这个地方的???.
注意到"1."-这是数字格式。更多信息: 在这里,
尝试:
<xsl:value-of select="count(preceding-sibling::*) + 1" />
编辑 -有一个脑部冻结那里,位置()更简单!
你也可以有条件声明上现在的位置()它可以真正帮助在许多情况。
为。
<xsl:if test="(position( )) = 1">
//Show header only once
</xsl:if>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<newBooks>
<xsl:for-each select="books/book">
<newBook>
<countNo><xsl:value-of select="position()"/></countNo>
<title>
<xsl:value-of select="title"/>
</title>
</newBook>
</xsl:for-each>
</newBooks>
</xsl:template>
</xsl:stylesheet>
不隶属于 StackOverflow