Hi Endre.
Yes, they have to be in order, and the elements with no _id have to be
the last ones. Then I simply get the closest node that has an _id.
If they aren't, then you'll have to change the variable 'last' so it
search for the biggest one.
Something like this:
<xsl:template name="generate-id">
<xsl:variable name="lastid"> <!-- here we search for the biggest _id
-->
<xsl:call-template name="biggest"/>
</xsl:variable>
<xsl:variable name="last"
select="number(substring-after($lastid,'id'))"/> <!-- this now usea
$lastid -->
<xsl:variable name="this"
select="count(preceding-sibling::element[not(@_id)])+1"/>
<xsl:variable name="temp" select="concat('00000',$last+$this)"/>
<xsl:value-of
select="concat('id',substring($temp,string-length($temp)-5))"/>
</xsl:template>
<xsl:template name="biggest">
<xsl:for-each select="(preceding-sibling::element |
following-sibling::element)[(_at_)_id]">
<xsl:sort select="@_id"/>
<xsl:if test="position()=last()"> <!-- the one we want will be the
last one -->
<xsl:value-of select="@_id"/>
</xsl:if>
</xsl:for-each>
</xsl:template>
This xml:
<page>
<element _id="id000001"/>
<element/>
<element _id="id000004"/>
<element _id="id000003"/>
<element/>
</page>
Now produce this result:
<page>
<element _id="id000001"/>
<element _id="id000005"/>
<element _id="id000004"/>
<element _id="id000003"/>
<element _id="id000006"/>
</page>
-----Original Message-----
From: owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
[mailto:owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com] On Behalf Of
Endre
Magyari
Sent: Monday, November 11, 2002 6:59 PM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: Re: [xsl] key(), generate-id question
Dear Americo,
Thank you!
One question: Do you rely on the strictly ascending order of the id
values?
I will try it now,
Thanks!
Endre
----- Original Message -----
From: "Américo Albuquerque" <aalbuquerque(_at_)viseu(_dot_)ipiaget(_dot_)pt>
To: <xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
Sent: Monday, November 11, 2002 8:39 PM
Subject: RE: [xsl] key(), generate-id question
Hi Endre.
Ttry this.
<xsl:template match="page">
<xsl:copy>
<xsl:apply-templates select="element[(_at_)_id] | element[not(@_id)]"/>
</xsl:copy>
</xsl:template>
<xsl:template match="element">
<element>
<xsl:attribute name="xmi.id">
<xsl:choose>
<xsl:when test="@_id">
<xsl:value-of select="@_id"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="generate-id"/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</element>
</xsl:template>
<xsl:template name="generate-id">
<xsl:variable name="last"
select="number(substring-after(preceding-sibling::element[(_at_)_id][1]/@_i
d,
'id'))"/>
<xsl:variable name="this"
select="count(preceding-sibling::element[not(@_id)])+1"/>
<xsl:variable name="temp" select="concat('00000',$last+$this)"/>
<xsl:value-of
select="concat('id',substring($temp,string-length($temp)-5))"/>
</xsl:template>
they work with this xml:
<page>
<element _id="id000001"/>
<element _id="id000002"/>
<element _id="id000003"/>
<element />
<element />
</page>
And produce this:
<page>
<element _id="id000001"/>
<element _id="id000002"/>
<element _id="id000003"/>
<element _id="id000004"/>
<element _id="id000005"/>
</page>
If the structure of your xml is diferent (probably is) then you can
change the 'element' in the templates by 'node()' Hope that this helps
you.
-----Original Message-----
From: owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
[mailto:owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com] On Behalf Of
Endre
Magyari
Sent: Monday, November 11, 2002 7:30 AM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: Re: [xsl] key(), generate-id question
You can't control how generate-id() works, but you could do
something like write a named template that will generate the ID. For
example, you could count the number of preceding elements that don't
have an _ID attribute and then add that value to the numeric part of
the nearest preceding element that *does* have an _ID attribute. All
doable with normal XSLT functions.
Based on this, as far as now I could do an enumeration of all the _id
values (as numbers) from the document, but I have no idea how to
select the one with the maximal value out of them. And Also, I have no
idea how/where to store the new id values being assigned. The problem
is that I've no experience with declarative languages. I can not get
used to the idea that a variable can not vary. What I would do is that
(if curr_value > max) max = curr_value. But how to do this here? Any
help is greatly appreciated.
<!-- ID Generator-->
<xsl:template name="idgen">
<xsl:for-each select="node()">
<xsl:for-each select="@_id">
<xsl:number value="substring-after(current(),'id')"/>;
</xsl:for-each>
</xsl:for-each>
</xsl:template>
Thank you,
Endre
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list