XSLT - 将递增的 id 属性添加到节点
XSLT - Add incrementing id attribute to the nodes
我有一个这样的xml,
<doc>
<section>1<section>
<section>1<section>
<p>22</p>
<p>22</p>
<p>22</p>
<footer></footer>
<footer></footer>
<footer></footer>
</doc>
我需要做什么 id
向 <footer>
节点添加新属性。
所以输出将是
<doc>
<section>1<section>
<section>1<section>
<p>22</p>
<p>22</p>
<p>22</p>
<footer id="number-1"></footer>
<footer id="number-2"></footer>
<footer id="number-3"></footer>
</doc>
我可以向 <footer
> 节点添加新属性,但我面临的问题是在 XSLT 中添加递增 ID。
<xsl:template match="footer">
<xsl:copy>
<xsl:attribute name="id"><xsl:value-of select="'number-'[position()]"/></xsl:attribute>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
我试过使用 xsl 变量,但由于它不能像其他语言那样改变,所以我做不到。我也尝试使用 position()
函数,但它只给出了当前 node.so 的位置,在这种情况下,id 编号从 6.
开始
你能给我一个解决方案吗?提前致谢
你可以使用
<xsl:attribute name="id">
<xsl:value-of select="'number-'"/>
<xsl:number level="any"/>
</xsl:attribute>
我有一个这样的xml,
<doc>
<section>1<section>
<section>1<section>
<p>22</p>
<p>22</p>
<p>22</p>
<footer></footer>
<footer></footer>
<footer></footer>
</doc>
我需要做什么 id
向 <footer>
节点添加新属性。
所以输出将是
<doc>
<section>1<section>
<section>1<section>
<p>22</p>
<p>22</p>
<p>22</p>
<footer id="number-1"></footer>
<footer id="number-2"></footer>
<footer id="number-3"></footer>
</doc>
我可以向 <footer
> 节点添加新属性,但我面临的问题是在 XSLT 中添加递增 ID。
<xsl:template match="footer">
<xsl:copy>
<xsl:attribute name="id"><xsl:value-of select="'number-'[position()]"/></xsl:attribute>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
我试过使用 xsl 变量,但由于它不能像其他语言那样改变,所以我做不到。我也尝试使用 position()
函数,但它只给出了当前 node.so 的位置,在这种情况下,id 编号从 6.
你能给我一个解决方案吗?提前致谢
你可以使用
<xsl:attribute name="id">
<xsl:value-of select="'number-'"/>
<xsl:number level="any"/>
</xsl:attribute>