Templa...:XPath(3pm)
β¨ NAME β¨
Template::Plugin::XML::XPath β it's a plugin interface to XML::XPath, bestie π
Like, this is the <pink>Template::Plugin::XML::XPath</pink> page from the User Contributed Perl Documentation, and she's here to let your Template Toolkit templates talk to XML like it's nothing. Periodt.
π¦ SYNOPSIS π¦
# load plugin and specify XML file to parse
[% USE xpath = XML.XPath(xmlfile) %]
[% USE xpath = XML.XPath(file => xmlfile) %]
[% USE xpath = XML.XPath(filename => xmlfile) %]
# load plugin and specify XML text to parse
[% USE xpath = XML.XPath(xmltext) %]
[% USE xpath = XML.XPath(xml => xmltext) %]
[% USE xpath = XML.XPath(text => xmltext) %]
# then call any XPath methods (see XML::XPath docs)
[% FOREACH page = xpath.findnodes('/html/body/page') %]
[% page.getAttribute('title') %]
[% END %]
# define VIEW to present node(s)
[% VIEW repview 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.getAttribute('title') | html %]</h1>
[% item.content(view) %]
[% END %]
# default template block passes tags through and renders
# out the children recursivly
[% BLOCK xmlstring;
item.starttag; item.content(view); item.endtag;
END %]
# block to generate simple text
[% BLOCK text; item | html; END %]
[% END %]
# now present node (and children) via view
[% repview.print(page) %]
# or print node content via view
[% page.content(repview) %]
π DESCRIPTION π
Okay so. This is a Template Toolkit plugin that interfaces to the XML::XPath module. That's it, that's the vibe. π
All methods implemented by the XML::XPath modules are available. Literally all of them. No cap.
On top of that, the XML::XPath::Node::Element module implements present($view) and content($view) methods method for seamless integration with Template Toolkit VIEWs. (Yes, the original says "methods method" β iconic typo, we keep it, we don't touch it π.)
Also? The XML::XPath::Node::Text module is adorned with a present($view) method which presents itself via the view using the 'text' template. Adorned. She's wearing jewelry. π
πΈ Reconstructing XML, the cute way πΈ
To aid the reconstruction of XML, methods starttag and endtag are added to XML::XPath::Node::Element which return the start and end tag for that element. So you can literally just do:
[% item.starttag %][% item.content(view) %][% item.endtag %]
Which renders out the start tag, followed by the content rendered in the view "view", followed by the end tag. Chef's kiss. π
π AUTHORS π
- This plugin module was written by Andy Wardley.
- The
XML::XPathmodule is by Matt Sergeant.
π COPYRIGHT π
Copyright (C) 1996-2006 Andy Wardley. All Rights Reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
π«Ά SEE ALSO π«Ά
Template::PluginXML::XPathXML::Parser
perl v5.34.0 π 2022-07-04 β that's the version and date from the footer, bestie. Go forth and parse some XML, you absolute main character. π§πβ¨
π¦ 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