T.J. Mather

NAME

HTML::Template::XPath - Easy access to XML files from HTML::Template using XPath

SYNOPSIS

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

DESCRIPTION

This is an easy to use templating system for extracting content from XML files. It is based on HTML::Template's <TMPL_VAR> and <TMPL_LOOP> tags and uses XML::LibXML's XPath function to extract the requested XML content.

It has built-in support for language localization.

METHODS

process

Processes an XPath Template file and XML file in a specified language.

  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.

process_all_lang

Processes the template and returns a hash reference containing language codes as keys and outputs as values.

  my $lang_output = $xpt->process_all_lang(xpt_filename => $xpt_filename,
                                           xml_filename => $xml_filename);

  # english output
  my $output_en = $lang_output->{'en'};

AUTHOR

T.J. Mather (tjmather@tjmather.com)

BUGS

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.

CREDITS

Fixes, Bug Reports, Docs have been generously provided by:

  Boris Zentner
  Tatsuhiko Miyagawa
  Matt Churchyard

Thanks!

COPYRIGHT

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.