Link to home
Create AccountLog in
Avatar of kawas
kawasFlag for United States of America

asked on

namespaces in LibXML

Hi,

I am using LibXML to create an xml document. I am also using namespaces.

The problem that i am running into is that the document that i am creating has the namespace declaration at each element.

For instance,

<foo:A xmlns:foo="http://www.www.com/foo">
   <foo:B xmlns:foo="http://www.www.com/foo"> blah blah blab </foo:B>
   <foo:C xmlns:foo="http://www.www.com/foo"> blah blah blab </foo:C>
</foo:A>

And I want the following:
<foo:A xmlns:foo="http://www.www.com/foo">
   <foo:B> blah blah blab </foo:B>
   <foo:C> blah blah blab </foo:C>
</foo:A>

How can i achieve this.

Thanks.
Avatar of Adam314
Adam314

Which libXML are you using?  I did a search, and found several.
Avatar of kawas

ASKER

I didn't see anything in the documentation that would cause (or remove) what you are getting.

Is the namespace defined as an attribute for each element?
Avatar of kawas

ASKER

Some code:

use XML::LibXML;
        my $root =
          XML::LibXML::Element->new( "A" );
        $root->setNamespace( "http://www.www.com/foo", "foo" );

        my $b =
          XML::LibXML::Element->new( "B" );
        $b->setNamespace( "http://www.www.com/foo", "foo" );
        $b->setAttributeNS( "http://www.www.com/foo", "foo", "name");
        $root->appendChild($b);

        my $c =
          XML::LibXML::Element->new( "C" );
        $c->setNamespace( "http://www.www.com/foo", "foo" );
        $c->setAttributeNS( "http://www.www.com/foo", "foo", "name");
        $root->appendChild($c);
print $root->toString(2);
I don't know much about the XML libraries.... but are you supposed to set the namespace for each element?  

Can't test now, but how about this:


use XML::LibXML;
my $root = XML::LibXML::Element->new( "A" );
$root->setNamespace( "http://www.www.com/foo", "foo" );

my $b = XML::LibXML::Element->new( "B" );
$root->appendChild($b);

my $c = XML::LibXML::Element->new( "C" );
$root->appendChild($c);

print $root->toString(2);
Avatar of kawas

ASKER

then b and c are not prefixed. the result is:

<foo:A xmlns:foo="http://www.www.com/foo">
  <B/>
  <C/>
</foo:A>
When there is a <NameGoesHere />, it means that NameGoesHere is defined, but doesn't have a value or any attributes.

Add an attribute using:
$node->setAttribute( $aname, $avalue );

Add text using:
$node->appendTextNode( $text );



So, try it with this code:
use XML::LibXML;
my $root = XML::LibXML::Element->new( "A" );
$root->setNamespace( "http://www.www.com/foo", "foo" );

my $b = XML::LibXML::Element->new( "B" );
$b->setAttribute('AttributeForB','ValueB');
$b->appendTextNode( "blah blah for B" );
$root->appendChild($b);

my $c = XML::LibXML::Element->new( "C" );
$c->setAttribute('AttributeForC','ValueC');
$c->appendTextNode( "blah blah for C" );
$root->appendChild($c);

print $root->toString(2);
Avatar of kawas

ASKER

Hi Adam,

I didnt realize that you had followed up on this question.

I tried your code, and this is what is returned:
<foo:A xmlns:foo="http://www.www.com/foo">
  <B AttributeForB="ValueB">
blah blah for B
  </B>
  <C AttributeForC="ValueC">
blah blah for C
  </C>
</foo:A>

B, C and the attributes arent prefixed.

I have kind of solved this problem:
http://groups.google.com/group/comp.lang.perl.modules/browse_frm/thread/415a2fb90363a209/#
Although, i am not happy with the solution.
I've read the comments in the link you provided... I don't know of a automatic way to add the prefix to every element - normally you wouldn't do this.
Avatar of kawas

ASKER

well, your right, if you want to default to the namespace declared on the top (root) element. I also realize that having the namespace declaration on every level is of the document is valid too (but really ugly). For the code that i was writing, i needed to prefix each element and have yet to find a good way of doing so using LibXML (perhaps the thing to do is move away from LibXML). The link i provided is a short term solution, I guess.

Thanks Adam
ASKER CERTIFIED SOLUTION
Avatar of DarthMod
DarthMod
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer