Click here to get back home

LibXML "Undefined namespace prefix"

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
LibXML "Undefined namespace prefix" Andrew 07-02-2007
Posted by Andrew on July 2, 2007, 5:42 pm
Please log in for more thread options


I am using LibXML to parse an XML file. The XML file is like so...

| <MyApp:Config>
| <MyApp:FilterMgr>
| ...
| </MyApp:FilterMgr>
| <MyApp:DBConnMgr>
| <RecalcMgr:RecalcMgr>
| ...
| </RecalcMgr:RecalcMgr>
| </MyApp:DBConnMgr>
| </MyApp:Config>

Here is the relevant code...

| use XML::LibXML;
| ...
| my $config = XML::LibXML->new->parse_file('config.xml');
| my $xpc = XML::LibXML::XPathContext->new($config);
| my @nodes = $xpc->findnodes($theXPath);

If $theXPath contains any prefix other than MyApp (like DBConnMgr),
then I get the following error...

| Undefined namespace prefix
| xmlXPathCompiledEval: evaluation failed

Is there some limitation/bug when it comes to namespace prefixes
nested within other namespace prefixes? I know that the namespaces
are all defined in one DTD. And everything was fine when this code
used XML::Parser instead of XML::LibXML...

Thanks in advance for any suggestions.


Posted by Andrew on July 3, 2007, 11:09 am
Please log in for more thread options


> I am using LibXML to parse an XML file. The XML file is like so...
>
> | <MyApp:Config>
> | <MyApp:FilterMgr>
> | ...
> | </MyApp:FilterMgr>
> | <MyApp:DBConnMgr>
> | <RecalcMgr:RecalcMgr>
> | ...
> | </RecalcMgr:RecalcMgr>
> | </MyApp:DBConnMgr>
> | </MyApp:Config>
>
> Here is the relevant code...
>
> | use XML::LibXML;
> | ...
> | my $config = XML::LibXML->new->parse_file('config.xml');
> | my $xpc = XML::LibXML::XPathContext->new($config);
> | my @nodes = $xpc->findnodes($theXPath);
>
> If $theXPath contains any prefix other than MyApp (like DBConnMgr),
> then I get the following error...
>
> | Undefined namespace prefix
> | xmlXPathCompiledEval: evaluation failed
>
> Is there some limitation/bug when it comes to namespace prefixes
> nested within other namespace prefixes? I know that the namespaces
> are all defined in one DTD. And everything was fine when this code
> used XML::Parser instead of XML::LibXML...
>
> Thanks in advance for any suggestions.

I have found a temporary solution. Except for the namespace prefix
that works ("MyApp" in the example above), I removed all other
prefixes from the XPath. (Of course, optionally, I could remove the
working prefix as well.)

It's still not clear to me why this happened, so any suggestions are
still welcome. Thanks!


Posted by Ian Wilson on July 3, 2007, 12:37 pm
Please log in for more thread options


Andrew wrote:
>
>>I am using LibXML to parse an XML file. The XML file is like so...
>>
>>| <MyApp:Config>
>>| <MyApp:FilterMgr>
>>| ...
>>| </MyApp:FilterMgr>
>>| <MyApp:DBConnMgr>
>>| <RecalcMgr:RecalcMgr>
>>| ...
>>| </RecalcMgr:RecalcMgr>
>>| </MyApp:DBConnMgr>
>>| </MyApp:Config>
>>
>>Here is the relevant code...
>>
>>| use XML::LibXML;
>>| ...
>>| my $config = XML::LibXML->new->parse_file('config.xml');
>>| my $xpc = XML::LibXML::XPathContext->new($config);
>>| my @nodes = $xpc->findnodes($theXPath);
>>
>>If $theXPath contains any prefix other than MyApp (like DBConnMgr),
>>then I get the following error...
>>
>>| Undefined namespace prefix
>>| xmlXPathCompiledEval: evaluation failed
>>
>>Is there some limitation/bug when it comes to namespace prefixes
>>nested within other namespace prefixes? I know that the namespaces
>>are all defined in one DTD. And everything was fine when this code
>>used XML::Parser instead of XML::LibXML...
>>
>>Thanks in advance for any suggestions.
>
>
> I have found a temporary solution. Except for the namespace prefix
> that works ("MyApp" in the example above), I removed all other
> prefixes from the XPath. (Of course, optionally, I could remove the
> working prefix as well.)
>
> It's still not clear to me why this happened, so any suggestions are
> still welcome. Thanks!
>

http://www.perlmonks.org/?node_id=555011 might be relevant

The last time I used XML::LibXML to parse some received XML I found it
easiest to just perform a namespacectomy on the XML first :-)

Elsewhere I've read that you can use XPaths like
"//*[name()='prefix:ElementName']"

Posted by Andrew on July 5, 2007, 1:15 pm
Please log in for more thread options


