[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 13:10:11 -0400

Hi Kyle,

At 12:11 PM 8/13/2003, you wrote:
I'd like this stylesheet to copy all existing credential nodes and create a new
node assigning a unique id

It's a reasonable requirement.


with generate-id().

Why? as opposed to by some other means?


It appears as if the generate-id
call doesn't observe existing id's in the current/copied credential nodes. So I
end up with duplicate (illegal) id's. I've tried passing several context nodes
to the generate-id() function, but with no luck.


Sample XSL:
<xsl:template match="/profile">
<profile>
<xsl:for-each select="credential">
<xsl:copy-of select="."/>
</xsl:for-each>
<credential>
<xsl:attribute name="id">
<xsl:value-of select="generate-id()"/>
</xsl:attribute>
<username><xsl:value-of select="$username"/></username>
<password><xsl:value-of select="$password"/></password>
<description><xsl:value-of select="$description"/></description>
<lastModified><xsl:value-of
select="java:getCurrentDateTimeGMT($dateHelper)"/></lastModified>
</credential>
</profile>
</xsl:template>


My current workaround is to transform twice.  The first transformation appends
the new credential node.  The second transformation assigns an id (using
generate-id()) to any credential nodes without an id (probably only newly
created).  Seems like I should be able to do it all in one transform.

The root of your problem is in thinking that generate-id() assigns (unique) IDs to new nodes, when it doesn't; it merely assigns (or retrieves) an ID for a node in the source. The generated value has nothing to do with any value in the source, even if on an attribute called "id". It needs only to be unique to the node provided as argument to the function, or to the context node (in your case, "/profile") if none is given.


Given the code you have provided, in which you create only one <credential> and assign it an ID unique to the <profile> you have matched, there is a good chance that the observed duplication is actually faithful to the input, in which IDs are not unique (check to verify whether this is not the case). To guarantee uniqueness, reassign an ID to every credential. Since you are also creating new credentials, a second pass (your current workaround) is an appropriate place to do this. If you have a requirement to preserve IDs where they exist, this is a more complex problem and generate-id() is probably not part of the solution at all.

Cheers,
Wendell



======================================================================
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
xml