Generate-id

1. generate-id()
2. Unique id values? Across runs and documents
3. Use for generate-id function
4. generate-id accross different xsl files
5. generate-id accross different xsl files

1.

generate-id()

Mike Kay


I have a problem with using generate-id()

apparantly, as I see it in my code, generate-id is dependent to the
context and the current location in the xml document. that is, when I
use generate-id() twice and in the same context it returns the same
value though I need two different values!

however, when I use generate-id(../..) or generate-id(/) it returns
some other values to me!

generate-id() with no parameters is equivalent to generate-id(.) - it returns the identifier of the context node. If you call it twice with the same argument, it is guaranteed to return the same answer.

colin Adams

It uniquely identifies the node passed as it's argument (or the context node, if it has no argument).

2.

Unique id values? Across runs and documents

David Carlisle



> I thought the only requirement was for unique to document id values,
> not across documents or when repeated in same document?

All bets are off for repeated runs but all nodes in a single run must have different ids. It doesn't matter which document the nodes are in,

3.

Use for generate-id function

G. Ken Holman

I've used generate-id() already in two places, and there are probably many more:

(1) - identification of current location in an arbitrary node set (other than the context node list): - consider when the context node list isn't the list in which you wish to find the current node - the following code notes the generated id of the current node, then walks through an arbitrary context node list generated by the for-each, doing work only when at the (persistent (for the running of the script)) node identifier

<xsl:template name="node-count">
  <xsl:variable name="this-id" expr="generate-id(.)"/>
        
  <xsl:for-each select="../node()|../@*"> < 
    <xsl:if test="generate-id(.)=$this-id">
      <xsl:value-of select="position()"/>
    </xsl:if>
  </xsl:for-each>
</xsl:template>

(2) - tables of content: - imagine you have a document with many sections and you want to create a table of contents with each entry pointing to one of the sections - the generate-id() function returns a value that parses as a name token (e.g.: "N1", "N2", "N136", etc.), so I used it as it was given to me - here are some snippets (I don't have the actual code I used at hand):

<xsl:template match="section">
  <a name="{generate-id(.)}">
<xsl:value-of select="title"/>...

<xsl:template match="toc">
  <xsl:for-each select="//section">
    <a href="#{generate-id(.)}">
<xsl:value-of select="title"/>...

4.

generate-id accross different xsl files

Jeni Tennison


> Will generate-id always produce the same id for a node in an xml
> document with different stylesheets?
	

No (or rather, it might do, but it's not guaranteed to).

> If not does anyone know a different way to link views in html of an
> xml document when the tags in the xml document are not marked with
> any id's?

You need to construct an ID that reflects something unique and unchanging about the nodes that provide the information in your source XML. This has to be a valid ID, so it has to start with a letter and not contain any spaces, but you can easily just add a letter to the start and substitute all spaces with hyphens or underscores using translate().

The easiest thing to use is the value of the node itself, one of its children or an attribute, or a combination of several of these concatenated together. The less information you use and the closer it is to the node, the better, as this reduces the processing that's needed to generate the IDs.

If there's no way of generating something unique from the information available around the node, then you can use the fact that it has a unique position within the XML document to give you the ID. Two ones that I might use are the following:

 - element name + count of previous elements with the same nam
e
  <xsl:variable name="name" select="name()" />
  <xsl:value-of select="concat($name, '-', 
		count(preceding::*[name()= $name]) +
		count(ancestor::*[name()= $name]))" />

 - 'node-' + hierarchical number
  <xsl:text />node-<xsl:number count="*" level="multiple" />

These are both pretty inefficient. Depending on the way you're constructing your output you might be able to come up with a way of passing down partial IDs through parameters or something to make it more efficient.

5.

generate-id accross different xsl files

Jeni Tennison



> Will generate-id always produce the same id for a node in an xml
> document with different stylesheets?
	

No (or rather, it might do, but it's not guaranteed to).

> If not does anyone know a different way to link views in html of an
> xml document when the tags in the xml document are not marked with
> any id's?

You need to construct an ID that reflects something unique and unchanging about the nodes that provide the information in your source XML. This has to be a valid ID, so it has to start with a letter and not contain any spaces, but you can easily just add a letter to the start and substitute all spaces with hyphens or underscores using translate().

The easiest thing to use is the value of the node itself, one of its children or an attribute, or a combination of several of these concatenated together. The less information you use and the closer it is to the node, the better, as this reduces the processing that's needed to generate the IDs.

If there's no way of generating something unique from the information available around the node, then you can use the fact that it has a unique position within the XML document to give you the ID. Two ones that I might use are the following:

 - element name + count of previous elements with the same name

  <xsl:variable name="name" select="name()" />
  <xsl:value-of select="concat($name, '-', count(preceding::*[name()= $name]) +
count(ancestor::*[name()= $name]))" />

 - 'node-' + hierarchical number
  <xsl:text />node-<xsl:number count="*" level="multiple" />

These are both pretty inefficient. Depending on the way you're constructing your output you might be able to come up with a way of passing down partial IDs through parameters or something to make it more efficient.