View on
MetaCPAN is shutting down
For details read Perl NOC. After June 25th this page will redirect to
Naoki Tomita > Template-Semantic > Template::Semantic



Annotate this POD

View/Report Bugs
Module Version: 0.09   Source  


Template::Semantic - Use pure XHTML/XML as a template


  use Template::Semantic;
  print Template::Semantic->process('template.html', {
      'title, h1' => 'Naoki Tomita',
      'ul.urls li' => [
          { 'a' => 'Profile & Contacts', 'a@href' => '', },
          { 'a' => 'Twitter',            'a@href' => '', },


      <head><title>person name</title></head>
          <h1>person name</h1>
          <ul class="urls">
              <li><a href="#">his page</a></li>


      <head><title>Naoki Tomita</title></head>
          <h1>Naoki Tomita</h1>
          <ul class="urls">
              <li><a href="">Profile &amp; Contacts</a></li>
              <li><a href="">Twitter</a></li>


Template::Semantic is a template engine for XHTML/XML based on XML::LibXML that doesn't use any template syntax. This module takes pure XHTML/XML as a template, and uses XPath or CSS selectors to assign values.


$ts = Template::Semantic->new( %options )

Constructs a new Template::Semantic object.

  my $ts = Template::Semantic->new(
  my $res = $ts->process(...);

If you do not want to change the options from the defaults, you may skip new() and call process() directly:

  my $res = Template::Semantic->process(...);

Set %options if you want to change parser options:

  • parser => $your_libxml_parser

    Set if you want to replace XML parser. It should be XML::LibXML based.

      my $ts = Template::Semantic->new(
          parser => My::LibXML->new,
  • (others)

    All other parameters are applied to the XML parser as method calls ($parser->$key($value)). Template::Semantic uses this configuration by default:

      no_newwork => 1  # faster
      recover    => 2  # "no warnings" style

    See "PARSER OPTIONS" in XML::LibXML::Parser for details.

      # "use strict;" style
      my $ts = Template::Semantic->new( recover => 0 );
      # "use warnings;" style
      my $ts = Template::Semantic->new( recover => 1 );
$res = $ts->process($filename, \%vars)
$res = $ts->process(\$text, \%vars)
$res = $ts->process(FH, \%vars)

Process a template and return a Template::Semantic::Document object.

The first parameter is the input template, which may take one of several forms:

  # filename
  my $res = Template::Semantic->process('template.html', $vars);
  # text reference
  my $res = Template::Semantic->process(\'<html><body>foo</body></html>', $vars);
  # file handle, GLOB
  my $res = Template::Semantic->process($fh, $vars);
  my $res = Template::Semantic->process(\*DATA, $vars);

The second parameter is a value set to bind the template. $vars should be a hash-ref of selectors and corresponding values. See the "SELECTOR" and "VALUE TYPE" sections below. For example:

    '.foo'    => 'hello',
    '//title' => 'This is a title',
$ts->define_filter($filter_name, \&code)

See the "Filter" section.


Use XPath expression or CSS selector as a selector. If the expression doesn't look like XPath, it is considered CSS selector and converted into XPath internally.

  print Template::Semantic->process($template, {
      # XPath sample that indicate <tag>
      '/html/body/h2[2]' => ...,
      '//title | //h1'   => ...,
      '//img[@id="foo"]' => ...,
      'id("foo")'        => ...,
      # XPath sample that indicate @attr
      '//a[@id="foo"]/@href'              => ...,
      '//meta[@name="keywords"]/@content' => ...,
      # CSS selector sample that indicate <tag>
      'title'         => ...,
      '#foo'          => ...,
      '.foo' => ...,
      # CSS selector sample that indicate @attr
      'img#foo@src'     => ...,
      ' a@href' => ...,
      '@alt, @title'    => ...,

Template::Semantic allows some selector syntax that is different from usual XPath for your convenience.

1. You can use xpath '//div' without using XML::LibXML::XPathContext even if your template has default namespace (<html xmlns="...">).

2. You can use 'id("foo")' function to find element with id="foo" instead of xml:id="foo" without DTD. Note: use '//*[@xml:id="foo"]' if your template uses xml:id="foo".

3. You can '@attr' syntax with CSS selector that specifies the attribute. This is original syntax of this module.









XML::LibXML, HTML::Selector::XPath

I got a lot of ideas from Template, Template::Refine, Web::Scraper. thanks!


Naoki Tomita <>

Feedback, patches, POD English check are always welcome!


This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

syntax highlighting: