[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: "Michael Kay" <mhk@xxxxxxxxx>
Date: Wed, 13 Aug 2003 18:38:05 +0100

> 
> I'm having unexpected results with the generate-id() function.

Your expectations were wrong.
> 
> I am attempting to add a new node to an existing document and 
> assigning a new and
> *unique* id attribute to the new node.
> 
> Sample XML:
> <profile>
> <credential id="N400012">
> 		<username><![CDATA[user1]]></username>
> 		<password><![CDATA[pass1]]></password>
> 		<description><![CDATA[my description]]></description>
> 		<lastModified>2003-08-07 14:19:13</lastModified>
> 	</credential>
> 	<credential id="N40002B">
> 		<username><![CDATA[user2]]></username>
> 		<password><![CDATA[pass2]]></password>
> 		<description><![CDATA[another id]]></description>
> 		<lastModified>2003-08-06 16:03:52</lastModified>
> 	</credential>
> </profile>
> 
> I'd like this stylesheet to copy all existing credential 
> nodes and create a new node assigning a unique id with 
> generate-id().  It appears as if the generate-id call doesn't 
> observe existing id's in the current/copied credential nodes. 

Correct. There is no relationship between the id's assigned by
generate-id() and the set of ID-typed attributes in the source document.
What made you think there should be, apart from wishful thinking?

Michael Kay


>  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.
> 
> Thanks for your help.
> 
> 
> 
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
> 


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



Current Thread