xsl-list
[Top] [All Lists]

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

2003-08-13 11:40:34
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


On Wed, 13 Aug 2003 13:10 , Wendell Piez <wapiez(_at_)mulberrytech(_dot_)com> 
sent:

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:
        
                
                        
                                
                        
                        
                                
                                        
select="generate-id()"/>
                                
                                
select="$username"/>
                                
select="$password"/>
                                
select="$description"/>
                                
select="java:getCurrentDateTimeGMT($dateHelper)"/>
                        
                
        

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  
and assign it an ID unique to the  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                           
wapiez(_at_)mulberrytech(_dot_)com','','','')">wapiez(_at_)mulberrytech(_dot_)com
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







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