[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index] | [List Home]
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
[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index] | [List Home]