blob: 72c03773be89f11a16a23fae16af9cf31e2ec5bd [file] [log] [blame]
<!doctype html>
<!-- Generated by FreeMarker/Docgen from DocBook -->
<html lang="en" class="page-type-section">
<head prefix="og: http://ogp.me/ns#">
<meta charset="utf-8">
<title>Details - FreeMarker Manual</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="format-detection" content="telephone=no">
<meta property="og:site_name" content="FreeMarker Manual">
<meta property="og:title" content="Details">
<meta property="og:locale" content="en_US">
<meta property="og:url" content="http://example.com/xgui_declarative_details.html">
<link rel="canonical" href="http://example.com/xgui_declarative_details.html">
<link rel="icon" href="favicon.png" type="image/png">
<link rel="stylesheet" type="text/css" href="docgen-resources/docgen.min.css?1594338519184">
</head>
<body itemscope itemtype="https://schema.org/Code">
<meta itemprop="url" content="http://example.com/">
<meta itemprop="name" content="FreeMarker Manual">
<!--[if lte IE 9]>
<div style="background-color: #C00; color: #fff; padding: 12px 24px;">Please use a modern browser to view this website.</div>
<![endif]--><div class="header-top-bg"><div class="site-width header-top"><a class="logo" href="http://example.com" role="banner"> <img itemprop="image" src="logo.png" alt="My Logo">
</a></div></div><div class="header-bottom-bg"><div class="site-width search-row"><a href="index.html" class="navigation-header">FreeMarker Manual</a><div class="navigation-header"></div></div><div class="site-width breadcrumb-row"><ul class="breadcrumb" itemscope itemtype="http://schema.org/BreadcrumbList"><li class="step-0" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="index.html"><span itemprop="name">FreeMarker Manual</span></a></li><li class="step-1" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="xgui.html"><span itemprop="name">XML Processing Guide</span></a></li><li class="step-2" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="xgui_declarative.html"><span itemprop="name">Declarative XML Processing</span></a></li><li class="step-3" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a class="label" itemprop="item" href="xgui_declarative_details.html"><span itemprop="name">Details</span></a></li></ul><div class="bookmarks" title="Bookmarks"><span class="sr-only">Bookmarks:</span><ul class="bookmark-list"><li><a href="alphaidx.html">Index</a></li><li><a href="gloss.html">Glossary</a></li><li><a href="ref.html">Reference</a></li><li><a href="app_faq.html">FAQ</a></li><li><a href="preface.html#test_target">Bőregér</a></li><li><a href="api/index.html">API</a></li><li><a href="../index.html">Home</a></li></ul></div></div></div> <div class="main-content site-width">
<div class="content-wrapper">
<div id="table-of-contents-wrapper" class="col-left">
<script>var breadcrumb = ["FreeMarker Manual","XML Processing Guide","Declarative XML Processing","Details"];</script>
<script src="toc.js?1594338519184"></script>
<script src="docgen-resources/main.min.js?1594338519184"></script>
</div>
<div class="col-right"><div class="page-content"><div class="page-title"><div class="pagers top"><a class="paging-arrow previous" href="xgui_declarative_basics.html"><span>Previous</span></a><a class="paging-arrow next" href="ref.html"><span>Next</span></a></div><div class="title-wrapper">
<h1 class="content-header header-section1" id="xgui_declarative_details" itemprop="headline">Details</h1>
</div></div><div class="page-menu">
<div class="page-menu-title">Page Contents</div>
<ul><li><a class="page-menu-link" href="#autoid_75" data-menu-target="autoid_75">Default handlers</a></li><li><a class="page-menu-link" href="#autoid_76" data-menu-target="autoid_76">Visiting a single node</a></li><li><a class="page-menu-link" href="#autoid_77" data-menu-target="autoid_77">XML namespaces</a></li></ul> </div>
<h2 class="content-header header-section2" id="autoid_75">Default handlers</h2>
<p>For some XML node types, there is a default handler, which
will handle the node if you haven&#39;t defined a handler for the node
(i.e. if there is no user-defined directive available with name
identical to the node name). Here are these node types, and what the
default handler does:</p>
<ul>
<li>
<p>Text node: prints the text as it. Note, that in most
applications, this will not be good for you, because you should
escape the text before you send it to the output (with
<code class="inline-code">?html</code> or <code class="inline-code">?xml</code> or
<code class="inline-code">?rtf</code>, ...etc. depends on the output
format).</p>
</li>
<li>
<p>Processing instruction node: call handler called
<code class="inline-code">@pi</code> if you have created such user-defined
directive, otherwise do nothing (ignore the node).</p>
</li>
<li>
<p>Comment node, document type node: Do nothing (ignore the
node).</p>
</li>
<li>
<p>Document node: Call <code class="inline-code">recurse</code>, that is,
visit all children of the document node.</p>
</li>
</ul>
<p>Element and attribute nodes will be handled according to the
usual, XML independent mechanism. That is,
<code class="inline-code">@<em class="code-color">node_type</em></code> will be
called as handler, and if that&#39;s not defined, then an error stops
template processing.</p>
<p>In the case of element nodes, this means that if you define a
macro (or other kind of user-defined directive) called
<code class="inline-code">@element</code>, that will catch all element nodes,
which has no more specific handler. If you have no
<code class="inline-code">@element</code> handler, then you
<em>must</em> define a handler for all possible
elements.</p>
<p>Attribute nodes are not visited by the
<code class="inline-code">recurse</code> directive, so you don&#39;t need to write
handlers for them.</p>
<h2 class="content-header header-section2" id="autoid_76">Visiting a single node</h2>
<p>With the <a href="ref_directive_visit.html#ref.directive.visit"><code>visit</code>
directive</a> you can visit a single node, instead of the
children of the node:
<code class="inline-code">&lt;#visit <em class="code-color">nodeToVisist</em>&gt;</code>.
This can be useful sometimes.</p>
<h2 class="content-header header-section2" id="autoid_77">XML namespaces</h2>
<p>We said that the name of the handler user-defined directive
(like a macro) for an element is the name of the element. In fact,
it is the full-qualified name of the element:
<code class="inline-code"><em class="code-color">prefix</em>:<em class="code-color">elementName</em></code>.
The rules regarding the usage of
<code class="inline-code"><em class="code-color">prefix</em></code>-es is the same
as with imperative processing. Thus, the user-defined
<code class="inline-code">book</code> directive handles only element
<code class="inline-code">book</code> that does not belong to any XML namespace
(unless you have specified a default XML namespace). So if the
example XML would use XML namespace
<code class="inline-code">http://example.com/ebook</code>:</p>
<div class="code-wrapper"><pre class="code-block code-unspecified">&lt;book xmlns=&quot;http://example.com/ebook&quot;&gt;
<em>...</em></pre></div>
<p>Then the FTL should look as this:</p>
<div class="code-wrapper"><pre class="code-block code-template"><strong>&lt;#ftl ns_prefixes={&quot;e&quot;:&quot;http://example.com/ebook&quot;}&gt;</strong>
&lt;#recurse doc&gt;
&lt;#macro &quot;<strong>e:</strong>book&quot;&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;#recurse .node[&quot;<strong>e:</strong>title&quot;]&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;&lt;#recurse .node[&quot;<strong>e:</strong>title&quot;]&gt;&lt;/h1&gt;
&lt;#recurse&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;/#macro&gt;
&lt;#macro &quot;<strong>e:</strong>chapter&quot;&gt;
&lt;h2&gt;&lt;#recurse .node[&quot;<strong>e:</strong>title&quot;]&gt;&lt;/h2&gt;
&lt;#recurse&gt;
&lt;/#macro&gt;
&lt;#macro &quot;<strong>e:</strong>para&quot;&gt;
&lt;p&gt;&lt;#recurse&gt;
&lt;/#macro&gt;
&lt;#macro &quot;<strong>e:</strong>title&quot;&gt;
&lt;#--
We have handled this element imperatively,
so we do nothing here.
--&gt;
&lt;/#macro&gt;
&lt;#macro @text&gt;${.node?html}&lt;/#macro&gt;</pre></div>
<p>Or, you can define a default XML namespace, and then the
further part of the template remains the same as in the original XML
namespace free example:</p>
<div class="code-wrapper"><pre class="code-block code-template">&lt;#ftl ns_prefixes={&quot;<strong>D</strong>&quot;:&quot;http://example.com/ebook&quot;}&gt;
&lt;#recurse doc&gt;
&lt;#macro book&gt;
<em>...</em></pre></div>
<p>But in this case don&#39;t forge that in XPath expressions (we
didn&#39;t used any in the example) the default XML namespace must be
accessed with an explicit <code class="inline-code">D:</code> since names without
prefix always refer to nodes with no XML namespace in XPath. Also
note that with the same logic as with imperative XML processing, the
name of handlers for elements that has no XML namespace is
<code class="inline-code">N:<em class="code-color">elementName</em></code> if (and
only if) there is a default XML namespace. However, for nodes that
are not of type element (such as text nodes), you never use the
<code class="inline-code">N</code> prefix in the handler name, because those nodes
are free of the idea of XML namespaces. So for example, the handler
for text nodes is always just <code class="inline-code">@text</code>.</p>
<p>For more detailed information, please read <a href="ref_directive_visit.html">the reference of
<code>recurse</code> and <code>visit</code></a>
directives.</p>
<div class="bottom-pagers-wrapper"><div class="pagers bottom"><a class="paging-arrow previous" href="xgui_declarative_basics.html"><span>Previous</span></a><a class="paging-arrow next" href="ref.html"><span>Next</span></a></div></div></div></div> </div>
</div>
<div class="site-footer"><div class="site-width"><div class="footer-top"><div class="col-left sitemap"></div><div class="col-right"><a class="xxe" href="http://www.xmlmind.com/xmleditor/" rel="nofollow" title="Edited with XMLMind XML Editor"><span>Edited with XMLMind XML Editor</span></a></div></div><div class="footer-bottom"> <p class="last-generated">
Last generated:
<time itemprop="dateModified" datetime="2020-07-09T23:48:39Z" title="Thursday, July 9, 2020 11:48:39 PM GMT">2020-07-09 23:48:39 GMT</time> </p>
<p class="copyright">
© <span itemprop="copyrightYear">1999</span>–2020
<a itemtype="http://schema.org/Organization" itemprop="copyrightHolder" href="https://apache.org/">The Apache Software Foundation</a> </p>
</div></div></div></body>
</html>