Recursive XInclusion with XPointer fragment selection fails
Hello!
I have three XML files: "a.xml", "b.xml" and "c.xml". Full XML files are at the end of this message.
1) "c.xml" is simple XML file.
2) "b.xml" does simple XInclusion of "c.xml" as '<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="c.xml" parse="xml" />'.
3) "a.xml" does XInclusion of "b.xml" using XPointer as '<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="b.xml" xpointer="element(/1/1)" parse="xml" />'.
I want to have "a.xml", which contains part of "b.xml" and whole "c.xml". I am not able to do this. I receive "a.xml" containing part of "b.xml" with just reference to "c.xml" (there is not done XInclusion of "c.xml") ?unparsed "b.xml". I can receive "a.xml" with (whole) "b.xml" and "c.xml", but only if I do not use XPointer. What am I doing wrong? I am using JAXP in Java 1.5.0_02-b09. Can You help me? I tested it in oXygen 7.0 (Java-based XML editor) and I received expected (correct) result.
Thank You for answers. Stepan
xml:base and xml:lang were stripped
c.xml:
<?xml version="1.0" encoding="UTF-8"?>
<c>
<element>
<text>ccc</text>
</element>
</c>
b.xml:
<?xml version="1.0" encoding="UTF-8"?>
<b xmlns:xi="http://www.w3.org/2001/XInclude">
<element>
<text>bbb</text>
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="c.xml" parse="xml"/>
</element>
</b>
a_withoutXPointer.xml:
<?xml version="1.0" encoding="UTF-8"?>
<a xmlns:xi="http://www.w3.org/2001/XInclude">
<element>
<text>aaa</text>
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="b.xml" parse="xml" />
</element>
</a>
output_withoutXPointer.xml:
<?xml version="1.0" encoding="UTF-8"?>
<a xmlns:xi="http://www.w3.org/2001/XInclude">
<element>
<text>aaa</text>
<b>
<element>
<text>bbb</text>
<c>
<element>
<text>ccc</text>
</element>
</c>
</element>
</b>
</element>
</a>
a_withXPointer.xml:
<?xml version="1.0" encoding="UTF-8"?>
<a xmlns:xi="http://www.w3.org/2001/XInclude">
<element>
<text>aaa</text>
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="b.xml" parse="xml" xpointer="element(/1/1)" />
</element>
</a>
output_withXPointerCORRECT.xml:
<?xml version="1.0" encoding="UTF-8"?>
<a xmlns:xi="http://www.w3.org/2001/XInclude">
<element>
<text>aaa</text>
<element>
<text>bbb</text>
<c>
<element>
<text>ccc</text>
</element>
</c>
</element>
</element>
</a>
output_withXPointerERROR.xml (by Java 1.5.0_02-b09):
<?xml version="1.0" encoding="UTF-8"?>
<a xmlns:xi="http://www.w3.org/2001/XInclude">
<element>
<text>aaa</text>
<element>
<text>bbb</text>
<xi:include href="c.xml" parse="xml"/>
</element>
</element>
</a>
my Java code:
import java.util.*;
import org.w3c.dom.*;
import java.io.*;
import java.sql.*;
//packages for general XML manipulation
import javax.xml.parsers.*;
//packages for XPath
import javax.xml.xpath.*;
//packages for saving Document
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
//packages for XSD validation
import javax.xml.validation.*;
import javax.xml.XMLConstants;
publicclass examplesOfUse{
publicstaticvoid main(String[] args){
try{
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
documentBuilderFactory.setXIncludeAware(true);
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
// HERE CHANGE FILENAME
Document document = documentBuilder.parse(new File(args[0]));
//a_withXPointer.xml|a_withoutXPointer.xml
ByteArrayOutputStream byteArrayOutputStream =new ByteArrayOutputStream();
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
Source source =new DOMSource(document);
StreamResult streamResult =new StreamResult(byteArrayOutputStream);
transformer.transform(source, streamResult);
//System.out.println("");
System.out.println(byteArrayOutputStream.toString("UTF-8"));
//System.out.println("");
}catch (Exception e){
System.out.println(e.getMessage());
System.out.println(e.getCause());
}//end_try-catch
}//end_main
}

