Subject: | There should be a way to force XML namespace declarations in advance |
XML::Writer doesn't offer any way to force namespace declarations ahead of use. This patch adds that functionality, as the 'FORCED_NS_DECLS' parameter.
Example:
my $w = new XML::Writer(
NAMESPACES => 1,
PREFIX_MAP => {'uri:test', 'test'},
FORCED_NS_DECLS => ['uri:test'],
DATA_MODE => 1
);
$w->startTag('doc');
$w->emptyTag(['uri:test', 'elem']);
$w->emptyTag(['uri:test', 'elem']);
$w->emptyTag(['uri:test', 'elem']);
$w->endTag('doc');
Result:
<doc>
<test:elem xmlns:test="uri:test" />
<test:elem xmlns:test="uri:test" />
<test:elem xmlns:test="uri:test" />
</doc>
Expected:
<doc xmlns:test="uri:test">
<test:elem />
<test:elem />
<test:elem />
</doc>
--- XML/Writer.pm 5 Feb 2004 08:57:54 -0000 1.4
+++ XML/Writer.pm 5 Feb 2004 22:27:30 -0000 1.8
@@ -694,6 +694,12 @@
my $nsDefaultDecl = undef;
my @nsCopyFlag = ();
my $nsCopyFlag = 0;
+ my @forcedNSDecls = ();
+
+ if ($params{FORCED_NS_DECLS}) {
+ @forcedNSDecls = @{$params{FORCED_NS_DECLS}};
+ delete $params{FORCED_NS_DECLS};
+ }
#
# Push the current declaration state.
@@ -791,6 +797,16 @@
}
$i += 2;
}
+
+ # We only do this for the outermost element
+ if (@forcedNSDecls) {
+ foreach (@forcedNSDecls) {
+ my @dummy = ($_, 'dummy');
+ my $d2 = \@dummy;
+ &{$processName}(\$d2, $_[0], 1);
+ }
+ @forcedNSDecls = ();
+ }
};
#
@@ -1045,6 +1061,14 @@
To set the default namespace, use '' for the prefix.
+=item FORCED_NS_DECLS
+
+An array reference; if this parameter is present, the document element
+will contain declarations for all the given namespace URIs.
+Declaring namespaces in advance is particularly useful when a large
+number of elements from a namespace are siblings, but don't share a direct
+ancestor from the same namespace.
+
=item NEWLINES
A true or false value; if this parameter is present and its value is