HTML::Template::XPath - Easy access to XML files from HTML::Template using XPath |
HTML::Template::XPath - Easy access to XML files from HTML::Template using XPath
In your perl code:
my $xpt = new HTML::Template::XPath(default_lang => 'en', root_dir => $root_dir relaxed_parser => 'yes');
my $output = $xpt->process(xpt_filename => $xpt_filename, xml_filename => $xml_filename, lang => 'en');
# hash references containing filenames and mtimes, used by PageKit for # caching my $file_mtimes = $xpt->file_mtimes;
Your XPath template:
Header <CONTENT_VAR NAME="document('foo.xml')/aaa/bbb"> <CONTENT_VAR NAME="/ddd/aaa/@bbb"> <CONTENT_VAR NAME="/ddd/eee"> <CONTENT_VAR NAME="eee"> <CONTENT_LOOP NAME="/ddd/fff"> <CONTENT_VAR NAME="@ttt"> <CONTENT_VAR NAME="ggg"> <CONTENT_VAR NAME="hhh"> <CONTENT_VAR NAME="hhh/@qqq"> <CONTENT_VAR NAME="iii"> </CONTENT_LOOP> Footer
Your XML file:
<ddd> <aaa bbb="ccc"/> <eee>jjj</eee> <fff ttt="uuu"> <ggg>sss</ggg> <hhh qqq="rrr">lll</hhh> <iii>mmm</iii> </fff> <fff ttt="vvv"> <ggg>nnn</ggg> <hhh>ooo</hhh> <iii>ppp</iii> </fff> </ddd>
Second XML file (foo.xml)
<aaa> <bbb>Content from second XML file</bbb> </aaa>
Output:
Header Content from second XML file ccc jjj jjj
uuu sss lll rrr mmm vvv nnn ooo ppp
Footer
This is an easy to use templating system for extracting content from XML files. It is based on the HTML::Template manpage's <TMPL_VAR> and <TMPL_LOOP> tags and uses the XML::LibXML manpage's XPath function to extract the requested XML content.
It has built-in support for language localization.
my $output = $xpt->process(xpt_filename => $xpt_filename, xml_filename => $xml_filename, lang => 'en');
my $output = $xpt->process(xpt_scalarref => $xpt_scalarref, xml_filename => $xml_filename, lang => 'en');
my $output = $xpt->process(xpt_scalarref => $xpt_scalarref, xml_text => $xml_text, lang => 'en');
In the third form, $xml_text should have no external XML file references, or the code is unlikely to work. Note that this has not been tested.
my $lang_output = $xpt->process_all_lang(xpt_filename => $xpt_filename, xml_filename => $xml_filename);
# english output my $output_en = $lang_output->{'en'};
T.J. Mather (tjmather@tjmather.com)
If you use the same XML::LibXML query for a CONTENT_LOOP as well as a CONTENT_VAR tag, then HTML::Template will croak. A workaround is to append a ``/.'' at the end of the xpath query.
Fixes, Bug Reports, Docs have been generously provided by:
Boris Zentner Tatsuhiko Miyagawa Matt Churchyard
Thanks!
Copyright (c) 2002 T.J. Mather. All rights Reserved.
This package is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
HTML::Template::XPath - Easy access to XML files from HTML::Template using XPath |