You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Gary L Peskin <ga...@firstech.com> on 2001/03/02 08:59:55 UTC

Problem with namespaceURI when schema is present

Xerces developers:

Please forgive me but I'm not too familiar with XML Schemas just yet. 
However, in the Xalan project, I've observed some behavior from Xerces-J
1.3.0 that I do not understand.  If it is a bug, please let me know and
I'll enter it into Bugzilla.  Otherwise, if I'm not understanding this,
please give a short explanation and I'll go away.

I'm using the following java code:
-------------------------------------------------------------------------
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Xproblem1 {
  public static void main(String[] argv) {

    try {
      DocumentBuilderFactory dFactory =
DocumentBuilderFactory.newInstance();
      dFactory.setValidating(true);
      dFactory.setNamespaceAware(true);
      DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
      Document doc = dBuilder.parse("file:///c:/temp/Xproblem1.xml");
      Element docElem = doc.getDocumentElement();
      System.err.println("Element is " + docElem.getLocalName());
      System.err.println("Namespace URI is *" +
docElem.getNamespaceURI() + "*");
    } catch (Exception e) {
        e.printStackTrace();
    } 
  }
}
-------------------------------------------------------------------------

If I use the following file for Xproblem1.xml:
-------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<Class>
  This is my sample text
</Class>
-------------------------------------------------------------------------
then I get the following output:
Element is Class
Namespace URI is *null*

This is what I would expect since there is no default namespace.

However, if I use the following file for Xproblem1.xml:
-------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<Class xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Xproblem1.xsd">
<Class>
  This is my sample text
</Class>
-------------------------------------------------------------------------
and this file for Xproblem1.xsd:
-------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2000/10/XMLSchema">
	<xsd:element name="Class" type="xsd:string"/>
</xsd:schema>
-------------------------------------------------------------------------
then I get the following output:
Element is Class
Namespace URI is **

This indicates that the namespace is one with the empty string.

I don't understand why the example with the schema produces a
namespaceURI with the empty string.  It seems to me like it should also
produce a namespaceURI which is null, indicating the absence of a
namespace.

Please enlighten me.

Thanks,
Gary