[jdom-interest] JDOM, Xerces and Crimson with namepsaces
Wouter Cordewiner
wcordewiner at hotmail.com
Wed Dec 5 02:27:05 PST 2001
Hi,
I still have one question, just to understand the namespace spec correctly.
If I'm not mistaking, the problem with the original XML was that the "ns1:el1" element had a default namepsace declaration, and this collided with the "ns1" prefix.
The XML spec states that a default namespace applies to the element where it is declared if that element has no namepsace prefix. But in this case, the element has a prefix "ns1".
So, shouldn't the declared default namespace only apply to child XML elements of "ns1:el1" which do not have a prefix?
Regards,
Wouter Cordewiner
----- Original Message -----
From: Eric A. Sirois
To: Wouter Cordewiner
Cc: jdom-interest at jdom.org
Sent: Wednesday, November 28, 2001 7:24 PM
Subject: Re: [jdom-interest] JDOM, Xerces and Crimson with namepsaces
Hello,
In your example the root element belongs to the "ns1" namespace, not the no namespace. If you want to have the root element to belong to the no namespace it cannot have a xmlns attribute declaration.
XML Namespace rec:
<!-- snip -->
5.1 Namespace Scoping
The namespace declaration is considered to apply to the element where it is specified and to all elements within the content of that element, unless overridden by another namespace declaration with the same NSAttName part
Multiple namespace prefixes can be declared as attributes of a single element
5.2 Namespace Defaulting
A default namespace is considered to apply to the element where it is declared (if that element has no namespace prefix), and to all elements with no prefix within the content of that element. If the URI reference in a default namespace declaration is empty, then unprefixed elements in the scope of the declaration are not considered to be in any namespace. Note that default namespaces do not apply directly to attributes.
<!-- snip -->
From your original note:
"When I run the NSTest class using the Crimson parser, the file gets loaded.
If I use the Xerces parser, I get following exception:
org.jdom.JDOMException: Error in building from stream: The namespace
xmlns:="http://namespace/ns" could not be added as content to "ns1:el1": The
namespace prefix "" collides with an attribute namespace on the element "
In this case Xerces is right and Crimson is wrong.
"If I'm interpreting the XML spec correctly, you can start declaring a default namespace (one without prefix) anywhere in the XML document. If another one already declared somewhere up the parent tree, it will override it."
True, but only for that element and any child elements. The namespace will no apply to parent elements. If you want the root element to belong to the default namespace, it must declared at the root element.
Kind regards,
Eric
----- Original Message -----
From: Wouter Cordewiner
To: Eric A. Sirois
Cc: jdom-interest at jdom.org
Sent: Wednesday, November 28, 2001 11:54 AM
Subject: Re: [jdom-interest] JDOM, Xerces and Crimson with namepsaces
Hi,
with the example you provide below, the root element will belong to the 'ns1' namespace, while in my example the root element belongs to no namespace.
If I'm interpreting the XML spec correctly, you can start declaring a default namespace (one without prefix) anywhere in the XML document. If another one already declared somewhere up the parent tree, it will override it.
Anyway, my test xml example was just to illustrate that when loading the XML through JDOM using the Xerces parser it fails, while using the Crimson it doesn't.
In my humble opinion, it seems like something is not consistent.
Regards,
Wouter Cordewiner
----- Original Message -----
From: Eric A. Sirois
To: Wouter Cordewiner
Cc: jdom-interest at jdom.org
Sent: Tuesday, November 27, 2001 11:41 PM
Subject: Re: [jdom-interest] JDOM, Xerces and Crimson with namepsaces
Hello,
The example below should work for you. For element "el1" you assigned it the default namespace "http://namespace/ns" but added the prefix for the ns1 prefix. The error message you received is correct. It's saying I don't understand the prefix "ns1" for element "el1" you just assigned it with a default namespace.When you declare/assign a namespace it is associated with the current element and any child element not including attributes.
<?xml version="1.0" encoding="UTF-8" ?><ns1:root xmlns:ns1="http://namespace/ns1"> <el1 xmlns="http://namespace/ns" att1="Attribute 1" /></ns1:root>
namespace element
ns1 root
default el1
If you need the XML as-is try
<?xml version="1.0" encoding="UTF-8" ?><root xmlns="http://namespace/ns"> <ns1:el1 xmlns:ns1="http://namespace/ns1" att1="Attribute 1" /></root>
HTH,
Eric
Wouter Cordewiner wrote:
Hi,I encountered an issue that seems to depend on the XML parser I use.Below a reproducable (XML file and Java code) to illustrate the issue Iencounter:File "ns.xml":<?xml version="1.0" encoding="UTF-8" ?><root xmlns:ns1="http://namespace/ns1"> <ns1:el1 xmlns="http://namespace/ns" att1="Attribute 1" /></root>Java code:public class NSTest { public static void main(String[] args) { try { // Create JDOM tree. org.jdom.input.DOMBuilder db = new org.jdom.input.DOMBuilder(); org.jdom.Document doc = db.build ( new java.io.File ( "ns.xml" ) ); } catch ( Throwable ex ) { ex.printStackTrace(); } } private NSTest() {}}When I run the NSTest class using the Crimson parser, the file gets loaded.If I use the Xerces parser, I get following exception:org.jdom.JDOMException: Error in building from stream: The namespacexmlns:="http://namespace/ns" could not be added as content to "ns1:el1": Thenamespace prefix "" collides with an attribute namespace on the element at org.jdom.input.DOMBuilder.build(DOMBuilder.java:279) at org.jdom.input.DOMBuilder.build(DOMBuilder.java:300) at NSTest.main(NSTest.java:7)Root cause: org.jdom.IllegalAddException: The namespacexmlns:="http://namespace/ns" could not be added as content to "ns1:el1": Thenamespace prefix "" collides with an attribute namespace on the element at org.jdom.Element.addNamespaceDeclaration(Element.java:391) at org.jdom.input.DOMBuilder.buildTree(DOMBuilder.java:417) at org.jdom.input.DOMBuilder.buildTree(DOMBuilder.java:459) at org.jdom.input.DOMBuilder.buildTree(DOMBuilder.java:377) at org.jdom.input.DOMBuilder.build(DOMBuilder.java:262) at org.jdom.input.DOMBuilder.build(DOMBuilder.java:300) at NSTest.main(NSTest.java:7)I looked into it, and I think the problem lies in the difference between theCrimson and Xerces parser on the implementation of the NamedNodeMapinterface of both parsers.The NamedNodeMap object returned from the getAttributes() method on thens1:el element is causing the problem.If you call the item(int) method on the NamedNodeMap object, Xerces andCrimson return the attributes in a different order.Any ideas/suggestions on how to solve this?Thank you,Wouter Cordewiner_______________________________________________To control your jdom-interest membership:http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20011205/049d80ca/attachment.htm
More information about the jdom-interest
mailing list