XSL如何使用id必须是数字的XSL为XML文档中的每个元素生成惟一的id属性?下面的XLS可以工作,除了生成的I是字母数字的,而我需要数字?
<?xml version='1.0' encoding='utf-8'?>
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
xmlns:msxsl='urn:schemas-microsoft-com:xslt' exclude-result-prefixes='msxsl'>
<xsl:output method='xml' indent='yes'/>
<xsl:template match='*'>
<xsl:copy>
<xsl:attribute name='ElementID'>
<xsl:value-of select='generate-id()'/>
</xsl:attribute>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet> 谢谢。
发布于 2010-04-22 21:17:50
你可以随时使用
concat(count(ancestor::node()),
'00000000',
count(preceding::node()))Michael Kay等见多识广的人警告说, <xsl:number/> 不是有效的(有时是O(N^2)),如果可能,应该避免使用。
发布于 2010-04-22 02:17:20
在level和count中使用number()进行切换似乎已经完成了这一任务。
谢谢
<?xml version='1.0' encoding='utf-8'?>
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
xmlns:msxsl='urn:schemas-microsoft-com:xslt' exclude-result-prefixes='msxsl'>
<xsl:output method='xml' indent='yes'/>
<xsl:template match='*'>
<xsl:copy>
<xsl:attribute name='ElementID'>
<xsl:number level='any' count='*' />
</xsl:attribute>
<xsl:copy-of select="@*"/><!--copy of existing all attributes-->
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>https://stackoverflow.com/questions/2685250
复制相似问题