This is the mail archive of the
docbook-apps@lists.oasis-open.org
mailing list .
Re: [docbook-apps] add 'id' afterwards automatically
- From: Janning Vygen <vygen at gmx dot de>
- To: David Tolpin <dvd at davidashen dot net>, Michael Wiedmann <michael dot wiedmann at detewe dot de>
- Cc: DocBook-Apps ML <docbook-apps at lists dot oasis-open dot org>
- Date: Tue, 14 Oct 2003 14:53:46 +0200
- Subject: Re: [docbook-apps] add 'id' afterwards automatically
- Organization: Planwerk 6 /websolutions
- References: <200310141153.h9EBr4ke083421@adat.davidashen.net>
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
To unsubscribe from this list, send a post to docbook-apps-unsubscribe@lists.oasis-open.org, or visit http://www.oasis-open.org/mlmanage/.