Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

copyCurrentNode in XML::LibXML::Reader will add xmlns attribute

by mjfan (Novice)
on Sep 15, 2017 at 02:23 UTC ( [id://1199434]=perlquestion: print w/replies, xml ) Need Help??

mjfan has asked for the wisdom of the Perl Monks concerning the following question:

I read some large XML using XML::LibXML::Reader and want to write out some element interested.

I call  print $reader->copyCurrentNode. But I got xmlns attribute for every element I copied. In original XML, this attribute only appear on the root.

I don't want this extra attribute thousands of times. How can I simply remove them? Is there any option to the parser?

Replies are listed 'Best First'.
Re: copyCurrentNode in XML::LibXML::Reader will add xmlns attribute
by ikegami (Patriarch) on Sep 15, 2017 at 04:15 UTC

    According to the docs for $node->cloneNode, it's behaviour required by the DOM spec (for $node->cloneNode, the function $reader->copyCurrentNode mirrors).

Re: copyCurrentNode in XML::LibXML::Reader will add xmlns attribute
by haukex (Archbishop) on Sep 15, 2017 at 07:16 UTC

    I've had similar experiences with XML::LibXML, see my node here for a potential workaround (basically manually copying and re-creating the nodes instead of cloning).

    Updated wording slightly for clarification.

        Thanks, despite being marked "experimental" it does appear to work in the OP's case:

        use warnings; use strict; use XML::LibXML::Reader; my $xml = <<'ENDXML'; <foo xmlns="http://www.example.com"><bar/></foo> ENDXML my $reader = XML::LibXML::Reader->new(string => $xml); while ($reader->read) { my $node = $reader->copyCurrentNode; $node->setNamespaceDeclURI(undef, undef) if $node->nodeName eq 'bar'; print "$node"; } __END__ <foo xmlns="http://www.example.com"/><bar/><foo xmlns="http://www.exam +ple.com"/>

        Unfortunately I might still be missing something, because applied to choroba's code here it doesn't seem to change the namespace of the nodes.

Re: copyCurrentNode in XML::LibXML::Reader will add xmlns attribute
by beech (Parson) on Sep 15, 2017 at 05:00 UTC

    Hi,

    Why copy? If you print the node ( eg toString) instead of a copy, the output will not contain the xmlns.

Re: copyCurrentNode in XML::LibXML::Reader will add xmlns attribute
by Anonymous Monk on Sep 15, 2017 at 14:42 UTC
    "Required by the DOM spec" or not, it is a pain in the ass that IMHO certainly should be optional in practice. As the OP said, you don't want this duplicated thousands of times, especially if you are perfectly aware which software will actually be reading the file.
Re: copyCurrentNode in XML::LibXML::Reader will add xmlns attribute
by mjfan (Novice) on Sep 18, 2017 at 02:12 UTC

    Thank you all for the reply. Seems the namespace is forcely written out and there is no way to change that behavior.

    I don't want to do go through each attribute and construct a new element neither.

    Currently I still use the copy and then do a string replacement  s/ xmlns="[^"]"// to remove it

      Hi,

      So why are you cloning the nodes?  $node->setNamespaceDeclURI(undef, undef); will unset the namespace

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1199434]
Approved by beech
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2024-04-23 10:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found