The DOM tree

We will use this XML document for the examples:

<book>
  <title>Test Book</title>
  <chapter>
    <title>Ch1</title>
    <para>p1.1</para>
    <para>p1.2</para>
    <para>p1.3</para>
  </chapter>
  <chapter>
    <title>Ch2</title>
    <para>p2.1</para>
    <para>p2.2</para>
  </chapter>
</book>  

W3C DOM models an XML document as a tree of nodes. The node tree of the above XML can be visualized as:

document
 |
 +- element book
     |
     +- text "\n  "
     |
     +- element title
     |   |
     |   +- text "Test Book"
     |
     +- text "\n  "
     |
     +- element chapter
     |   |
     |   +- text "\n    "
     |   |
     |   +- element title
     |   |   |
     |   |   +- text "Ch1"
     |   |
     |   +- text "\n    "
     |   |
     |   +- element para     
     |   |   |
     |   |   +- text "p1.1"
     |   |
     |   +- text "\n    "
     |   |
     |   +- element para     
     |   |   |
     |   |   +- text "p1.2"
     |   |
     |   +- text "\n    "
     |   |
     |   +- element para     
     |      |
     |      +- text "p1.3"
     |
     +- element
         |
         +- text "\n    "
         |
         +- element title
         |   |
         |   +- text "Ch2"
         |
         +- text "\n    "
         |
         +- element para     
         |   |
         |   +- text "p2.1"
         |
         +- text "\n    "
         |
         +- element para     
             |
             +- text "p2.2"  

Note that the disturbing "\n  "-s are the line-breaks (indicated here with \n, an escape sequence used in FTL string literals) and the indentation spaces between the tags.

Notes on the DOM related terminology:

The programmer drops the document node of the DOM tree into the FreeMarker data-model, and then the template author can walk the DOM tree using that variable as the starting-point.

The DOM nodes in FTL correspond to node variables. This is a variable type, similarly to type string, number, hash, etc. Node variable type makes it possible for FreeMarker to get the parent node and the child nodes of a node. This is technically required to allow the template author to navigate between the nodes, say, to use the node built-ins or the visit and recurse directives; we will show the usage of these in the further chapters.

FreeMarker Manual -- For FreeMarker 2.3.20
HTML generated: 2013-06-27 20:54:33 GMT
Edited with XMLMind XML Editor
Here!