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