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 Thomas Börkel <tb...@ap-ag.com> on 2003/05/26 15:31:46 UTC

createElementNS() does not work as expected in new versions of Xerces (> 2.0.1)

HI!

We used createElementNS() in 2.0.1 successfully. Since we switched to newer versions (now 2.4.0), it stopped working as expected. We have reviewed our code and changed it as suggested in http://nagoya.apache.org/bugzilla/show_bug.cgi?id=7687 with no positive effect.

Using 2.0.1, the test code below produces:
<root><testNode xmlns="http://schemas.p2plus.com/test/"/></root>
<root><testNode xmlns="http://schemas.p2plus.com/test/"/></root>

Using 2.4.0, the test code below produces:
<root><testNode/></root>
<root><testNode xmlns=""/></root>

Any help would be greatly appreciated. Thanks!

Regards,
Thomas


Test code:
----------

import java.io.*;
import javax.xml.parsers.*;
import org.apache.xml.serialize.*;
import org.w3c.dom.*;
import org.xml.sax.*;

public class NamespaceTest {
  
  public static void main(String[] args) throws Exception {
    Document doc;
    Element root, tag;
    Attr attr;
    String namespaceURI = "http://schemas.p2plus.com/test/";
    String tagName = "testNode";
    
    doc = getDocument("<root />");
    root = doc.getDocumentElement();
    tag = doc.createElementNS(namespaceURI, tagName);  
    root.appendChild(tag);
    System.out.println(docToString(doc));
    
    doc = getDocument("<root />");
    root = doc.getDocumentElement();
    tag = doc.createElementNS(namespaceURI, tagName);  
    attr = doc.createAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns");
    tag.setAttributeNodeNS(attr);
    root.appendChild(tag);
    System.out.println(docToString(doc));
  }
  
  public static Document getDocument(String xml) throws Exception {
    DocumentBuilderFactory dbf;
    DocumentBuilder db;
    Document doc;
    
    dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    dbf.setValidating(true); 
    dbf.setAttribute("http://apache.org/xml/features/validation/dynamic",
                    new Boolean(true));
    dbf.setAttribute("http://apache.org/xml/features/validation/schema", 
                     new Boolean(true));
    db = dbf.newDocumentBuilder();
    doc = db.parse(new InputSource(new StringReader(xml)));
    return(doc);
  }
  
  public static String docToString(Document doc) throws Exception {
    OutputFormat outputFormat;
    XMLSerializer xmlSerializer;
    StringWriter sw;
    
    try {
      sw = new StringWriter();
      outputFormat = new OutputFormat();
      outputFormat.setOmitXMLDeclaration(true);
      outputFormat.setEncoding("utf-16");
      outputFormat.setPreserveSpace(true);
      xmlSerializer = new XMLSerializer(sw, outputFormat);
      xmlSerializer = new XMLSerializer(sw, outputFormat);
      xmlSerializer.serialize(doc);
      return(sw.toString());
    } catch (IOException ioe) {
      return(ioe.getMessage());
    }
  }
}

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


Re: createElementNS() does not work as expected in new versions of Xerces (> 2.0.1)

Posted by Elena Litani <el...@ca.ibm.com>.
Hi Thomas, 

As Sander points out, your code does not set the value of xmlns
attribute.

Just to clarify the difference in behavior for different Xerces
versions: in Xerces 2.0.1 the XMLSerializer was automatically performing
namespace fixup, i.e. adding appropriate namespace declarations. Thus,
the existing namespace decl on the testNode element was modified to
include the namespace of testNode.
In Xerces 2.4.0 this behavior is not available by default. If you want
XMLSerializer to perform namespace fixup, you need either to use DOM L3
[1] or use setNamespace method on XMLSerializer.

[1] http://xml.apache.org/xerces2-j/faq-dom.html#faq-3

Thank you,
-- 
Elena Litani / IBM Toronto

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