Re: [xml] redicting parts of trees


On Tue, 2005-05-24 at 15:15 +0200, Kasimier Buchcik wrote: 

I would like to make a change to xmlReconciliateNs(), while fixing it.

Current behaviour/errors:
1) It does not find already existent ns-decls in @root's descendants;
   this can lead to shadowed prefixes.
2) Due to a speed-up code of xmlSearchNsByHref(), which returns
   a ns-decl even if it is not declared, but just referenced, it will
   fail to create missing ns-decls.
3) It creates missing ns-decls on @root only.
4) If creating missing ns-decls, it does not allow to reuse a prefix,
   which is already used in @tree's parent axis. This increases the
   cases, where the prefix need to be change.
   Example: For missing ns-decls in the default namespace, the prefix 
   will be changed to "default1", "default2", etc.

New behaviour:
1) It will attach ns-references to the nearest ancestor ns-decl with the
   same ns-name; this includes @root's descendant axis.
2) It will not use ns-references while searching for ns-decls.

Now the most important change:
3) It will create missing ns-decls on the nearest ancestor-or-self
   of a node which references a missing ns-decl.

Arguments for 3):

It will be more conforming to W3C's ns-normalisation algorithm, which
creates missing ns-decls where first needed.

The following tries to advocate 3) from the view of DOM-wrappers:

DOM-wrappers should not create ns-decls automatically, so a
ns-normalization is only performed per DOM-API call. I.e. it would be
wrong to magically auto-create a ns-decl on any added element-node on
the node itself.
Example (Delphi code):

elem := doc.createElementNS('', 'a1');
for i := 1 to 3 do begin
  elem := doc.createElementNS('urn:test:foo', 'foo:a2');

should not result in...

  <foo:a2 xmlns:foo="urn:test:foo"/>
  <foo:a2 xmlns:foo="urn:test:foo"/>
  <foo:a2 xmlns:foo="urn:test:foo"/>

... automatically. With DOM, one still has the possibility to add a
namespace declaration attribute to avoid this:


resulting in the following:

<a1 xmlns:foo="urn:test:foo">

It seems that xmlReconciliateNs() should not be expected to be called
immediately after each change to the tree.

This being said, creating missing ns-decls on nearest ancestor-or-self
elements will have the following advantage:

Consider the following doc pieces:
<p1 xmlns="urn:test:AAA">

<p1 xmlns="urn:test:BBB">

Remove <a> and <b> and add them to <p2>:
<p2 xmlns="urn:test:ZZZ"/>  
  <a/> <!-- a.namespaceURI() is "urn:test:AAA", a.prefix() is NULL -->
  <b/> <!-- a.namespaceURI() is "urn:test:BBB", a.prefix() is NULL -->

Ns-reconcile <p2>:
<p2 xmlns="urn:test:ZZZ"/>  
  <a xmlns="urn:test:AAA"/>
  <b xmlns="urn:test:BBB"/>

So we preserve the default namespace for <a> and <b> if we create
ns-decls where needed. On the other hand, if creating new ns-decls on
<p2> only, we get:

<p2 xmlns="urn:test:ZZZ"   
  <default1:a />
  <default2:b />

Now consider big content of <a> and <b>, plus QNames in text content
which require the default namespace: the advantage of the default
namespace is lost, plus QNames without a prefix are in the wrong

By the way, the DOM Document.normalizeDocument() method, which can
ns-normalize the document as well, need not to be called by the user. So
it's up to the user: if he thinks that he has placed the namespace
declaration attributes at the correct position, then he don't need to
ns-normalize; this is commonly done if the doc is big and a
ns-normalization is avoided. If he's unsure, or simply didn't care about
declaring namespaces, he'll call Document.normalizeDocument().

Any objections to change xmlReconciliateNs this way?



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]