ID Attributes

1. Generating same IDs across invocations
2. ID attributes
3. Using keys instead of ID values
4. Unique values across multiple documents
5. Mixing generate-id and existing id values


Generating same IDs across invocations

Jeni Tennison

How to do this really depends on whether and in what way your source document is going to change across invocations. A simple approach is to generate an id that uniquely identifies the element within the document by numbering all the elements of a certain type:

<xsl:attribute name="id">
  <xsl:value-of select="name()">-<xsl:number level="any" />

will generate para-1, para-2, para-3 and so on throughout the document. The xsl:number with level="any" numbers instances of the element across the entire document. This is enough if your source document is not going to change at all across invocations.

Otherwise you need something a bit more complex, and that relies on asking the question: what is it that uniquely identifies this element? It could be its content, the combination of values of attributes, its position, or anything. Once you've answered that question, you can use the answer to generate the unique id.


ID attributes

Chris Maden

Q expansion: I have an XML instance that makes use of the ID/IDREF attributes to establish links within the document, If I use "id($targetid)" it does not work.

For this to work, the ID attributes must be declared as such, and the processor must read those declarations. If you're using XT, for example, you need to have the declarations in the DTD.


Using keys instead of ID values

Joerg Pietschmann

The id() function searches the whole tree. But there must exist a DTD for this tree, which declares the attribute ID as ID ( See

If you don't want to have a DTD, you can set up a key

<xsl:key name="nodes" match="node" use="@ID"/>

as top-level.

And later in a template

<xsl:apply-templates select="key('nodes', 'abc')"/>

which applies templates to all <node>'s with a value of ID attribute of 'abc'.


Unique values across multiple documents

Dimitre Novatchev.

> > I want to create an index across multiple XML
> > documents in order to check the name uniqueness of the
> > element "Member".
> >
> I think that to check uniqueness across multiple documents in 1.0, the
> only way is to copy the relevant data into a new temporary document, and
> then use the standard grouping (or deduplicating) techniques on that.

Not really. This can be achieved using the "makeDistinct" template from FXSL without creating a new temporary document.

The solution was first described on this list almost two years ago:


Mixing generate-id and existing id values

David Tolpin

> generate-id() is not guaranteed to generate an identifier unique 
> for the document.
> It is only guaranteed to produce a different string for each node. 
> If another node already
> has the same id as one just generated, there will be 
> duplicate identifiers in the result.

Here is one that will do the job


  <xsl:key name="ids" match="//*" use="@id"/>

  <xsl:template match="*">
      <xsl:if test="not(@id)">
	<xsl:attribute name="id">
	  <xsl:call-template name="generate-unique-id">
	    <xsl:with-param name="prefix" select="generate-id()"/>
      <xsl:apply-templates select="*|@*|text()"/>

  <xsl:template name="generate-unique-id">
    <xsl:param name="prefix"/>
    <xsl:param name="suffix"></xsl:param>
    <xsl:variable name="id" select="concat($prefix,$suffix)"/>

      <xsl:when test="key('ids',$id)">
        <xsl:call-template name="generate-unique-id">
	  <xsl:with-param name="prefix" select="$prefix"/>
	  <xsl:with-param name="suffix" select="concat($suffix,'x')"/>
        <xsl:value-of select="$id"/>

  <xsl:template match="@*|text()">