Re: [xsl] Re: Re: Creating an array

Subject: Re: [xsl] Re: Re: Creating an array
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Tue, 27 May 2003 19:44:03 -0400
At 2003-05-27 15:57 -0600, Alan Gardner wrote:
The
solution I need is a way to have random numbers but not duplicated numbers
(the randomly generated number is associated with a link - which can't be
duplicated or content on the web site will be redundant.

Why do they have to be random?


So I'm back at the original question - how do I track what random numbers
I've already generated so I can compare them using contains() or some string
function.

What comparison are you looking for?


Are you looking for uniqueness across a single execution of the stylesheet producing a final result, or are you looking for uniqueness across a multiple number of executions of a stylesheet over many results?

If you can keep a "master number" and just use steadily incrementing values, then you won't have a problem with duplicates. For comparison purposes, including non-digit name characters in your link identifiers, perhaps using ...,"_12_", "_13_", "_14_" will ensure there is no conflict with "_112_", "_113_", "_114" when using the contains() function (though I'm not really sure why you are focusing on the contains() function).

If you are obliged to use XSLT for everything, then you could even keep track of that master number in an XML file (an example is below; note how the master.xml file is incremented after every run of the stylesheet) ... but I don't think this is very elegant.

Perhaps if you described the problem you were trying to solve with your thought of using random numbers rather than trying to find a way of working with random numbers, we might be able to suggest an alternative solution to your problem.

I hope this helps.

................... Ken

t:\ftemp>type master.xml
<number>123</number>

t:\ftemp>type alan.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<test>
  <thing/>
  <thing/>
  <thing/>
  <thing/>
  <thing/>
</test>
t:\ftemp>type alan.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:saxon="http://icl.com/saxon";
                extension-element-prefixes="saxon"
                exclude-result-prefixes="saxon"
                version="1.0">

<xsl:template match="/">
  <xsl:variable name="start" select="number(document('master.xml')/number)"/>

  <result>
    <xsl:for-each select="/test/thing">
      <Hello/>
    </xsl:for-each>
  </result>

  <saxon:output href="master.xml">
    <number><xsl:value-of select="$start+count(/test/thing)"/></number>
  </saxon:output>
</xsl:template>

</xsl:stylesheet>

t:\ftemp>saxon -o alan.out alan.xml alan.xsl

t:\ftemp>type master.xml
<?xml version="1.0" encoding="utf-8"?><number>128</number>
t:\ftemp>saxon -o alan.out alan.xml alan.xsl

t:\ftemp>type master.xml
<?xml version="1.0" encoding="utf-8"?><number>133</number>
t:\ftemp>


-- Upcoming hands-on courses: (registration still open!) - (XSLT/XPath and/or XSL-FO) North America: June 16-20, 2003

G. Ken Holman                mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.         http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0   +1(613)489-0999 (F:-0995)
ISBN 0-13-065196-6                      Definitive XSLT and XPath
ISBN 0-13-140374-5                              Definitive XSL-FO
ISBN 1-894049-08-X  Practical Transformation Using XSLT and XPath
ISBN 1-894049-11-X              Practical Formatting Using XSL-FO
Member of the XML Guild of Practitioners:    http://XMLGuild.info
Male Breast Cancer Awareness http://www.CraneSoftwrights.com/s/bc


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



Current Thread