LibXML "Undefined namespace prefix"

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 [at] 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.
Andrew [ Mo, 02 Juli 2007 23:42 ] [ ID #1757842 ]

Re: LibXML "Undefined namespace prefix"

On Jul 2, 5:42 pm, Andrew <acheon... [at] gmail.com> 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 [at] 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!
Andrew [ Di, 03 Juli 2007 17:09 ] [ ID #1758966 ]

Re: LibXML "Undefined namespace prefix"

Andrew wrote:
> On Jul 2, 5:42 pm, Andrew <acheon... [at] gmail.com> 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 [at] 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']"
Ian Wilson [ Di, 03 Juli 2007 18:37 ] [ ID #1758967 ]

Re: LibXML "Undefined namespace prefix"

On Jul 3, 12:37 pm, Ian Wilson <scoblo... [at] infotop.co.uk> wrote:
> Andrew wrote:
> > On Jul 2, 5:42 pm, Andrew <acheon... [at] gmail.com> 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 [at] 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...
Andrew [ Do, 05 Juli 2007 19:15 ] [ ID #1761071 ]

Re: LibXML "Undefined namespace prefix"

Andrew wrote:
> On Jul 3, 12:37 pm, Ian Wilson <scoblo... [at] infotop.co.uk> 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 [at] nodes = $doc->getElementsByTagName($tagname);
my $count = [at] 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.
Ian Wilson [ Fr, 06 Juli 2007 13:12 ] [ ID #1761961 ]

Re: LibXML "Undefined namespace prefix"

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 [at] 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...

I think I've run into the same thing. Found this comment in some old code
that uses LibXML:

# For reasons beyond my ken, XPath expressions involving
# internally-defined namespaces fail with 'undefined prefix'
# errors unless we place a dummy attribute at the root
# element. Don't even ask how many hours I spent chasing
# this.
my $attr = $dom->createAttributeNS( '', 'dummy', '' );
$dom->getDocumentElement()->setAttributeNodeNS( $attr );

May be worth a try?

Steve
Steven Hirsch [ Mi, 11 Juli 2007 13:57 ] [ ID #1765963 ]
Perl » comp.lang.perl.modules » LibXML "Undefined namespace prefix"

Vorheriges Thema: Porn free teen sex ans fuck mpeg free teen poorn free funkymonkeymail shemale
Nächstes Thema: Win32::SqlServer 2.003 releaed