Mr. Ed has asked for the wisdom of the Perl Monks concerning the following question:
I wonder how to add namespace into my xml-attribute declaration: "xsi:nil='true'" I have to create a xml like this:
But I can't find the right way to do this. My Code:<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <node1 xsi:nil="true" /> </root>
Gives me this:1 #!/bin/perl 2 use strict; 3 use warnings; 4 use utf8; 5 6 use XML::Writer; 7 8 9 my $ns1 = "http://www.w3.org/2001/XMLSchema-instance"; 10 my $prefixMap = {$ns1 => "xsi"}; 11 12 my $writer = new XML::Writer( 13 OUTPUT => 'self', 14 DATA_INDENT => 3, 15 DATA_MODE => 1, 16 NAMESPACES => 1, 17 PREFIX_MAP => $prefixMap 18 ); 19 20 $writer->startTag("root"); 21 22 $writer->emptyTag("node1", ('nil' => "true")); 23 $writer->emptyTag("node3", [$ns1, "nil" => "true"]); 24 $writer->endTag("root"); 25 26 print $writer->to_string;
<root> <node1 nil="true" /> <node3 xsi:nil="xmlns:xsi" http://www.w3.org/2001/XMLSchema-instanc +e="" /> </root>
1) I miss the namespace declaratione in the <root> - element
2) If I try to set the namespace on an attribute manualy like: $writer->emptyTag("node1", ('xsi:nil' => "true")); I get an error: Attribute name 'xsi:nil' contains ':' and the writer terminates.
Is there anyone who can help me? Thanks.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: XML::Writer and namespaces
by Mr. Ed (Initiate) on Dec 21, 2016 at 13:51 UTC | |
by Mr. Ed (Initiate) on Dec 21, 2016 at 13:59 UTC | |
by Anonymous Monk on Dec 04, 2019 at 15:02 UTC |