Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSL numeric generate-id()

Tags:

xml

xslt

How can XSL generate a unique id attribute for every element in an XML document using XSL where the id must be numeric? The XLS below works except that the generated ids are alphanumeric and I need numeric?

<?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>  

Thank you.

like image 584
gregn Avatar asked Apr 21 '10 17:04

gregn


1 Answers

You can always use:

     concat(count(ancestor::node()),
           '00000000',
           count(preceding::node()))

Knowledgeable people such as Michael Kay warn that <xsl:number/> is not efficient (sometimes O(N^2)) and should be avoided if possible.

like image 157
Dimitre Novatchev Avatar answered Sep 29 '22 02:09

Dimitre Novatchev