Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

XInclude fragment (xpointer) from same or separate file using SAX or JAXB

843834Dec 9 2009 — edited Dec 15 2009
I'm trying to use xinclude to duplicate a piece of xml in the same file.
I need this to work using JAXB,
but for this bug test I'm simply using SAX.

If the source file (arg[0]) is
"xmldata/A.xml"
and contains an xinclude of the form
<xi:xinclude href="xmldata/B.xml"/>
(i.e. includes an entire file) it seems to work.

However, if the xinclude is of any of the following forms
(1) <xi:include href="xmldata/B.xml" xpointer="inside-group" />
(2) <xi:include href="xmldata/A.xml" xpointer="inside-group"/>
(3) <xi:include href="" xpointer="inside-group"/>
(4) <xi:include xpointer="inside-group"/>
it fails.
For trying to select something using xpointer and href to file B (case 1),
or xpointer and an implied file of the original file (case 3,4), I get:
Exception in thread "Main Thread" org.xml.sax.SAXParseException:
An 'include' failed, and no 'fallback' element was found.
And for trying to use xpointer and the same source file (A) explicitly (case 2), I get:
Error attempting to parse XML file (href='xmldata/A.xml').

I thought XInclude was supposed to be fully supported,
which would mean both internal references and fragments should work.
I understand the no fallback part, but it shouldn't be failing.
Any clues to what I'm doing wrong would be much appreciated...

Thanks,

Gary

package test;
import java.io.FileReader;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.XMLReader;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.DefaultHandler;
public class TestSaxParser extends DefaultHandler {
public static void main (String args[]) throws Exception {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
spf.setXIncludeAware(true);
XMLReader xr = spf.newSAXParser().getXMLReader();
TestSaxParser handler = new TestSaxParser();
xr.setContentHandler(handler);
xr.setErrorHandler(handler);
FileReader r = new FileReader(args[0]);
xr.parse(new InputSource(r));
}
}

xmldata/A.xml:
<?xml version="1.0" encoding="UTF-8"?>
<test:root xmlns:test="http://www.example.com/A"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xi="http://www.w3.org/2001/XInclude"
xsi:schemaLocation="http://www.example.com/A ../schemas/A.xsd ">
<test:outside>
<test:inside-group name="inside-group" id="inside-group">
<test:group-item>GroupItem</test:group-item>
</test:inside-group>
<!--
none of these work
<xi:include href="xmldata/A.xml" xpointer="inside-group"/>
<xi:include href="" xpointer="inside-group"/>
<xi:include xpointer="inside-group"/>
<xi:include href="xmldata/B.xml" xpointer="inside-group" />
these work
<xi:include href="xmldata/B.xml" />
-->
<xi:include xpointer="inside-group" />
</test:outside>
</test:root>

xmldata/B.xml:
<?xml version="1.0" encoding="UTF-8"?>
<test:root xmlns:test="http://www.example.com/A"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xi="http://www.w3.org/2001/XInclude"
xsi:schemaLocation="http://www.example.com/A ../schemas/A.xsd ">
<test:inside-group name="inside-group" id="inside-group">
<test:group-item>GroupItem</test:group-item>
</test:inside-group>
</test:root>
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 12 2010
Added on Dec 9 2009
1 comment
951 views