Subject: | XML::DOM::XPath methods do not work on objects created by XML::DOM::Document->new |
Hey Michel sorry it's me again (ouch). :-P
The following simple piece of code does not work:
-- CODE BEGINS --
use XML::DOM::XPath;
$xml = new XML::DOM::Document;
$root = $xml->createElement('root');
$xml->appendChild($root);
print "hello world" if $xml->exists('root');
-- CODE ENDS --
This will produce the following error message:
Can't call method "exists" on an undefined value at /usr/local/perl561/lib/site_perl/5.6.1/XML/DOM/XPath.pm line 57.
But the same method call on the same XML structure generated by XML::DOM::Parser works flawlessly:
-- CODE BEGINS --
use XML::DOM::XPath;
$xml = new XML::DOM::Parser->parse('<root/>');
print "hello world" if $xml->exists('root');
-- CODE ENDS --
This will print the string "hello world" as intended.
The workaround for me right now is to convert the DOM object back to XML first and then let the parser parse into a DOM object again just for XPath query purposes:
-- CODE BEGINS --
use XML::DOM::XPath;
$xml = new XML::DOM::Document;
$root = $xml->createElement('root');
$xml->appendChild($root);
print "hello world" if XML::DOM::Parser->new->parse($xml->toString)->exists('root');
-- CODE ENDS --
And this is obviously crude and inefficient (but works).
Again, I'm running Perl 5.6.1 built for i686-linux, with XML-DOM-1.43, XML-XPath-1.13 and XML-DOM-XPath-0.06 installed.
Thanks a lot, Michel!