[jdom-interest] The default namespace behaviour

Edelson, Justin Justin.Edelson at mtvn.com
Wed Jun 7 06:37:06 PDT 2006


Michael and Paul are spot on - you're conflating the XML file ("lexical XML") with the XML document (the document object model [note the lower case]).
 
This may sound odd, but if you really want to do the transformation you describe below, why deal with JDOM at all? Instead do:
 
// read the file into String object docAsString
docAsString = docAsString.replaceAll("xmlns=\"one\"", "xmlns=\"two\");
 
If you wanted, you could then parse this String using StringReader:
Document doc = saxBuilder.build(new StringReader(docAsString));
 
> This doesn't fit the XML idea that, if you change the parent's namespace, un-prefixed children will implicitly
> change their namespace.
Can you provide a reference for where this "XML idea" is documented? DOM doesn't provide mutators for namespace, so this isn't remotely possible in the DOM world and I don't think there are many other XML specs that address changing documents.

________________________________

From: jdom-interest-bounces at jdom.org on behalf of Colin LeMahieu
Sent: Wed 6/7/2006 1:37 AM
To: jdom-interest at jdom.org
Subject: RE: [jdom-interest] The default namespace behaviour 



I was hoping to mimic the behavior that XML files have.  When using the default namespace, all un-prefixed sub elements are within that namespace.

 

<a xmlns="one">

  <b/>

  <c/>

</a>

 

The b and c tags are in the "one" namespace.

 

If I changed this to:

<a xmlns="two">

  <b/>

  <c/>

</a>

 

The b and c tags are in the "two" namespace.  I made one small change to a and got a side-effect of changing b and c; this is how it works with written XML documents.

 

If I parsed the first example and generated a JDOM model and called a.setNamespace(Namespace.getNamespace("two")) and then spit the file out, what I would have, essentially, is:

<a xmlns="two">

  <b xmlns="one"/>

  <c xmlns="one"/>

</a>

 

This doesn't fit the XML idea that, if you change the parent's namespace, un-prefixed children will implicitly change their namespace.

 

----------------

Colin LeMahieu

Phone: 408-499-5269

Fax: 267-989-4575

Email: clemahieu at gmail.com

 

I strongly disagree with your sense that:

a.setNamespace(namespace.getNamespace("ReallyLongNamespace"));

 

should set the namespace for all child elements that use the same default namespace as a. This would be a big leap for JDOM to make. Since it's entirely legitimate to have different default namespaces at different levels, having JDOM guess which ones you wanted to change would invariably be incorrect (in other words, what if I really wanted setNamespace() to just set that element's namespace). Writing the getDescendents()-using code above or even better, as Mr. Huffman suggested, a custom JDOMFactory would be the best way to go about explictingly specifying what transformation you wanted to do. 

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.jdom.org/pipermail/jdom-interest/attachments/20060607/c009695d/attachment.htm


More information about the jdom-interest mailing list