Templa...L::DOM(3pm)
β¨ NAME β¨
Template::Plugin::XML::DOM β bestie, it's the plugin interface to XML::DOM π
okay so real quick, the vibe check: this is a User Contributed Perl Documentation page, section 3pm, and it's literally the little bridge that lets your Template Toolkit templates talk directly to XML::DOM. iconic behaviour fr fr π
πΈ SYNOPSIS πΈ
# load plugin
[% USE dom = XML.DOM %]
# also provide XML::Parser options
[% USE dom = XML.DOM(ProtocolEncoding = 'ISO-8859-1') %]
# parse an XML file
[% doc = dom.parse(filename) %]
[% doc = dom.parse(file = filename) %]
# parse XML text
[% doc = dom.parse(xmltext) %]
[% doc = dom.parse(text = xmltext) %]
# call any XML::DOM methods on document/element nodes
[% FOREACH node = doc.getElementsByTagName('report') %]
* [% node.getAttribute('title') %] # or [% node.title %]
[% END %]
# define VIEW to present node(s)
[% VIEW report notfound='xmlstring' %]
# handler block for a <report>...</report> element
[% BLOCK report %]
[% item.content(view) %]
[% END %]
# handler block for a <section title="...">...</section> element
[% BLOCK section %]
<h1>[% item.title %]</h1>
[% item.content(view) %]
[% END %]
# default template block converts item to string
[% BLOCK xmlstring; item.toString; END %]
# block to generate simple text
[% BLOCK text; item; END %]
[% END %]
# now present node (and children) via view
[% report.print(node) %]
# or print node content via view
[% node.content(report) %]
# following methods are soon to be deprecated in favour of views
[% node.toTemplate %]
[% node.childrenToTemplate %]
[% node.allChildrenToTemplate %]
π¦ DESCRIPTION π¦
So like, this is a Template Toolkit plugin that interfaces to the XML::DOM module. It loads XML::DOM and creates an XML::DOM::Parser object, which it keeps stored internally, no big deal πββοΈ. After that you can just call the parse() method on the plugin and it will parse an XML stream into a DOM document. ate.
[% USE dom = XML.DOM %]
[% doc = dom.parse('/tmp/myxmlfile') %]
The XML::DOM plugin object (that's 'dom' in these examples, keep up π) acts as a sentinel for the documents it creates ('doc' and any others). The way that it works is: when the plugin object goes out of scope at the end of the current template, it automatically calls dispose() on any documents it made. so it cleans up after itself, which honestly? main character energy π
β οΈ But listen β if you dispose of the plugin object before the end of the block (like, by assigning a new value to the 'dom' variable), then the documents get disposed right then and there, and you should not use them after that. not even a little. π
[% USE dom = XML.DOM %]
[% doc = dom.parse('/tmp/myfile') %]
[% dom = 'new value' %] # releases XML.DOM plugin and calls
# dispose() on 'doc', so don't use it!
The plugin constructor will also accept configuration options destined for the XML::Parser object, because of course it will π
:
[% USE dom = XML.DOM(ProtocolEncoding = 'ISO-8859-1') %]
π METHODS π
β¨ parse() β¨
The parse() method accepts a positional parameter which contains a filename or an XML string. It's assumed to be a filename unless it contains a < character β that's the whole tell, iykyk.
[% xmlfile = '/tmp/foo.xml' %]
[% doc = dom.parse(xmlfile) %]
[% xmltext = BLOCK %]
<xml>
<blah><etc/></blah>
...
</xml>
[% END %]
[% doc = dom.parse(xmltext) %]
The named parameters 'file' (or 'filename') and 'text' (or 'xml') can also be used, if you're a keyword-arguments kind of girlie:
[% doc = dom.parse(file = xmlfile) %]
[% doc = dom.parse(text = xmltext) %]
The parse() method returns an instance of the XML::DOM::Document object representing the parsed document in DOM form. From there you can call any XML::DOM methods on the document node and on any other nodes that its methods hand back to you. See XML::DOM for the full tea. π₯
[% FOREACH node = doc.getElementsByTagName('CODEBASE') %]
* [% node.getAttribute('href') %]
[% END %]
This plugin also provides an AUTOLOAD method for XML::DOM::Node which calls getAttribute() for any undefined methods. So you can use the short form of
[% node.attrib %]
in place of
[% node.getAttribute('attrib') %]
short and sweet, we love a lazy-girl abbreviation π«Ά
π PRESENTING DOM NODES USING VIEWS π
You can define a VIEW to present all or part of a DOM tree by automatically mapping elements onto templates. Lowkey genius. Consider a source document like the following:
<report>
<section title="Introduction">
<p>
Blah blah.
<ul>
<li>Item 1</li>
<li>item 2</li>
</ul>
</p>
</section>
<section title="The Gory Details">
...
</section>
</report>
We can load it up via the XML::DOM plugin and fetch the node for the <report> element.
[% USE dom = XML.DOM;
doc = dom.parse(file = filename);
report = doc.getElementsByTagName('report')
%]
We can then define a VIEW as follows to present this document fragment in a particular way. The Template::Manual::Views documentation contains further details on the VIEW directive and the various configuration options it supports β go read it, bestie, it's giving homework but make it useful π.
[% VIEW report_view notfound='xmlstring' %]
# handler block for a <report>...</report> element
[% BLOCK report %]
[% item.content(view) %]
[% END %]
# handler block for a <section title="...">...</section> element
[% BLOCK section %]
<h1>[% item.title %]</h1>
[% item.content(view) %]
[% END %]
# default template block converts item to string representation
[% BLOCK xmlstring; item.toString; END %]
# block to generate simple text
[% BLOCK text; item; END %]
[% END %]
Each BLOCK defined within the VIEW represents a presentation style for a particular element or elements. The current node is available via the 'item' variable. Elements that contain other content can generate it according to the current view by calling [% item.content(view) %]. Elements that don't have a specific template defined are mapped to the 'xmlstring' template via the 'notfound' parameter specified in the VIEW header. This replicates the node as an XML string, effectively allowing general XML/XHTML markup to be passed through unmodified. no cap. β¨
To present the report node via the view, we simply call:
[% report_view.print(report) %]
The output from the above example would look something like this:
<h1>Introduction</h1>
<p>
Blah blah.
<ul>
<li>Item 1</li>
<li>item 2</li>
</ul>
</p>
<h1>The Gory Details</h1>
...
To print just the content of the report node (i.e. don't process the 'report' template for the report node), you can call:
[% report.content(report_view) %]
π AUTHORS π
This plugin module was written by Andy Wardley and Simon Matthews. legends only. π«Ά
The XML::DOM module is by Enno Derksen and Clark Cooper. It extends the the XML::Parser module, also by Clark Cooper, which itself is built on James Clark's expat library. it's giving family tree, and honestly? obsessed π³
π COPYRIGHT π
Copyright (C) 2000-2006 Andy Wardley, Simon Matthews.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. so like, go off, share the love π
β SEE ALSO β
Template::PluginXML::DOMXML::Parser
and that's the page, bestie!! πβ¨ this little writeup is from perl v5.34.0, dated 2022-07-04, and it's filed under Template::Plugin::XML::DOM(3pm). mwah π go parse something gorgeous. π¦
π¦ where this page comes from
- debian bookworm: libtemplate-plugin-xml-perl 2.17-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libtemplate-plugin-xml-perl 2.17-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libtemplate-plugin-xml-perl 2.17-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libtemplate-plugin-xml-perl 2.17-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libtemplate-plugin-xml-perl 2.17-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms