<html>
<head>
<title>generate-id() Function</title>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div id="Description">
<table cellpadding="0" cellspacing="0" border="0" width="100%" class="main">
<tr>
<td valign="top" class="NAME">generate-id() Function</td>
<td valign="top" class="COMPATIBILITY">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="divider"><img src="dwres:18084" width="100%" height="1"></td>
</tr>
<tr>
<td valign="top" colspan="2" class="description">
Generates a unique ID (an XML name) for a given node. If no node-set is given, <span class="LITERAL">generate-id()</span> generates an ID for the context node.</td></tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Inputs</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>An optional node-set. If no node-set is given, this function generates an ID for the context node. If the node-set is empty, <span class="LITERAL">generate-id()</span> returns an empty string. </p>
</td>
</tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Output</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>A unique ID, or an empty string if an empty node-set is given. Several things about the <span class="LITERAL">generate-id()</span> function are important to know:</p>
<ul>
        <dd>For a given transformation, every time you invoke <span class="LITERAL">generate-id()</span> against a given node, the XSLT processor must return the same ID. The ID can't change while you're doing a transformation. If you ask the XSLT processor to transform your document with this stylesheet tomorrow, there's no guarantee that <span class="LITERAL">generate-id()</span> will generate the same ID the second time around. All of tomorrow's calls to <span class="LITERAL">generate-id()</span> will generate the same ID, but that ID might not be the one generated today.</p>
        </li>
        <dd>The <span class="LITERAL">generate-id()</span> function is not required to check if its generated ID duplicates an ID that's already in the document. In other words, if an element in your document has an attribute of type <span class="LITERAL">ID</span> with the value <span class="LITERAL">sdk3829a</span>, there's a remote possibility that an ID returned by <span class="LITERAL">generate-id()</span> would have the value <span class="LITERAL">sdk3829a</span>. It's not likely, but it could happen.</p>
        </li>
        <dd>If you invoke <span class="LITERAL">generate-id()</span> against two different nodes, the two generated IDs must be different.</p>
        </li>
        <dd>Given a node-set, <span class="LITERAL">generate-id()</span> returns an ID for the node in the node-set that occurs first in document order. </p>
        </li>
        <dd>If the node-set you pass to the function is empty (you invoke <span class="LITERAL">generate-id(fleeber)</span>, but there are no <span class="LITERAL">&lt;fleeber&gt;</span> elements in the current context), <span class="LITERAL">generate-id()</span> returns an empty string.</p>
        </li>
      </ul>
</td>
</tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Defined in</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>XSLT section 12.4, Miscellaneous Additional Functions.</p>
</td>
</tr>
<tr>
<td colspan="2" class="CLEARSEPARATION">&nbsp;</td>
</tr>
<tr>
<td colspan="2" class="TITLE">Example</td>
</tr>
<tr>
<td colspan="2" class="description">
<p>Here's a simple stylesheet that uses the <span class="LITERAL">document('')</span> function to access all of its own <span class="LITERAL">&lt;xsl:text&gt;</span> nodes. It then uses <span class="LITERAL">generate-id()</span> to generate a unique ID for each of those nodes, then calls <span class="LITERAL">generate-id()</span> again to illustrate that the function generates the same ID for a given node. Here's the stylesheet:</p>
<span class="PROGRAMLISTING"><pre>
&lt;?xml version="1.0"?&gt;
&lt;xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;

  &lt;xsl:output method="text"/&gt;

  &lt;xsl:variable name="newline"&gt;
&lt;xsl:text&gt;
&lt;/xsl:text&gt;
  &lt;/xsl:variable&gt;

  &lt;xsl:template match="/"&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:text&gt;A test of the generate-id() function:&lt;/xsl:text&gt;

    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:for-each select="document('')//xsl:text"&gt;
      &lt;xsl:text&gt;Node name: &lt;/xsl:text&gt;
      &lt;xsl:value-of select="name()"/&gt;
      &lt;xsl:text&gt; - generated id: &lt;/xsl:text&gt;
      &lt;xsl:value-of select="generate-id()"/&gt;
      &lt;xsl:value-of select="$newline"/&gt;
    &lt;/xsl:for-each&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:text&gt;Now we'll try it again...&lt;/xsl:text&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:value-of select="$newline"/&gt;
    &lt;xsl:for-each select="document('')//xsl:text"&gt;
      &lt;xsl:text&gt;Node name: &lt;/xsl:text&gt;
      &lt;xsl:value-of select="name()"/&gt;
      &lt;xsl:text&gt; - generated id: &lt;/xsl:text&gt;
      &lt;xsl:value-of select="generate-id()"/&gt;
      &lt;xsl:value-of select="$newline"/&gt;
    &lt;/xsl:for-each&gt;
  &lt;/xsl:template&gt;

&lt;/xsl:stylesheet&gt;</pre></span>
<p>Our stylesheet generates these results:</p>
<span class="PROGRAMLISTING"><pre>

A test of the generate-id() function:

Node name: xsl:text - generated id: NC
Node name: xsl:text - generated id: N16
Node name: xsl:text - generated id: N22
Node name: xsl:text - generated id: N28
Node name: xsl:text - generated id: N38
Node name: xsl:text - generated id: N44
Node name: xsl:text - generated id: N4A


Now we'll try it again...

Node name: xsl:text - generated id: NC
Node name: xsl:text - generated id: N16
Node name: xsl:text - generated id: N22
Node name: xsl:text - generated id: N28
Node name: xsl:text - generated id: N38
Node name: xsl:text - generated id: N44
Node name: xsl:text - generated id: N4A
</pre></span>
<p>The IDs generated each time are the same. </p>
</td>
</tr>
</table>
</div>
</body>
</html>