You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Derumier David <Da...@trasys.be> on 2003/08/06 13:19:25 UTC

Probably a simple question: avoiding xmlns="" on each element...

 

Hello,

 

I'm just starting to use Xerces to manage (parse and generate) XML file.
I use Xerces 2.4 to generate XML file.

 

My code is very common, I think:

 

...

      Document xmldoc = builder.newDocument();

 

      Element root = xmldoc.createElement("NewNETP");

 

      root.setAttribute("xmlns", "XXX");

      root.setAttribute("xmlns:MF","XXX");

      root.setAttribute("xmlns:xsi" ,
"http://www.w3.org/2001/XMLSchema-instance");

      root.setAttribute("xsi:schemaLocation", "XXX ./new_netp.xsd");

...

    Element elt = xmldoc.createElement("CompanyName");

    elt.appendChild(xmldoc.createTextNode(netp.getCompanyName()));

...

 

 

The problem: XML file generated contain < XMLNS="" > on each element!
What's wrong? 

 

 

<?xml version="1.0" encoding="UTF-8"?>

<NewNETP xmlns="XXX"

    xmlns:MF="XXX"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="XXX ./new_netp.xsd"
CreationDate="2003-08-06T13:07:05">

    <CompanyName xmlns="">TEST</CompanyName>

    <CompanyAddress xmlns="">test</CompanyAddress>

    <CompanyEmail xmlns="">test</CompanyEmail>

...

 

 

Thank you,

 

David DERUMIER

 


Re: Probably a simple question: avoiding xmlns="" on each element...

Posted by Joseph Kesselman <ke...@us.ibm.com>.



That's entirely correct.

The parent element was assigned a default namespace URI
      <NewNETP xmlns="XXX" ...
You then -- erroneously, by the way; you should NEVER use the old
non-namespaced factory methods together with namespace-aware nodes! --
created a bunch of non-namespaced children.To properly express that in XML
syntax,  the default namespace must be explicitly cleared, which means
xmlns="" must be generated.

Depending on what you intended to do, consider one of:

1) Accept that this is the correct result.

2) Use an explicit (non-default) namespace on the parent element (give it a
prefix and bind that prefix), so the children aren't inhertiting a default
that must be cleared.

3) Put the children into the same namespace as the parent.


______________________________________
Joe Kesselman, IBM Next-Generation Web Technologies: XML, XSL and more.
"The world changed profoundly and unpredictably the day Tim Berners Lee
got bitten by a radioactive spider." -- Rafe Culpin, in r.m.filk


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org