szr has asked for the wisdom of the Perl Monks concerning the following question:
Element foo:child is valid without a xmlns:foo="http://foo" attribute since that appears further up the tree, in the root node. What I don't quite understand is the behavior of addChild or appendChild of XML::LibXML::Node which doesn't seem to realize that prefix is associated with a namespace further up the tree (in the root node) and doesn't properly split foo:child.<?xml version="1.0"?> <root xmlns:foo="http://foo"><parent><foo:child/></parent></root>
Which yields: child->prefix=, child->localname=foo:childuse strict; use warnings; use XML::LibXML; my $d = XML::LibXML::Document->new; $d->setDocumentElement( my $r = $d->createElement( q{root} ) ); $r->setNamespace( q{http://foo}, q{foo}, 0 ); my $p = $r->addChild( $d->createElement( q{parent} ) ); my $c = $p->addChild( $d->createElement( q{foo:child} ) ); printf qq{child->prefix=%s, child->localname=%s\n}, $c->prefix, $c->lo +calname;
I get this: an->prefix=foo, an->name=abc$c->setAttribute( q{foo:abc}, q{123} ); my $an = $c->getAttributeNode( q{foo:abc} ); printf qq{an->prefix=%s, an->name=%s\n}, $an->prefix, $an->name;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: XML::LibXML::Element appendChild/addChild not inheriting namespace?
by choroba (Cardinal) on Dec 31, 2015 at 19:24 UTC | |
by szr (Scribe) on Jan 01, 2016 at 18:56 UTC | |
by choroba (Cardinal) on Jan 01, 2016 at 19:28 UTC |