This is the mail archive of the docbook-apps@lists.oasis-open.org mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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/.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]