OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

docbook-apps message

[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index] | [List Home]


Subject: Re: [docbook-apps] add 'id' afterwards automatically


> > 
> > IMHO generate-id(node-set) will generate a unique id for each given 
> > node-set. It always use the context node if no node-set is given.
> > so there is no need to run it recursivly. 
> 
> generate-id() is not guarantied to generate an identifier unique for the document.
> It is only guarantied to produce a different string for each node. If another node already
> has the same id as one just generated, there will be duplicate identifiers in the result.
> 
> > 
> > Just use a stylesheet like this (not tested):
> > 
> ....
> > It should do the job.
> > 
> 
> It won't.
> 

Here is one that will.

<xsl:transform 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
	version="1.0">

  <xsl:key name="ids" match="//*" use="@id"/>

  <xsl:template match="*">
    <xsl:copy>
      <xsl:if test="not(@id)">
	<xsl:attribute name="id">
	  <xsl:call-template name="generate-unique-id">
	    <xsl:with-param name="prefix" select="generate-id()"/>
          </xsl:call-template>
	</xsl:attribute>
      </xsl:if>
      <xsl:apply-templates select="*|@*|text()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template name="generate-unique-id">
    <xsl:param name="prefix"/>
    <xsl:param name="suffix"></xsl:param>
    <xsl:variable name="id" select="concat($prefix,$suffix)"/>

    <xsl:choose>
      <xsl:when test="key('ids',$id)">
        <xsl:call-template name="generate-unique-id">
	  <xsl:with-param name="prefix" select="$prefix"/>
	  <xsl:with-param name="suffix" select="concat($suffix,'x')"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$id"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="@*|text()">
    <xsl:copy/>
  </xsl:template>

</xsl:transform>

David Tolpin
http://davidashen.net/


[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index] | [List Home]