[Petal] Stupid Petal Tricks: Meta-templates using the 'structure' modifier

Kurt Stephens kstep at pepsdesign.com
Mon Feb 17 13:24:08 GMT 2003


I know that there are many modules out there for dumping arbitrary datasets
to XML, but today I tried something really silly with Petal.  The following
is an example of a meta-template that uses the structure modifier to create
a template with XML elements defined by the data passed to the
meta-template.  I included a namespace URI in the top-level element.

The meta-template:

<?xml version="1.0" encoding="iso-8859-1" ?>
<!--? Meta-template builds an XML structure ?-->
<Dataset xmlns="http://example.org/ns/ToBeReplaced"
         xmlns:petal="http://purl.org/petal/1.0/ToBeReplaced"
         xmlns:meta="http://purl.org/petal/1.0/"
         meta:attributes="xmlns ns_uri; xmlns:petal
string:http://purl.org/petal/1.0/">
  <TableName petal:repeat="record data"
             meta:replace="structure string:&lt;${table}
petal:repeat=&quot;record data&quot;&gt;" />
    <FieldName meta:repeat="field fields"
               meta:replace="structure string:&lt;${field}
petal:content=&quot;record/${field}&quot;&gt;${field} data&lt;/${field}&gt;"
/>
  <TableName petal:repeat="record data"
             meta:replace="structure string:&lt;/${table}&gt;" />
</Dataset>

Processed with:

my $output = $template->process(
    ns_uri => 'http://example.org/ns/stuff',
    table  => 'stuff',
    fields => [ qw( foo bar baz ) ],
    );

Yields:

<?xml version="1.0" encoding="iso-8859-1" ?>

<Dataset xmlns="http://example.org/ns/stuff"
xmlns:petal="http://purl.org/petal/1.0/">
  <stuff petal:repeat="record data">
    <foo petal:content="record/foo">foo data</foo><bar
petal:content="record/bar">bar data</bar><baz petal:content="record/baz">baz
data</baz>
  </stuff>
</Dataset>


I have attached a copy of the meta-template and a sample script.

Best Regards,

Kurt Stephens
kstep at pepsdesign.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: struct_demo.pl
Type: application/octet-stream
Size: 719 bytes
Desc: not available
Url : http://lists.webarch.co.uk/pipermail/petal/attachments/20030217/57890d12/struct_demo.obj
-------------- next part --------------
A non-text attachment was scrubbed...
Name: struct_demo.xml
Type: text/xml
Size: 785 bytes
Desc: not available
Url : http://lists.webarch.co.uk/pipermail/petal/attachments/20030217/57890d12/struct_demo.xml


More information about the Petal mailing list