Hi all,

I have a tree structure backend template:

    <xs:element name="nodes">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="node" minOccurs="0" maxOccurs="unbounded">
                    <xs:complexType>    
                        <xs:sequence>                            
                            <xs:element ref="node" minOccurs="0" 
maxOccurs="unbounded"/>
                            <xs:element name="nl-NL" type="xs:string"/>        
                            <xs:element name="label" type="xs:string"/>         
   
                        </xs:sequence>
                        <xs:attribute name="id" type="xs:string"/>              
      
                    </xs:complexType>
                </xs:element>
                
            </xs:sequence>
          </xs:complexType>
    </xs:element>

I want to generate an autonumber id for each item in a tree. Once strict rule 
is that once an id is giving to an element it should never be reused. Even id's 
of elements that are deleted.

To implement this i use an post processing xslt:
  <xsl:import href="extensions://types/resources/xsl//autoids.xsl"/>
    
  <xsl:variable name="max_id_stored" select="/nodes/@maxid"/>
  <xsl:variable name="CONTAINER_EL">node</xsl:variable>
  <xsl:variable name="ID_ATTR">id</xsl:variable>
  <xsl:variable name="ELM_IGNORE">state</xsl:variable>
  
  <xsl:template match="nodes">
    <nodes maxid="{$new_max_id}">      
      <xsl:apply-templates select="*"/>      
    </nodes>
  </xsl:template>

where maxid is constantly updated!.

the autoid.xsl contains:
<xsl:variable name="max">
    <xsl:call-template name="max">
      <xsl:with-param name="nodes" 
select="//*[name()=$CONTAINER_EL]/@*[name()=$ID_ATTR and 
string(number(.))!='NaN']"/>
    </xsl:call-template>
  </xsl:variable>
  
  <xsl:variable name="maxid">
    <xsl:choose>
      <xsl:when test="(string(number($max_id_stored))='NaN' or 
number($max_id_stored) &lt;= number($max)) and number($max) &gt;= 0">
          <xsl:value-of select="number($max) + 1"/>    
      </xsl:when>
      <xsl:when test="string(number($max_id_stored)) != 'NaN'">
        <xsl:value-of select="$max_id_stored"/>
      </xsl:when>
      <xsl:otherwise>0</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  
  <xsl:variable name="nrOfElementsWithoutId" 
select="count(//*[name()=$CONTAINER_EL and (@*[name()=$ID_ATTR]='' or 
count(@*[name()=$ID_ATTR])=0)])"/>
  
  <xsl:variable name="new_max_id" 
select="number($nrOfElementsWithoutId)+number($maxid)"/>
  
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  
  <xsl:template match="*[name()=$CONTAINER_EL]" priority="9">
    <xsl:copy>
      <xsl:attribute name="{$ID_ATTR}">
        <xsl:choose>
          <xsl:when test="number(@*[name()=$ID_ATTR]) &gt;= 0 or 
string-length(@*[name()=$ID_ATTR]) &gt; 0">
            <xsl:value-of select="@*[name()=$ID_ATTR]"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:variable name="emptyPrecedingIds">
              <xsl:number level="any" count="*[name()=$CONTAINER_EL and 
(@*[name()=$ID_ATTR]='' or count(@*[name()=$ID_ATTR])=0)]"/>
            </xsl:variable>             
            <xsl:number value="number($maxid)+number($emptyPrecedingIds)-1" 
format="1"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:attribute> 
      <xsl:apply-templates select="@*[name()!=$ID_ATTR]|node()"/> 
    </xsl:copy>    
  </xsl:template>
  

This code reuses id's. 
What i also notices is that <xsl:variable name="max_id_stored" 
select="/nodes/@maxid"/> is not updated with the new maxid after a save. Only 
after i close en open the document again. This is one of the reason it can 
reuses ids of those elements that are deleted. How come max_id_stored is not 
changed after a save? Any help?

THanks in advance,
Amon





_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today it's FREE!
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/********************************************
Hippocms-dev: Hippo CMS development public mailinglist

Searchable archives can be found at:
MarkMail: http://hippocms-dev.markmail.org
Nabble: http://www.nabble.com/Hippo-CMS-f26633.html

Reply via email to