[XOM-interest] Problem creating the right namespace.

Elliotte Harold elharo at metalab.unc.edu
Wed Feb 25 09:28:46 EST 2009


Christian-Josef Schrattenthaler wrote:
> Hi!
> 
>  
> 
> I have to use xml files for a little Java project. These files are
> generated over the Java application itself, but I have to use an
> external xml schema.
> 
>  
> 
> The root element must look like:
> 
>  
> 
> <rootelement xmlns=http://... xmlns:xsi=http://...
> xsi:schemaLocation="http://...">
> 
>  
> 
> I did:
> 
>  
> 
> Element xmlRoot = new Element("onlv",
> "http://www.oenorm.at/schema/A2063/2009-01-15");
> 
> xmlRoot.addNamespaceDeclaration("xsi",
> "http://www.w3.org/2001/XMLSchema-instance");
> 

You don't need to add an explicit namespace declaration. Instead just add

Attribute a = new Attribute("xsi:schemaLocation", 
"http://www.w3.org/20001/schema-instance", "foo/bar");

> But now I don't know how to add 'xsi:schemaLocation="http://..."', and I
> have an empty (xmlns="") attribute on each child element I add to the
> root element.


Every element you create in a namespace shoudl be given its namespace in 
Java. e,g,

Element foo = new Element("foo", 
"http://www.oenorm.at/schema/A2063/2009-01-15");

XOM will figure out where to put the namespace delclarations. You only 
need to call addNamespaceDeclaration if you have namespaces in attribute 
values that do not appear in element content in the same scope, or if 
you want to move a namespace declaration higher in the hierarchy 
(usually to the root element) than it would naturally appear.


-- 
Elliotte Rusty Harold  elharo at metalab.unc.edu
Refactoring HTML Just Published!
http://www.amazon.com/exec/obidos/ISBN=0321503635/ref=nosim/cafeaulaitA


More information about the XOM-interest mailing list