1XML::Grove::Subst(3) User Contributed Perl Documentation XML::Grove::Subst(3)
2
3
4
6 XML::Grove::Subst - substitute values into a template
7
9 use XML::Grove::Subst;
10
11 # Using subst method on XML::Grove::Document or XML::Grove::Element:
12 $new_grove = $source_grove->subst( ARGS );
13 $new_grove = $source_grove->subst_hash( ARG );
14
15 # Using an XML::Grove::Subst instance:
16 $subster = XML::Grove::Subst->new();
17 $new_grove = $subster->subst( $source_grove, ARGS );
18 $new_grove = $subster->subst_hash( $source_grove, ARG );
19
21 "XML::Grove::Subst" implements XML templates. "XML::Grove::Subst"
22 traverses through a source grove replacing all elements with names
23 `"SUB:XXX"' or `"SUB:key"' with their corresponding values from ARGS (a
24 list) or ARG (a hash), repsectively.
25
27 $grove_obj->subst( ARGS ) =item $subster->subst( $grove_obj, ARGS )
28 Search for `"SUB:XXX"' elements, where XXX is an array index, and
29 replace the element with the value from ARGS, a list of values.
30 The return value is a new grove with the substitutions applied.
31
32 $grove_obj->subst_hash( ARG ) =item $subster->subst_hash( $grove_obj,
33 ARG )
34 Search for `"SUB:key"' elements and replace the element with the
35 value from ARG, a hash of values. The hash key is taken from the
36 `"key"' attribute of the `"SUB:key"' element, for example,
37 `"<SUB:key key='foo'>"'. The return value is a new grove with the
38 substitutions applied.
39
41 The following template, in a file `"template.xml"', could be used for a
42 simple parts database conversion to HTML:
43
44 <html>
45 <head>
46 <title><SUB:key key='Name'></title>
47 </head>
48 <body>
49 <h1><SUB:key key='Name'></title>
50 <p>Information for part number <SUB:key key='Number'>:</p>
51 <SUB:key key='Description'>
52 </body>
53 </html>
54
55 To use this template you would first parse it and convert it to a
56 grove, and then use `"subst_hash()"' every time you needed a new page:
57
58 use XML::Parser::PerlSAX;
59 use XML::Grove;
60 use XML::Grove::Builder;
61 use XML::Grove::Subst;
62 use XML::Grove::PerlSAX;
63 use XML::Handler::XMLWriter;
64
65 # Load the template
66 $b = XML::Grove::Builder->new();
67 $p = XML::Parser::PerlSAX->new( Handler = $b );
68 $source_grove = $p->parse( Source => { SystemId => 'template.xml' } );
69
70 # Apply the substitutions
71 $new_grove = $source_grove->subst_hash( { Name => 'Acme DCX-2000 Filter',
72 Number => 'N4728',
73 Description => 'The Best' } );
74
75 # Write the new grove to standard output
76 $w = XML::Handler::XMLWriter->new();
77 $wp = XML::Grove::PerlSAX->new( Handler => $w );
78 $wp->parse( Source => { Grove => $new_grove } );
79
81 Ken MacLeod, ken@bitsko.slc.ut.us
82
84 perl(1), XML::Grove(3)
85
86 Extensible Markup Language (XML) <http://www.w3c.org/XML>
87
89 Hey! The above document had some coding errors, which are explained
90 below:
91
92 Around line 173:
93 You forgot a '=back' before '=head1'
94
95
96
97perl v5.30.0 2019-07-26 XML::Grove::Subst(3)