XML::BindData - Bind data structures into XML
<?xml version="1.0" encoding="utf-8"?> <request> <type>add</type> <obj> <title tmpl-bind="module.title"/> <description tmpl-bind="module.description" tmpl-default="A Perl module"/> <version tmpl-bind="module.version" tmpl-default="0.0.1"/> <no-show tmpl-if="foo"/> <multiple-elems> <item tmpl-each="items"> <id tmpl-bind="id"/> <name tmpl-bind="name"/> </item> </multiple-elems> <keywords> <keyword tmpl-each="keywords" tmpl-bind="this"/> </keywords> <here> <is-a> <nested tmpl-each="nested"> <thing tmpl-each="this" tmpl-attr-map="value:this"/> </nested> </is-a> </here> </obj> </request>
my $data = { module => { title => 'XML::BindData', description => <<'EOF', Yet another way to generate XML for you. EOF }, items => [ { id => 1, name => 'perl' }, { id => 2, name => 'xml' }, ], keywords => [ qw/ perl xml / ], nested => [ [ qw/1 2 3/ ], [ qw/4 5 6/ ], ], }; print XML::BindData->bind($source_xml, $data);
<request> <type>add</type> <obj> <title>XML::BindData</title> <description>Yet another way to generate XML for you.</description> <version>0.0.1</version> <multiple-elems> <item> <id>1</id> <name>perl</name> </item> <item> <id>2</id> <name>xml</name> </item> </multiple-elems> <keywords> <keyword>perl</keyword> <keyword>xml</keyword> </keywords> <here> <is-a> <nested> <thing value="1"></thing> <thing value="2"></thing> <thing value="3"></thing> </nested> <nested> <thing value="4"></thing> <thing value="5"></thing> <thing value="6"></thing> </nested> </is-a> </here> </obj> </request>
N.B. This module should be considered BETA quality. Bugs are expected.
This module provides yet another mechanism through which XML files can be created from Perl. It does this by reading in a valid XML template, and binding data directly into the DOM; creating/removing nodes as needs be.
This has the following benefits:
The module is probably not appropriate if you are already happily using XSLT, Template Toolkit, etc. for XML generation.
This forms the entire public API to the module. It will parse the XML and traverse the resulting tree, binding the information in %data.
These directives may be assigned to nodes in the tree:
Adds text content to the node. If an option is dot.separated, this will split on the dot and descend into nested hashes.
To be used with tmpl-bind. Adds a default text content value to the node if the value specified by tmpl-bind is undefined.
tmpl-bind
For the array ref found at 'option', duplicate this node, setting the current context to each item. An option name of 'this' refers to the current item, so a common idiom is:
<foo tmpl-each="nums" tmpl-bind="this"/>
...for the data...
{ nums => [ 1, 2, 3 ] }
...this would return...
<foo>1</foo><foo>2</foo><foo>3</foo>
Only show the node if bool is true-ish. Can be negated as tmpl-if="!bool".
tmpl-if="!bool"
Bind the value of 'opt1' into the attribute 'attr-name-one'. Multiple attributes can be assigned at a time, separated by commas. Comma and colon characters in a default value must be preceded with a single backslash.
Bind the value of 'default1' into the attribute 'attr-name-one' if no other defined value is supplied. Multiple attributes can be assigned at a time, separated by commas.
To install XML::BindData, copy and paste the appropriate command in to your terminal.
cpanm
cpanm XML::BindData
CPAN shell
perl -MCPAN -e shell install XML::BindData
For more information on module installation, please visit the detailed CPAN module installation guide.