 |
| XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

November 5th, 2008, 11:20 AM
|
|
Friend of Wrox
|
|
Join Date: May 2004
Posts: 109
Thanks: 18
Thanked 0 Times in 0 Posts
|
|
How to Generate Unique values for IDs
I'm running Saxon 9 (When I upgrade to Oxygen 10, I'll be running Saxon 9SA, oh, yeah!)
I need to generate a set of XML files for a customer. They defined the schema and have already developed tools using the files.
They have an attribute on assorted elements called 'mcp-id'
The only requirement is the value for each instance must be unique within the file.
My thought is a simple counter (first instance of mcp-id=1, second instance of mcp-id=2, and so on).
Any suggestions on generating unique values for my mcp-id?
------------------------
Keep Moving Forward
GnuPG Key fingerprint = 1AD4 726D E359 A31D 05BF ACE5 CA93 7AD5 D8E3 A876
Michael Hare
__________________
------------------------
Keep Moving Forward
GnuPG Key fingerprint = 1AD4 726D E359 A31D 05BF ACE5 CA93 7AD5 D8E3 A876
Michael Hare
|
|

November 5th, 2008, 11:32 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
<xsl:number> will generate an incrementing number.
/- Sam Judson : Wrox Technical Editor -/
|
|

November 5th, 2008, 11:40 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
If it's an ID attribute then it the identifier should start with a letter - so use say A1 A2 A3 rather than 1 2 3.
Maintaining a purely incrementing counter isn't always easy in XSLT. It's usually better to use something that's a function of where you are in the source document. If there's a simple correspondence between nodes in the source and nodes in the result then generate-id() is often suitable. You can also use xsl:number but it might be less efficient.
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
|
|

November 5th, 2008, 11:50 AM
|
|
Friend of Wrox
|
|
Join Date: May 2004
Posts: 109
Thanks: 18
Thanked 0 Times in 0 Posts
|
|
Thanks!
I tried generate-id(), but I wasn't getting unique values for each instance.
I think <xsl:number> is what I was looking for.
------------------------
Keep Moving Forward
GnuPG Key fingerprint = 1AD4 726D E359 A31D 05BF ACE5 CA93 7AD5 D8E3 A876
Michael Hare
|
|

November 5th, 2008, 11:58 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
If generate-id() didn't give you unique ids, that's because you called it twice while positioned at the same input node. If that's the case, then you will have the same problem with xsl:number.
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
|
|

November 5th, 2008, 01:43 PM
|
|
Friend of Wrox
|
|
Join Date: May 2004
Posts: 109
Thanks: 18
Thanked 0 Times in 0 Posts
|
|
Yes, I do..
I'm going to attempt to re-structure the script.
------------------------
Keep Moving Forward
GnuPG Key fingerprint = 1AD4 726D E359 A31D 05BF ACE5 CA93 7AD5 D8E3 A876
Michael Hare
|
|

November 6th, 2008, 11:14 AM
|
|
Friend of Wrox
|
|
Join Date: May 2004
Posts: 109
Thanks: 18
Thanked 0 Times in 0 Posts
|
|
I've restructured the script, but it's only helping so much.
I'm at the mercy of the input schema.
Why is the generate-id() function generating unique IDs on the input schema, rather than the output schema?
A script can take a single input element can generate several output elements and they each need their own unique ID.
I'm sure it's because I'm missing some pattern in the XML, but it seems it would be simpler to create unique ID based on the output schema (since that's where the IDs are going to be placed) rather than the input schema.
Thanks!
------------------------
Keep Moving Forward
GnuPG Key fingerprint = 1AD4 726D E359 A31D 05BF ACE5 CA93 7AD5 D8E3 A876
Michael Hare
|
|

November 6th, 2008, 12:20 PM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
>Why is the generate-id() function generating unique IDs on the input schema
Two possible answers:
(a) it generates an id for the context node (the current input node) because that's what the spec says it should do.
(b) why does the spec say it should do that? (i) because that's useful for some of the intended use cases, (ii) because XSLT is a functional language, and calling the same function twice with the same input conditions therefore has to produce the same answer.
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer's Reference
|
|
 |