使用 XSLT 3 和
xsl:iterate
:
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="p">
<xsl:copy>
<xsl:iterate select="x">
<xsl:param name="n" select="()"/>
<xsl:variable name="n" select="if (@n and @n != '') then @n else $n + 1"/>
<xsl:copy>
<xsl:attribute name="n" select="$n"/>
</xsl:copy>
<xsl:next-iteration>
<xsl:with-param name="n" select="$n"/>
</xsl:next-iteration>
</xsl:iterate>
</xsl:copy>
</xsl:template>
在线示例.