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


Am Dienstag, 14. Oktober 2003 13:53 schrieb David Tolpin:
> > Is there an easy way to process existing DocBook XML files and
> > add to all elements an "id" attribute which do not already have
> > one?
>
> I would do it in Java in two passes. During the first pass, I would
> collect all "id" values in a hash table,
>
> [...]
>
> It is possible to do the same in XSLT (and the stylesheet will be
> called once) with help of xsl:key/key(), but the code will be less
> readable, in my opinion. For each element without id attribute, one
> should generate-id() and then add a sequential number to the
> generated value in recursive calls to a template until there are no
> elements with this id in the document.

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. 

Just use a stylesheet like this (not tested):

<xsl:template match="*">
  <xsl:copy>
    <xsl:attribute name="id">
      <xsl:value-of select="generate-id()"/>
    </xsl:attribute>
    <!-- this overrides generated ids with existing ids --> 
    <xsl:copy-of select="@*"> 
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

<!-- copy the rest -->
<xsl:template match="text()|processing-instruction()|comment()">
  <xsl:copy/>
</xsl:template>

It should do the job.

kind regards,
janning


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