LibXML "Undefined namespace prefix"
LibXML "Undefined namespace prefix"
am 02.07.2007 23:42:57 von Andrew
I am using LibXML to parse an XML file. The XML file is like so...
|
|
| ...
|
|
|
| ...
|
|
|
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.
Re: LibXML "Undefined namespace prefix"
am 03.07.2007 17:09:10 von Andrew
On Jul 2, 5:42 pm, Andrew wrote:
> I am using LibXML to parse an XML file. The XML file is like so...
>
> |
> |
> | ...
> |
> |
> |
> | ...
> |
> |
> |
>
> 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!
Re: LibXML "Undefined namespace prefix"
am 03.07.2007 18:37:13 von Ian Wilson
Andrew wrote:
> On Jul 2, 5:42 pm, Andrew wrote:
>
>>I am using LibXML to parse an XML file. The XML file is like so...
>>
>>|
>>|
>>| ...
>>|
>>|
>>|
>>| ...
>>|
>>|
>>|
>>
>>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']"
Re: LibXML "Undefined namespace prefix"
am 05.07.2007 19:15:59 von Andrew
On Jul 3, 12:37 pm, Ian Wilson wrote:
> Andrew wrote:
> > On Jul 2, 5:42 pm, Andrew wrote:
>
> >>I am using LibXML to parse an XML file. The XML file is like so...
>
> >>|
> >>|
> >>| ...
> >>|
> >>|
> >>|
> >>| ...
> >>|
> >>|
> >>|
>
> >>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(,
url>). However, I have to manually registerNs every prefix...
Re: LibXML "Undefined namespace prefix"
am 06.07.2007 13:12:57 von Ian Wilson
Andrew wrote:
> On Jul 3, 12:37 pm, Ian Wilson wrote:
>
>>
>> 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(,
> url>). However, I have to manually registerNs every prefix...
>
----------------------------8<----------------------
#!/usr/bin/perl
use strict;
use warnings;
use XML::LibXML;
my $xml='';
while () { $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__
...
...
----------------------------8<----------------------
Not nice but sufficed for my purposes.
Re: LibXML "Undefined namespace prefix"
am 11.07.2007 13:57:42 von Steven Hirsch
Andrew wrote:
> I am using LibXML to parse an XML file. The XML file is like so...
>
> |
> |
> | ...
> |
> |
> |
> | ...
> |
> |
> |
>
> 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...
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