1 # $Id: 8_Namespaces.t,v 1.7 2004/04/05 09:12:51 grantm Exp $
12 eval { require XML::SAX; };
14 plan skip_all => 'no XML::SAX';
17 eval { require XML::NamespaceSupport; };
19 plan skip_all => "no XML::NamespaceSupport";
21 if($XML::NamespaceSupport::VERSION < 1.04) {
22 plan skip_all => "XML::NamespaceSupport is too old (upgrade to 1.04 or better)";
28 ##############################################################################
29 # S U P P O R T R O U T I N E S
30 ##############################################################################
32 ##############################################################################
39 open(IN, $Src) || return(undef);
44 open(OUT, ">$Dst") || return(undef);
52 ##############################################################################
53 # T E S T R O U T I N E S
54 ##############################################################################
58 # Force default behaviour of using SAX parser if it is available (which it
59 # is or we wouldn't be here).
61 $XML::Simple::PREFERRED_PARSER = '';
63 # Confirm that by default qnames are not expanded on input
65 my $xml = q(<config xmlns:perl="http://www.perl.com/">
66 <perl:list count="3" perl:type="array">
70 <test xmlns:perl="http://www.microsoft.com" perl:tm="trademark" />
82 'perl:type' => 'array',
84 'xmlns:perl' => 'http://www.microsoft.com',
85 'perl:tm' => 'trademark',
88 'xmlns:perl' => 'http://www.perl.com/'
91 my $opt = XMLin($xml);
92 is_deeply($opt, $expected, 'qnames are not expanded by default');
95 # Try again with nsexpand option set
98 '{http://www.perl.com/}list' => {
105 '{http://www.perl.com/}type' => 'array',
107 '{http://www.microsoft.com}tm' => 'trademark',
108 '{http://www.w3.org/2000/xmlns/}perl' => 'http://www.microsoft.com'
111 '{http://www.w3.org/2000/xmlns/}perl' => 'http://www.perl.com/'
114 $opt = XMLin($xml, nsexpand => 1);
115 is_deeply($opt, $expected, 'qnames are expanded on request');
118 # Confirm that output expansion does not occur by default
121 '{http://www.w3.org/2000/xmlns/}perl' => 'http://www.perl.com/',
122 '{http://www.perl.com/}attr' => 'value',
124 '{http://www.perl.com/}element' => [ 'data' ],
130 (\s+{http://www.w3.org/2000/xmlns/}perl="http://www.perl.com/"
131 |\s+{http://www.perl.com/}attr="value"
134 \s*<{http://www.perl.com/}element\s*>data</{http://www.perl.com/}element\s*>
137 }sx, 'clarkian names not converted to qnames on output by default');
140 # Confirm nsexpand option works on output
142 $xml = XMLout($opt, nsexpand => 1);
145 (\s+xmlns:perl="http://www.perl.com/"
146 |\s+perl:attr="value"
149 \s*<perl:element\s*>data</perl:element\s*>
152 }sx, 'clarkian names are converted to qnames on output on request');
155 # Check that default namespace is correctly read in ...
157 $xml = q(<opt xmlns="http://www.orgsoc.org/">
160 <member>Dick</member>
161 <member>Larry</member>
167 'xmlns' => 'http://www.orgsoc.org/',
168 '{http://www.orgsoc.org/}list' => {
169 '{http://www.orgsoc.org/}member' => [ 'Tom', 'Dick', 'Larry' ],
173 $opt = XMLin($xml, nsexpand => 1);
174 is_deeply($opt, $expected, 'expansion of default namespace works');
177 # ... and written out
179 $xml = XMLout($opt, nsexpand => 1);
182 \s+xmlns="http://www.orgsoc.org/"
185 \s*<member>Tom</member>
186 \s*<member>Dick</member>
187 \s*<member>Larry</member>
191 }sx, 'default namespaces are output correctly too');
194 # Check that the autogeneration of namespaces works as we expect
197 'xmlns' => 'http://www.orgsoc.org/',
198 '{http://www.orgsoc.org/}list' => {
199 '{http://www.orgsoc.org/}member' => [ 'Tom', 'Dick', 'Larry' ],
200 '{http://www.phantom.com/}director' => [ 'Bill', 'Ben' ],
204 $xml = XMLout($opt, nsexpand => 1);
206 if($xml =~ m{<list\s+xmlns:(\w+)="http://www.phantom.com/"\s*>}) {
209 # regex match split in two to workaround 5.8.1/utf8/regex match prob
212 \s+xmlns="http://www.orgsoc.org/"
217 }sx, 'namespace prefixes are generated automatically (part 1)');
220 (\s*<member>Tom</member>
221 \s*<member>Dick</member>
222 \s*<member>Larry</member>
223 |\s*<${prefix}:director>Bill</${prefix}:director>
224 \s*<${prefix}:director>Ben</${prefix}:director>){2}
226 }sx, 'namespace prefixes are generated automatically (part 2)');