Writing templates is very simple. The basic template definition uses the directive
|
|
|
|
#match(<XPath Expression >)
< template content >
#end
|
|
|
|
|
This directive declares that when a node matches the <XPath Expression >
then the body of the directive is to be rendered to the ouput.
In the body, you would place any static content you wish to go to
the output, and also get data from the XML document you are working in.
Future plans include a #name() directive, and optional arguments to the #match()
and #name() directives to support modes and namsepaces.
The Node API
During processing, when a match occurs and a template is invoked, a few objects are placed in the
Context for you to access. These elements are read-only and cannot be modified via #set().
The most important is the current Node, which is accessible via the reference
$node
. The $node
is the current node that matched
the template XPath expression (or name).
You can use the $node object two ways. First, you can use the Velocity property formalism to access
child nodes in the document tree rooted at the current node. So with a document such as :
|
|
|
|
<?xml version="1.0"?>
<document>
<section name="first">
<p>
<code>foo</code>
</p>
<p>
<code>bar</code>
</p>
</section>
</document>
|
|
|
|
|
you could use references like
$node.section.p.code
to access elements and atributes in the subtree,
assuming that $node was the 'document' element.
Further, you can of course call methods on the
sub-elements :
As for methods, $node
has the following API
for you to use :
Reference
|
Methods
|
Description
|
$node
|
|
Current node for this template match.
|
|
$node.name()
|
Element name of node - ex 'table'
|
|
$node.value()
|
Text content of node
|
|
$node.attrib("name")
|
Returns attribute of node if appropriate.
|
|
$node.selectNodes(xpathexpr)
|
Returns an iteratable list of nodes that satisfy the XPath expression
|
|
$node.selectSingleNode(xpathexpr)
|
Returns the first node that satisfies the XPath expression
|
|
$node.get(xpathexpr)
|
Returns the first node that satisfies the XPath expression
|
|
$node.children()
|
Returns a List of all children of this node
|
|
$node.copy()
|
Does a 'deep copy' of this node's subtree to the output
|
|
$node.copy(List)
|
Does a 'deep copy' of the specified nodelist to the output.
|
|
$node.valueOf(xpathexpr)
|
Returns the result of the specified XPath as a Object
|
The next node-specific reference is $attrib
. This
corresponds to a collection of the current nodes attributes (if appropriate - for
example, an attribute doesn't itself have attributes) which you can use
to quickly get the values using Velocity's property reference formalism.
So with the example above, if you matched the 'section' node
you could find the value of the 'name' attribute via
Which would return 'first'.
The final reference available is $context
. This object offers the
following API :
Reference
|
Methods
|
Description
|
$context
|
|
Utility context.
|
|
$context.applyTemplates()
|
Applies all templates in the subtree against the ruleset.
|
|
$context.applyTemplates(xpathexpr)
|
Applies templates to all nodes in the subtree
that match the given XPath expression. Ex. $context.applyTemplates("*|@*")
|
|
$context.applyTemplates(Node)
|
Applies templates to the specified node. First match is applied.
|
|
$context.applyTemplates(Node, xpathexpr)
|
Applies the XPath expression to the specified node, and applies
templates to the resulting nodeset.
|
|
$context.applyTemplates(Node, xpathexpr)
|
Applies the XPath expression to the specified node, and applies
templates to the resulting nodeset.
|
|
$context.getAppValue(Object key)
|
Returns the application value for this key. For example, Ant will
place the current input file name under the key "infilename".
|