Hi Peter,

This is happening because the parser you created is not namespace aware. 
Try setting namespace awareness [1] to true on the factory and it should 
work.

Thanks.

[1] 
http://xml.apache.org/xerces2-j/javadocs/api/javax/xml/parsers/DocumentBuilderFactory.html#setNamespaceAware(boolean)

Peter Pimley <[EMAIL PROTECTED]> wrote on 06/27/2005 06:58:41 AM:

> 
> Hi everyone,
> 
> I'm using JAXP 1.3 (by default since I'm using Java 1.5), using Xerces 
> as my parser.  I'm loading a Document that contains XInclude tags, which 

> I'd like to be resolved as the document is loaded.  I can't get this to 
> happen though.  When I've loaded the document it still contains the 
> XInlude Element objects.  I've called setXInluceAware(true) on my 
> DocumentBuilderFactory, and checked isXIncludeAware on my 
> DocumentBuilder, and it returns true.
> 
> I'm confident that my input documents are correct, since 'xmllint 
> --xinclude' on the command line works as I'd expect it to.  xmllint uses 

> libxml2 afaik.  I've tried this on Xerces 2.6.2, and I've just tried it 
> on the newly released 2.7.0 and the results are the same.
> 
> JAXP has always just 'done the right thing' for me up to now, so I'm not 

> familiar with the internals of the parsing process.  I'm about to 
> download the source and add prints to trace what's going on, but before 
> I do that I'd like to see if anybody can spot any basic errors I'm 
> making.  I've made a minimal test program to show what's happening, and 
> included the output.  The program loads a Document, then recursively 
> prints the element tags.
> 
> Thanks in advance,
> Peter Pimley
> 
> 
> 
> ----  Test.java ----
> 
> import javax.xml.parsers.*;
> import org.w3c.dom.*;
> 
> class Test {
> 
>     public static void main (String [] args) throws Exception {
>         String filename = args[0];
>         System.err.println ("Loading from "+filename);
>         Document doc = load (filename);
>         recursivePrint (doc.getDocumentElement());
>     }
> 
>     private static Document load (String filename) throws Exception {
>         // create a factory
>         DocumentBuilderFactory factory = 
> DocumentBuilderFactory.newInstance();
>         System.err.println ("Factory is of type "+factory.getClass());
>         factory.setXIncludeAware (true);
>         System.err.println ("XIncludeAware is 
"+factory.isXIncludeAware());
> 
>         // create a builder
>         DocumentBuilder builder = factory.newDocumentBuilder ();
>         System.err.println ("Builder created of type 
"+builder.getClass());
>         System.err.println ("Builder XInlcude aware is 
> "+builder.isXIncludeAware());
> 
>         System.err.println ("Parsing...");
>         Document doc = builder.parse (filename);
>         System.err.println ("Parsed.");
>         return doc;
>     }
> 
> 
>     private static void recursivePrint (Element e) {
>         String name = e.getNodeName();
>         System.err.println ("Start element "+name);
>         NodeList childNodes = e.getChildNodes();
>         for (int i=0; i<childNodes.getLength(); i++) {
>             Node n = childNodes.item(i);
>             if (n.getNodeType() != Node.ELEMENT_NODE) continue;
>             recursivePrint ((Element) n);
>         }
>         System.err.println ("End element "+name);
>     }
> 
> }
> 
> ---- a.xml ----
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <book xmlns:xi="http://www.w3.org/2001/XInclude";>
>     <chapter id="1">This text is part of the main document</chapter>
>     <xi:include href="b.xml"/>
> </book>
> 
> 
> 
> ---- b.xml ----
> 
> <chapter id="2">This text is part of the included document</chapter>
> 
> 
> ---- command line output (Debian sarge, Java 1.5, Xerces 2.7.0) ----
> 
> [EMAIL PROTECTED]:preprocessor$ java -version
> java version "1.5.0_01"
> Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_01-b08)
> Java HotSpot(TM) Client VM (build 1.5.0_01-b08, mixed mode, sharing)
> 
> [EMAIL PROTECTED]:preprocessor$ xmllint --xinclude a.xml
> <?xml version="1.0" encoding="UTF-8"?>
> <book xmlns:xi="http://www.w3.org/2001/XInclude";>
>     <chapter id="1">This text is part of the main document</chapter>
>     <chapter id="2">This text is part of the included document</chapter>
> </book>
> 
> [EMAIL PROTECTED]:preprocessor$ java -Djaxp.debug=1 
> -Djavax.xml.parsers.DocumentBuilderFactory=org.apache.xerces.jaxp.
> DocumentBuilderFactoryImpl 
> -cp .:xerces-2.7.0.jar Test a.xml
> Loading from a.xml
> JAXP: find factoryId =javax.xml.parsers.DocumentBuilderFactory
> JAXP: found system property, 
> value=org.apache.xerces.jaxp.DocumentBuilderFactoryImpl
> JAXP: created new instance of class 
> org.apache.xerces.jaxp.DocumentBuilderFactoryImpl using ClassLoader: 
> [EMAIL PROTECTED]
> Factory is of type class 
org.apache.xerces.jaxp.DocumentBuilderFactoryImpl
> XIncludeAware is true
> Builder created of type class org.apache.xerces.jaxp.DocumentBuilderImpl
> Builder XInlcude aware is true
> Parsing...
> Parsed.
> Start element book
> Start element chapter
> End element chapter
> Start element xi:include    <------- The include tag has not been 
> resolved :(
> End element xi:include
> End element book
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: [EMAIL PROTECTED]
E-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to