freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Per Olsson (JIRA)" <>
Subject [jira] [Created] (FREEMARKER-41) XPathSupport executeQuery doesn't handle text() in models that isn't normalized
Date Wed, 30 Nov 2016 10:43:00 GMT
Per Olsson created FREEMARKER-41:

             Summary: XPathSupport executeQuery doesn't handle text() in models that isn't
                 Key: FREEMARKER-41
             Project: Apache Freemarker
          Issue Type: Bug
          Components: engine
    Affects Versions: 2.3.25-incubating
            Reporter: Per Olsson

XPath expressions that contains text() doesn't evaluate to the correct value when the model
isn't normalized and includes multiple text nodes. This will happen when the xml-parser creates
multiple text nodes due to performance or memory reasons and is a fully normal behaviour.

The solution in the function executeQuery with the NodeIterator (files: freemarker/ext/dom/
and freemarker/ext/dom/ doesn't handle adjacent(siblings) textnodes.
The problem probably also exists for CDATA nodes. I don't know if the jaxen solution behaves
in the same manner. 

synchronized public TemplateModel executeQuery(Object context, String xpathQuery) throws TemplateModelException
  NodeIterator nodeIterator = xresult.nodeset();
  Node n;
  do {
      n = nodeIterator.nextNode();
      if (n != null) {
  } while (n != null);
Sample code to reproduce
    Configuration cfg = new Configuration(Configuration.VERSION_2_3_25);
    ClassTemplateLoader ctl = new ClassTemplateLoader(App.class, "/");

    // --- sample.ftl ---
    // <#ftl>
    // Text:${model["//root/text()"]}
    // Node:${model["//root"]}
    Template temp = cfg.getTemplate("sample.ftl");

    // --- Model --- 
    // Element:root
    //     |- TextNode:SA
    //     |- TextNode:MPLE

    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.newDocument();
    Element rootElement = doc.createElement("root");
    Text text = doc.createTextNode("SA");
    text = doc.createTextNode("MPLE");

    Map<String, Object> model = new HashMap<String, Object>();
    model.put("model", doc);
    StringWriter sw = new StringWriter();
    temp.process(model, sw);

    // --- Actual output ---
    // Text:SA
    // Node:SAMPLE

    // --- Expected output ---
    // Text:SAMPLE
    // Node:SAMPLE 

This message was sent by Atlassian JIRA

View raw message