kawas
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.
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.
Which libXML are you using? I did a search, and found several.
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?
Is the namespace defined as an attribute for each element?
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);
use XML::LibXML;
my $root =
XML::LibXML::Element->new(
$root->setNamespace( "http://www.www.com/foo", "foo" );
my $b =
XML::LibXML::Element->new(
$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->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);
Can't test now, but how about this:
use XML::LibXML;
my $root = XML::LibXML::Element->new(
$root->setNamespace( "http://www.www.com/foo", "foo" );
my $b = XML::LibXML::Element->new(
$root->appendChild($b);
my $c = XML::LibXML::Element->new(
$root->appendChild($c);
print $root->toString(2);
ASKER
then b and c are not prefixed. the result is:
<foo:A xmlns:foo="http://www.www.com/foo">
<B/>
<C/>
</foo:A>
<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('Attribut eForB','Va lueB');
$b->appendTextNode( "blah blah for B" );
$root->appendChild($b);
my $c = XML::LibXML::Element->new( "C" );
$c->setAttribute('Attribut eForC','Va lueC');
$c->appendTextNode( "blah blah for C" );
$root->appendChild($c);
print $root->toString(2);
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(
$root->setNamespace( "http://www.www.com/foo", "foo" );
my $b = XML::LibXML::Element->new(
$b->setAttribute('Attribut
$b->appendTextNode( "blah blah for B" );
$root->appendChild($b);
my $c = XML::LibXML::Element->new(
$c->setAttribute('Attribut
$c->appendTextNode( "blah blah for C" );
$root->appendChild($c);
print $root->toString(2);
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 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.
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
Thanks Adam
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.