title: Saxon: convert xdmNode to org.w3c.dom.Node
body: <p>I would like to convert a xdmNode to a dom Node. I have found the following partial solution:</p>
  
  <pre><code>Processor proc = new Processor(false);
  XPathCompiler xpath = proc.newXPathCompiler();
  Document doc = DocumentUtilities.createEventCountsTestDocument();
  XdmNode xdmNode = proc.newDocumentBuilder().wrap(doc);
  
  expression = "/caseFileModel/EventCount/EventCountEvents[EventCmmnId=\"_4cd9cdaabbd244548a32afcd4cf2e1c0\" and EventEventType=\"CREATE\"]/Timestamps/Date[last()]";
  value = xpath.evaluate(expression, xdmNode);
  XdmNode node = (XdmNode) value;
  Node element = ((Node) node.getExternalNode());
  System.out.println(element.getTextContent());
  </code></pre>
  
  <p>Where <code>createEventCountsTestDocument</code> generates a document based on the following XML:</p>
  
  <pre><code>&lt;caseFileModel&gt;
          &lt;/EventCountEvents&gt;
          &lt;EventCountEvents&gt;
              &lt;EventCmmnId&gt;_8dbe1b3fa1a04787b2ef35d153d93ae5&lt;/EventCmmnId&gt;
              &lt;EventEventType&gt;CREATE&lt;/EventEventType&gt;
              &lt;EventCount&gt;1&lt;/EventCount&gt;
              &lt;Timestamps&gt;
                  &lt;Date&gt;2017-03-13T16:47:04Z&lt;/Date&gt;
              &lt;/Timestamps&gt;
          &lt;/EventCountEvents&gt;
          &lt;EventCountEvents&gt;
              &lt;EventCmmnId&gt;_4cd9cdaabbd244548a32afcd4cf2e1c0&lt;/EventCmmnId&gt;
              &lt;EventEventType&gt;CREATE&lt;/EventEventType&gt;
              &lt;EventCount&gt;1&lt;/EventCount&gt;
              &lt;Timestamps&gt;
                  &lt;Date&gt;2017-03-13T16:47:04Z&lt;/Date&gt;
              &lt;/Timestamps&gt;
          &lt;/EventCountEvents&gt;
      &lt;/EventCount&gt;
  &lt;/caseFileModel&gt;
  </code></pre>
  
  <p>I get the correct output namely <code>2017-03-03T12:00:00Z</code> however <code>element</code> is a <code>com.sun.org.apache.xerces.internal.dom.DeferredElementImpl</code> and not a <code>w3c.dom.Node</code>. This means when I call <code>element.getNodeValue()</code> it returns <code>null</code>. If I call <code>element.getFirstChild().getNodeValue()</code> it does return the correct value <code>2017-03-03T12:00:00Z</code>. So there seems to be something wrong but I can not find it. I would like to find a way to convert the <code>xdmNode</code> to a <code>w3c.dom.Node</code> and not to a <code>DeferredElementImpl</code>.</p>