> Andrew wrote:
>
> >>I am using LibXML to parse an XML file. The XML file is like so...
>
> >>| <MyApp:Config>
> >>| <MyApp:FilterMgr>
> >>| ...
> >>| </MyApp:FilterMgr>
> >>| <MyApp:DBConnMgr>
> >>| <RecalcMgr:RecalcMgr>
> >>| ...
> >>| </RecalcMgr:RecalcMgr>
> >>| </MyApp:DBConnMgr>
> >>| </MyApp:Config>
>
> >>Here is the relevant code...
>
> >>| use XML::LibXML;
> >>| ...
> >>| my $config = XML::LibXML->new->parse_file('config.xml');
> >>| my $xpc = XML::LibXML::XPathContext->new($config);
> >>| my @nodes = $xpc->findnodes($theXPath);
>
> >>If $theXPath contains any prefix other than MyApp (like DBConnMgr),
> >>then I get the following error...
>
> >>| Undefined namespace prefix
> >>| xmlXPathCompiledEval: evaluation failed
>
> >>Is there some limitation/bug when it comes to namespace prefixes
> >>nested within other namespace prefixes? I know that the namespaces
> >>are all defined in one DTD. And everything was fine when this code
> >>used XML::Parser instead of XML::LibXML...
>
> >>Thanks in advance for any suggestions.
>
> > I have found a temporary solution. Except for the namespace prefix
> > that works ("MyApp" in the example above), I removed all other
> > prefixes from the XPath. (Of course, optionally, I could remove the
> > working prefix as well.)
>
> > It's still not clear to me why this happened, so any suggestions are
> > still welcome. Thanks!
>
> http://www.perlmonks.org/?node_id=555011might be relevant
>
> The last time I used XML::LibXML to parse some received XML I found it
> easiest to just perform a namespacectomy on the XML first :-)
>
> Elsewhere I've read that you can use XPaths like
> "//*[name()='prefix:ElementName']"

Thanks for the response. Unfortunately, I've seen these suggestions
while searching Google myself, and I haven't been able to solve my
issue. I also take back the temporary "solution" that I posted --
that didn't work either, I found out. (It just got rid of the error
message.)

It works when I register namespaces using registerNs(<prefix>, <uri/
url>). However, I have to manually registerNs every prefix...


Posted by Ian Wilson on July 6, 2007, 7:12 am
Please log in for more thread options


Andrew wrote:
>

<description of problem with LibXML snipped>

>>
>> http://www.perlmonks.org/?node_id=555011might be relevant
>>
>> The last time I used XML::LibXML to parse some received XML I found it
>> easiest to just perform a namespacectomy on the XML first :-)
>>
>> Elsewhere I've read that you can use XPaths like
>> "//*[name()='prefix:ElementName']"
>
>
> Thanks for the response. Unfortunately, I've seen these suggestions
> while searching Google myself, and I haven't been able to solve my
> issue. I also take back the temporary "solution" that I posted --
> that didn't work either, I found out. (It just got rid of the error
> message.)
>
> It works when I register namespaces using registerNs(<prefix>, <uri/
> url>). However, I have to manually registerNs every prefix...
>

----------------------------8<----------------------
#!/usr/bin/perl
use strict;
use warnings;
use XML::LibXML;

my $xml='';
while (<DATA>) { $xml .= $_; }

$xml =~ s|<(/?)[^:]+:|<$1|gs;
my $doc = XML::LibXML->new->parse_string($xml);

my $tagname = 'RecalcMgr';
my @nodes = $doc->getElementsByTagName($tagname);
my $count = @nodes;
print "Found $count occurrences of '$tagname'\n";

__DATA__
<MyApp:Config>
<MyApp:FilterMgr>
...
</MyApp:FilterMgr>
<MyApp:DBConnMgr>
<RecalcMgr:RecalcMgr>
...
</RecalcMgr:RecalcMgr>
</MyApp:DBConnMgr>
</MyApp:Config>
----------------------------8<----------------------

Not nice but sufficed for my purposes.

Similar ThreadsPosted
Help! SOAP::Lite: How to set the namespace prefix for the request? June 18, 2006, 2:54 pm
namespace declarations in LibXML April 15, 2006, 12:48 am
"Undefined subroutine" November 21, 2004, 8:48 pm
Getting "undefined sysmbol February 8, 2005, 8:12 am
GD-1.38: Undefined symbol: .__fixsfsi July 12, 2004, 10:43 am
undefined symbol: gdImageCreateFromGif June 14, 2007, 1:00 pm
SOAP::Lite " December 1, 2006, 5:13 am
Mail::IMAPClient folders($prefix) March 24, 2007, 10:18 pm
HTTP::Response decoded_content is undefined March 28, 2007, 8:04 am
Can't call method on an undefined value at June 27, 2005, 2:56 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap