title: XInclude support in Java 6
body: <p>I've seen this example posted several times:</p>
  
  <pre><code>public class XIncludeTest {
  
    public static void main(String[] args) throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.setXIncludeAware(true);
        DocumentBuilder docBuilder = factory.newDocumentBuilder();
        System.out.println("isXIncludeAware " + docBuilder.isXIncludeAware());
        Document doc = docBuilder.parse(args[0]);
  
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  
        //initialize StreamResult with File object to save to file
        StreamResult result = new StreamResult(new StringWriter());
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);
  
        String xmlString = result.getWriter().toString();
        System.out.println(xmlString);    
      }
  }
  </code></pre>
  
  <p>I pass this simple xml file in:</p>
  
  <pre><code>a.xml
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;a xmlns:xi="http://www.w3.org/2003/XInclude"&gt;
    &lt;xi:include href="b.xml" xpointer="element(/b/c)"/&gt;
    &lt;xi:include href="b.xml" xpointer="element(/b/d)"/&gt;
  &lt;/a&gt;
  
  b.xml
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;b&gt;
    &lt;c&gt;1&lt;/c&gt;
    &lt;d&gt;2&lt;/d&gt;
  &lt;/b&gt;
  </code></pre>
  
  <p>And all I get back out is the contents of a.xml, as above - no part of b.xml was included.  I've tried endless variations on the xpointer syntax to no avail.  I have, however, been able to get things to work in perl via XML::LibXML but I need this to work in Java.</p>
  
  <p>What am I not getting?</p>
  
  <p>OK, now I've updated my xml file to something that works:</p>
  
  <pre><code>a.xml
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;a xmlns:xi="http://www.w3.org/2001/XInclude"&gt;
    &lt;xi:include href="b.xml" xpointer="element(/1/1)"/&gt;
    &lt;xi:include href="b.xml" xpointer="element(/1/2)"/&gt;
  &lt;/a&gt;
  </code></pre>
  
  <p>I'd rather not use offsets into the document - names are much better (like in the first version of a.xml).  I'm trying to understand <a href="http://www.w3.org/TR/2003/REC-xptr-framework-20030325" rel="nofollow">XPointer Framework</a> and have been using <a href="http://commons.oreilly.com/wiki/index.php/XPath_and_XPointer/XPointer_Syntax" rel="nofollow">XPath and XPointer/XPointer Syntax</a> as a reference as well - it seems I should be able to use a "shorthand" pointer but only if the NCName matches some ID-type attribute.  Problem is that I don't have a DTD or schema that defines an "ID-type attribute".  Given that the Java parser doesn't support the xpointer schema, is my only option to use indexes?</p>