[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Assigning unique id to new node using generate-id()


Subject: Re: [xsl] Assigning unique id to new node using generate-id()
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Wed, 13 Aug 2003 15:02:28 -0400

Kyle,

If you need to keep the IDs you have (which is very common), you should create new IDs according to whatever rules or "business logic" you use to create the IDs in the first place (which presumably addresses their requirement for uniqueness). If you have so far evaded the definition of any such logic, now is the time to tackle the problem, since generate-id() is not well suited for generating, and managing, unique identifiers in such a scenario.

This is because generate-id() is explicitly defined to provide values unique only for *one run* of a given stylesheet, and unique only within a set that the processor itself can define (it assigns all of them), whereas you need a value that's unique within a set that includes values already given in the input.

I'm sure list members will be happy to suggest their favorite ways of assigning unique ID values across a broad scope that spans many stylesheet runs. What's the best way? It really depends ... unfortunately a really robust solution may have to call some kind of assignment routine outside the XSLT processor, which only sees its own little world.

I hope this helps,
Wendell

At 02:40 PM 8/13/2003, you wrote:
Thank you for you reply.

I would like to keep the existing ID's the same.  As they are referenced from
other nodes (a credential is referenced from 0 or more services).

My current workaround seems to be working although I'm not 100% convinced that it
will always create a unique id.


After new node is created without an id, I transform the document again with.
This is run against the result of the first transform which adds the new
credential without and id.

<xsl:for-each select="credential">
<xsl:choose>
<xsl:when test="@id">
<xsl:copy-of select="."/>
</xsl:when>
<xsl:otherwise>
<credential>
<xsl:attribute name="id">
<xsl:value-of select="generate-id()"/>
</xsl:attribute>
<xsl:copy-of select="username"/>
<xsl:copy-of select="password"/>
<xsl:copy-of select="description"/>
<xsl:copy-of select="lastModified"/>
</credential>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>


As I said, this seems to work.

What other methods would you recommend for generating a unique id?  It is
possible, albeit unlikely, that the a profile may contain identical credential
nodes (where username, password, description and lastModified are identical).

Thanks,
Kyle


======================================================================
Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
----------------------------------------------------------------------
  Mulberry Technologies: A Consultancy Specializing in SGML and XML
======================================================================


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list




Current Thread
Keywords