NAME

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

SYNOPSIS

use Template::Semantic;

print Template::Semantic->process('template.html', {
    'title, h1' => 'Naoki Tomita',
    'ul.urls li' => [
        { 'a' => 'Profile & Contacts', 'a@href' => 'http://e8y.net/', },
        { 'a' => 'Twitter',            'a@href' => 'http://twitter.com/tomita/', },
    ],
});

template.html

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

output:

<html>
    <head><title>Naoki Tomita</title></head>
    <body>
        <h1>Naoki Tomita</h1>
        <ul class="urls">
            <li><a href="http://e8y.net/">Profile &amp; Contacts</a></li>
            <li><a href="http://twitter.com/tomita/">Twitter</a></li>
        </ul>
    </body>
</html>

DESCRIPTION

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.

METHODS

SELECTOR

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 span.bar' => ...,

    # CSS selector sample that indicate @attr
    'img#foo@src'     => ...,
    'span.bar 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.

VALUE TYPE

Basics

Loop

Callback

Filter

SEE ALSO

Template::Semantic::Cookbook

Template::Semantic::Document

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

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

AUTHOR

Naoki Tomita tomita@cpan.org

Feedback, patches, POD English check are always welcome!

LICENSE

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