title: Using Freemarker DocumentModel with namespaces
body: <p>I am being sent an XML document containing namespace information that I need to process. From all I have read in the documentation I should be able to pass the XML document to the template and use XPath in the template to access the document elements.</p>
  
  <p>I am able to do this successfully on a document that doesn't contain namespaces. But as soon as I introduce namespaces I get an exception.  Here's some sample XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &lt;ns:a xmlns:ns="http://stackoverflow.com/"&gt;
    &lt;ns:b&gt;
      &lt;ns:c&gt;Element c&lt;/ns:c&gt;
      &lt;ns:d&gt;
        &lt;ns:e&gt;Element e&lt;/ns:e&gt;
      &lt;/ns:d&gt;
    &lt;/ns:b&gt;
  &lt;/ns:a&gt;
  </code></pre>
  
  <p>And here's my template:</p>
  
  <pre><code>&lt;#ftl ns_prefixes={
    "ns":"http://stackoverflow.com/"}
  &gt;
  
  ${doc.@@markup}
  ${doc["/ns:a/ns:b/ns:c"]}
  ${doc["/ns:a/ns:b/ns:d/ns:e"]}
  </code></pre>
  
  <p>When I apply this template to the XML, I get the following error:</p>
  
  <pre><code>Nov 08, 2015 6:31:41 PM freemarker.log._JULLoggerFactory$JULLogger error
  SEVERE: Error executing FreeMarker template
  FreeMarker template error:
  For "${...}" content: Expected a string or something automatically convertible to string (number, date or boolean), but this has evaluated to a sequence+hash (wrapper: f.e.dom.NodeListModel):
  ==&gt; doc["/ns:a/ns:b/ns:c"]  [in template "freemarker.ftl" at line 6, column 3]
  
  ----
  Tip: This XML query result can't be used as string because for that it had to contain exactly 1 XML node, but it contains 0 nodes. That is, the constructing XML query has found no matches.
  ----
  
  ----
  FTL stack trace ("~" means nesting-related):
      - Failed at: ${doc["/ns:a/ns:b/ns:c"]}  [in template "freemarker.ftl" at line 6, column 1]
  ----
  </code></pre>
  
  <p>I left this out initially, but here is my code that applies the template.</p>
  
  <pre><code>public static void main(String[] args) {
      try {
          File f = new File("c:/temp/freemarker.xml");
          Configuration c = new Configuration();
          c.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
          c.setDefaultEncoding("UTF-8");
          c.setDirectoryForTemplateLoading(new File("c:/temp"));
  
          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
          DocumentBuilder builder = factory.newDocumentBuilder();
          Document doc = builder.parse(f);
          Element note = doc.getDocumentElement();
          NodeModel.simplify(note);
          NodeModel.useJaxenXPathSupport();
          Map&lt;String, Object&gt; root = new HashMap&lt;String, Object&gt;();
          root.put("doc", note);
          Template template = c.getTemplate("freemarker.ftl");
  
          Writer out = new StringWriter();
          template.process(root, out);
  
          System.out.println(out);
      } catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>As I said, this works fine without namespaces. Unfortunately I can't remove the namespace information is it is possible to have a name conflict that would require the namespace to determine which element is being referenced.</p>
  
  <p>Can anyone tell me what I am doing wrong? Or is this possibly a bug?</p>
  
  <p>Thanks!</p>