Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 301
  • Last Modified:

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.
0
kawas
Asked:
kawas
  • 5
  • 5
1 Solution
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now