XSL如何使用XSL为XML文档中的每个元素生成唯一的id属性,其中id必须是数字?下面的XLS工作,除了生成的ID是字母数字,我需要数字?
<?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>
Run Code Online (Sandbox Code Playgroud)
谢谢.
你可以随时使用:
concat(count(ancestor::node()),
'00000000',
count(preceding::node()))
Run Code Online (Sandbox Code Playgroud)
知识渊博的人,如迈克尔凯警告说,<xsl:number/>效率不高(有时是O(N ^ 2)),如果可能的话应该避免.
使用带有 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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6509 次 |
| 最近记录: |