We help IT Professionals succeed at work.

namespaces in LibXML

kawas
kawas asked
on
Medium Priority
322 Views
Last Modified: 2013-11-19
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.
Comment
Watch Question

Top Expert 2009

Commented:
Which libXML are you using?  I did a search, and found several.
Top Expert 2009

Commented:
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?

Author

Commented:
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);
Top Expert 2009

Commented:
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);

Author

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

<foo:A xmlns:foo="http://www.www.com/foo">
  <B/>
  <C/>
</foo:A>
Top Expert 2009

Commented:
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);

Author

Commented:
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.
Top Expert 2009

Commented:
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.

Author

Commented:
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
Commented:
PAQed with points refunded (250)

DarthMod
Community Support Moderator

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.