p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   Unique identifier generator each time (http://p2p.wrox.com/showthread.php?t=100351)

msambasiva@gmail.com August 29th, 2019 03:09 AM

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.

mhkay August 29th, 2019 03:20 AM

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.

msambasiva@gmail.com August 29th, 2019 03:49 AM

<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

mhkay August 29th, 2019 04:13 AM

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.


All times are GMT -4. The time now is 05:57 PM.

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