I feel there is something I'm missing in regards to namespace inheritance in
XML::LibXML.
Please consider the following:
<?xml version="1.0"?>
<root xmlns:foo="http://foo"><parent><foo:child/></parent></root>
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.
use 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;
Which yields:
child->prefix=, child->localname=foo:child
When I would have expected:
child->prefix=foo, child->localname=child
I know
createElementNS can be used, but that requires one to know what NS to pass rather than being able to rely on inheritance.
What is even stranger is inheritance actually appears to work with attributes. If I add the following to the code above:
$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;
I get this:
an->prefix=foo, an->name=abc
Also note the lack of using
setAttributeNS method, as this is about inheritance, rather than manually specifying the namespace uri each time an attribute is added.
So why doesn't this seem to work for elements? Maybe I'm really missing something, probably fairly obvious, as it seems this should be trivial.
Thanks.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.