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.
LVL 13
kawasAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Adam314Commented:
Which libXML are you using?  I did a search, and found several.
0
kawasAuthor Commented:
0
Adam314Commented:
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?
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

kawasAuthor 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);
0
Adam314Commented:
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);
0
kawasAuthor Commented:
then b and c are not prefixed. the result is:

<foo:A xmlns:foo="http://www.www.com/foo">
  <B/>
  <C/>
</foo:A>
0
Adam314Commented:
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);
0
kawasAuthor 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.
0
Adam314Commented:
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.
0
kawasAuthor 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
0
DarthModCommented:
PAQed with points refunded (250)

DarthMod
Community Support Moderator
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.