[jdom-interest] Getting schema validation working without enabling Xerces namespace support. How?

Brett McLaughlin brett.mclaughlin at lutris.com
Wed Sep 13 08:47:39 PDT 2000

Travers Waker wrote:
> Hi everyone.
> Lets finally settle this thread.

OK. I can help ;-)

> This is an extract from the September archives of the jdom-interest mailing
> list.
> >> I also struggled with namespaces and I found that in SAXBuilder
> >> the fact
> >> that the "namespaces" feature is hardwired to false prevents
> >> correct handling of schemas. Setting the property to true seemed to
> >> solve
> >> the problem.
> >>
> >> This is an issue that I brought to the list but no one answered.
> >> Why is this property hardwired to false ?
> >Because JDOM does its own namespace handling, which allows namespaces
> >and validation to work at the same time.
> >-jh-
> I might be wrong, but I get the impression that Jason's reply implies that
> schema validation still works when Xerces namespace handling is disabled
> because JDOM does its own namespace handling.  However, this is definitely
> not my experience (the schema is ignored when Xerces namespace handling is
> disabled) .  So, how does one get schema validation working without enabling
> Xerces namespace handling?

Well, not neccessarily. Xerces uses its internals to do XML Schema 
validation, and thus the line of communication is one way. In other
words, JDOM cannot process namespaces (in our way) and then relay that
information /back/ to Xerces. So Xerces needs the SAX namespaces
processing feature on. However, until now, JDOM turned that off, because
of a lot of details that are too long to type ;-) Seriously, it was
easier at the time to handle namespaces ourselves, extracting them from
the qName of an element. However, it appears that the time has come to
change this. Both because of XML Schema processing/validation, as well
as the need to capture multiple namespace declarations on an Element.

But this is a big deal - and it will take some refactoring of both
builders, and won't be quick. I'll try to get to it over the next few
days, but fair warning - I am /swamped/. Now for those of you wondering,
we will /not/ expose that feature for people to set off and on;
SAXBuilder relies on it being one way, and I'm not going to bog down all
that code with if/else statements in each callback to allow a user to
set it on or off. Besides, when I turn it on and make these changes, I
can think of no compelling reason for someone to want to turn it off.

So that's not a resolution, because the code isn't done, but I hope it's
an explanation and a satisfactory game plan.



> In particular, how do I get the very simple example code in the email
> attached below to work without first enabling Xerces namespace handling?
> ------------- Email sent to Scott Ellsworth on 12 September ---------------
> Hi Scott.
> I've got the Schemas working with JDOM.  The main problem was that JDOM
> defaults to disabling namespace support in Xerces, which breaks schema
> support.  You just have to go into the source file
> jdom/src/java/org/jdom/input/SAXBuilder.java and edit the line that looks
> like this:
>   parser.setFeature("http://xml.org/sax/features/namespaces", false);
> and change it to:
>   parser.setFeature("http://xml.org/sax/features/namespaces", true);
> Then rerun JDOM's Build script to get Ant to include your change into the
> new jdom.jar, and copy the jdom.jar to wherever you keep your jar libraries.
> After that, the following simple example works fine, throwing an exception
> because validation by the schema fails.
> File: test.xml
> ----------------------------------------------------------------------------
> <?xml version="1.0" encoding="UTF-8"?>
> <tag xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
>     xsi:noNamespaceSchemaLocation='test.xsd'>1</tag>
> ----------------------------------------------------------------------------
> File: test.xsd
> ----------------------------------------------------------------------------
> <?xml version="1.0" encoding="UTF-8"?>
> <xsd:schema xmlns:xsd="http://www.w3.org/1999/XMLSchema">
> <xsd:simpleType name="LimitedInt" base="xsd:integer">
>   <xsd:minInclusive value="2"/>
> </xsd:simpleType>
> <xsd:element name="tag" type="LimitedInt"/>
> </xsd:schema>
> ----------------------------------------------------------------------------
> File: JdomTest.java
> ----------------------------------------------------------------------------
> package tests.jdom;
> import java.io.File;
> import org.jdom.*;
> import org.jdom.input.*;
> class JdomTest {
>   public static void main(String[] args) throws Exception {
>     SAXBuilder builder = new SAXBuilder(true);
>     Document doc = builder.build(new File("VagasConfig.xml
>     Element currentElement = doc.getRootElement();
>     String content = currentElement.getContent();
>     System.out.println("The <tag> field contains: " + content);
>   }
> }
> ----------------------------------------------------------------------------
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com

Brett McLaughlin, Enhydra Strategist
Lutris Technologies, Inc. 
1200 Pacific Avenue, Suite 300 
Santa Cruz, CA 95060 USA 

More information about the jdom-interest mailing list