Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 5th, 2008, 11:20 AM
Friend of Wrox
Points: 470, Level: 7
Points: 470, Level: 7 Points: 470, Level: 7 Points: 470, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2004
Location: Plano, TX, USA.
Posts: 109
Thanks: 18
Thanked 0 Times in 0 Posts
Default 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
Reply With Quote
  #2 (permalink)  
Old November 5th, 2008, 11:32 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

<xsl:number> will generate an incrementing number.

/- Sam Judson : Wrox Technical Editor -/
Reply With Quote
  #3 (permalink)  
Old November 5th, 2008, 11:40 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
Reply With Quote
  #4 (permalink)  
Old November 5th, 2008, 11:50 AM
Friend of Wrox
Points: 470, Level: 7
Points: 470, Level: 7 Points: 470, Level: 7 Points: 470, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2004
Location: Plano, TX, USA.
Posts: 109
Thanks: 18
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote
  #5 (permalink)  
Old November 5th, 2008, 11:58 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

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
Reply With Quote
  #6 (permalink)  
Old November 5th, 2008, 01:43 PM
Friend of Wrox
Points: 470, Level: 7
Points: 470, Level: 7 Points: 470, Level: 7 Points: 470, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2004
Location: Plano, TX, USA.
Posts: 109
Thanks: 18
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote
  #7 (permalink)  
Old November 6th, 2008, 11:14 AM
Friend of Wrox
Points: 470, Level: 7
Points: 470, Level: 7 Points: 470, Level: 7 Points: 470, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2004
Location: Plano, TX, USA.
Posts: 109
Thanks: 18
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote
  #8 (permalink)  
Old November 6th, 2008, 12:20 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom Generate IDs wackoyacky SQL Language 4 June 4th, 2007 08:53 AM
Auto generate IDs in MS SQL... Lynn SQL Server 2000 8 November 28th, 2006 10:00 AM
Ints or Unique IDs? pettrer ASP.NET 2.0 Basics 3 June 5th, 2006 08:28 PM
Unique Control IDs inside a loop using a varible Joshua Sebastian Classic ASP Basics 2 February 3rd, 2005 10:44 AM
unique login names and incremental user IDs krstofer Classic ASP Basics 8 March 11th, 2004 10:55 AM



All times are GMT -4. The time now is 03:41 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.