This is the mail archive of the
docbook-apps@lists.oasis-open.org
mailing list .
Re: [docbook-apps] add 'id' afterwards automatically
- From: David Tolpin <dvd at davidashen dot net>
- To: 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 16:53:04 +0500 (AMST)
- Subject: Re: [docbook-apps] add 'id' afterwards automatically
> 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,
Hashtable idtab=new Hashtable();
startElement(...)
String id=attrs.getValue("id");
if(id!=null) idtab.put(id,id);
During the second pass, I would add an autogenerated attribute not present in the table
to all elements without one.
int number=0;
startElement(...)
String id=attrs.getValue("id");
if(id==null) {
do {
id="generated"+number;
++number;
} while(idtab.containsKey(id));
attrs.addAttribute("","id","id",id);
}
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.
David Tolpin
To unsubscribe from this list, send a post to docbook-apps-unsubscribe@lists.oasis-open.org, or visit http://www.oasis-open.org/mlmanage/.