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 | Calendar | 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 August 29th, 2019, 03:09 AM
Registered User
Points: 44, Level: 1
Points: 44, Level: 1 Points: 44, Level: 1 Points: 44, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2017
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
Default Unique identifier generator each time

Hi,

XSLT2.0, Saxon

We have a requirement to create a xml file foreach service given below. As the service names are same, we are using generate-id to append a random number. ex: Sales_Person_Setup.12345.xml Sales_Person_Setup.76589.xml

<service anchor="com.apps.cdm.foundation.resources.resource ServiceV2.SalesPersonService">
<name>Sales Person Setup</name>

<service anchor="com.apps.cdm.foundation.resources.resource Service.SalesPersonService">
<name>Sales Person Setup</name>

So far, it's good. The problem here is, generate-id is creating separate unique id for each execution.

We need to have unique identifier using anchor attribute & name element each time. It would be great help if you can suggest any clue.

Thanks in advance,
Samba.
Reply With Quote
  #2 (permalink)  
Old August 29th, 2019, 03:20 AM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You need to tell us more about what you are doing (what's the source document, what is the stylesheet) and why the results are unsatisfactory. Please reduce it to the minimum example that demonstrates the problem, but make it complete and runnable. I could suggest other approaches, e.g. using position(), but without understanding your requirements in more detail, that might have exactly the same drawbacks.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
  #3 (permalink)  
Old August 29th, 2019, 03:49 AM
Registered User
Points: 44, Level: 1
Points: 44, Level: 1 Points: 44, Level: 1 Points: 44, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2017
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
Default

<serviceReference>
<services>
<service anchor="com.apps.cdm.foundation.resources.resource ServiceV2.SalesPersonService">
<name>Sales Person Setup</name>
<someelements>some content</someelements>
</service>
<service anchor="com.apps.cdm.foundation.resources.resource Service.SalesPersonService">
<name>Sales Person Setup</name>
<someelements>different content</someelements>
</service>

</services>
</serviceReference>

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0"
exclude-result-prefixes="#all">

<xsl:template match="/">
<xsl:variable name="inpath" select="string-join(tokenize(document-uri(/), '/')[position() lt last()], '/')"/>
<xsl:variable name="docpath">
<xsl:value-of select="tokenize(document-uri(/), '/')[last()-1]"/>
</xsl:variable>
<xsl:variable name="indexGroupingKey" select="6"/>
<!-- Iterate through all the service data objects to prepare dita map container -->
<xsl:if test="count(//serviceReference/services/service) &gt; 0">
<xsl:for-each select="//serviceReference/services/service">

<xsl:sort select="name"/>
<xsl:variable name="topic_id_svc">
<!--takes the name and strips special chars,etc -->
<xsl:call-template name="generatename">
<xsl:with-param name="title">
<xsl:value-of select="name"/>
</xsl:with-param>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="topic_name_svc">
<!--passes the name string and appends a dash and id to give unique id -->
<xsl:call-template name="generateid">
<xsl:with-param name="title">
<xsl:value-of select="substring($topic_id_svc,1,50)"/>
</xsl:with-param>
<!--Concatenate title, some are very long -->
<xsl:with-param name="genid">
<xsl:value-of select="generate-id(name)"/>
</xsl:with-param>
</xsl:call-template>
</xsl:variable>

<xsl:result-document method="xml" href="{$docpath}{$file.separator.esc}topic_refs{$f ile.separator.esc}{$topic_name_svc}.dita" doctype- system="topic.dtd" doctype-public="-//OASIS//DTD DITA Topic//EN" encoding="UTF-8"
indent="yes">
</xsl:result-document>
</xsl:template>
</xsl:transform>

Currently it's generating the seprate unique files for each service with the content in same execution of tool. The expected output is to have same names for each service accross different executions of the tool.
Note: There might be chances to edit the services. i.e new services may be added or deleted the services from the xml. Service position might not be constant.

Current results:
================
Execution 1:
Sales_Person_Setup-12345.xml Sales_Person_Setup-76589.xml
Execution 2:
Sales_Person_Setup-78321.xml Sales_Person_Setup-67598.xml

Expected results:
================
Execution 1:
Sales_Person_Setup-12345.xml Sales_Person_Setup-76589.xml
Execution 2:
Sales_Person_Setup-12345.xml Sales_Person_Setup-76589.xml
Reply With Quote
  #4 (permalink)  
Old August 29th, 2019, 04:13 AM
mhkay's Avatar
Wrox Author
Points: 18,481, Level: 59
Points: 18,481, Level: 59 Points: 18,481, Level: 59 Points: 18,481, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,960
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Unfortunately you haven't shown the generateid named template.

Saxon's implementation of generate-id() delivers a two-part ID, the document number which will vary between runs, and the node-id within document which should be the same provided that the source document is the same. You could try picking this apart.

But a much simpler approach would be to use position().

Incidentally, a few observations on your code:

(a) You often use an xsl:variable containing an xsl:value-of where using xsl:variable/@select would be more appropriate

(b) With XSLT 2.0 the code becomes much more concise and readable if you use functions rather than named templates

(c) The xsl:if around the xsl:for-each appears redundant, though I can't be sure because of missing code.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
Reply With Quote
The Following User Says Thank You to mhkay For This Useful Post:
msambasiva@gmail.com (August 29th, 2019)
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
Create unique identifier for importing Excel into Access littlerauws Access 0 August 6th, 2013 09:31 AM
Identifier headworth BOOK: Beginning Microsoft Visual Basic 2008 ISBN: 978-0-470-19134-7 4 July 28th, 2009 04:34 PM
Unique Key by concatenating military time and date code_lover C++ Programming 0 August 8th, 2008 06:09 AM
Unique Record Identifier peace Access VBA 1 December 6th, 2005 12:03 PM
Updating Unique Identifier foreign key to NULL jraymond VB Databases Basics 0 June 16th, 2003 01:51 PM



All times are GMT -4. The time now is 06:07 PM.


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