List Info

Thread: LibXML using Xpath isn't returning a result set




LibXML using Xpath isn't returning a result set
country flaguser name
Australia
2007-09-16 23:40:31
Hi
I have a problem with an Xpath query that doesn't return a
result.
The xpath looks like this 
/Grid/Cluster[ClusterName/text() =
'CQUGRID01']/Database[Name/text() = 'CS89A2']/LinuxAppServer
and this works when using query the xml file with XPath
Explorer in Eclipse.
When I use that in a perl program I am working on it fails
to get a result
my perl command from the debugging looks like this
In the trace below I have removed the quotes from around the
xpath varables, which had no change on the problem.


43:             my $query = "/Grid/Cluster[ClusterName/text() =
$clusternode]/Database[Name/text() =
$_[0]]/LinuxAppServer";
  DB<1> n
main::find_appserv_node(XpathTest.pl:44):
44:             print $query . "n";
  DB<1> n
/Grid/Cluster[ClusterName/text() =
CQUGRID03]/Database[Name/text() = CS89A2]/LinuxAppServer
main::find_appserv_node(XpathTest.pl:45):
45:             appnodes1 =
$config->findnodes($query);
  DB<1> n
main::find_appserv_node(XpathTest.pl:46):
46:             print appnodes1 ;
  DB<1> n
main::find_appserv_node(XpathTest.pl:47):
47:             if (appnodes1){
  DB<1> n
main::find_appserv_node(XpathTest.pl:41):
41:                     print "Getting AS ClusterNode:
" . $clusternode ."n";
  DB<1> n
Getting AS ClusterNode: CQUGRID01
main::find_appserv_node(XpathTest.pl:43):
43:             my $query = "/Grid/Cluster[ClusterName/text() =
$clusternode]/Database[Name/text() =
$_[0]]/LinuxAppServer";
  DB<1> n
main::find_appserv_node(XpathTest.pl:44):
44:             print $query . "n";
  DB<1> n
/Grid/Cluster[ClusterName/text() =
CQUGRID01]/Database[Name/text() = CS89A2]/LinuxAppServer
main::find_appserv_node(XpathTest.pl:45):
45:             appnodes1 =
$config->findnodes($query);
  DB<1> s
XML::LibXML::Node::findnodes(/usr/lib/perl5/site_perl/5.8.5/
i386-linux-thread-multi/XML/LibXML.pm:878):
878:        my ($node, $xpath) = _;
  DB<1> x _
0  XML::LibXML:ocument=
SCALAR(0x8ee5d04)
   -> 149996728
1  '/Grid/Cluster[ClusterName/text() =
CQUGRID01]/Database[Name/text() = CS89A2]/LinuxAppServer'
  DB<2> n
XML::LibXML::Node::findnodes(/usr/lib/perl5/site_perl/5.8.5/
i386-linux-thread-multi/XML/LibXML.pm:879):
879:        my nodes = $node->_findnodes($xpath);
  DB<2> s
XML::LibXML::Node::findnodes(/usr/lib/perl5/site_perl/5.8.5/
i386-linux-thread-multi/XML/LibXML.pm:880):
880:        if (wantarray) {
  DB<2> x $node 
0  XML::LibXML:ocument=
SCALAR(0x8ee5d04)
   -> 149996728
  DB<3> x $xpath
0  '/Grid/Cluster[ClusterName/text() =
CQUGRID01]/Database[Name/text() = CS89A2]/LinuxAppServer'
  DB<4> n
XML::LibXML::Node::findnodes(/usr/lib/perl5/site_perl/5.8.5/
i386-linux-thread-multi/XML/LibXML.pm:881):
881:            return nodes;
  DB<4> x nodes
  empty array
  DB<5> n
main::find_appserv_node(XpathTest.pl:46):
46:             print appnodes1 ;
  DB<5> n
main::find_appserv_node(XpathTest.pl:47):
47:             if (appnodes1){
  DB<5> n
main::find_appserv_node(XpathTest.pl:60):
60:             if (appnodes) {
  DB<5> n
main::find_appserv_node(XpathTest.pl:70):
70:     return appservnodes;
  DB<5> n
main::(XpathTest.pl:23):        foreach my $testnode(testnodes
){
  DB<5> n
Debugged program terminated.  Use q to quit or R to
restart,
  use O inhibit_exit to avoid stopping after program
termination,
  h q, h R or h O to get additional info.  
  DB<5> 
This is very frustrating, and looks like a bug in LibXML,
but I will leave that as a doubt.
Maybe there is a better way to find the elements and return
the data.

Any help appreciated

Cheers 

-- 
Peter McLarty
Technology Consultant
Brisbane Australia
MACM MIEE MACS
Membership Development Coordinator IEEE QLD Section
GOLD Affinity Group Chair IEEE QLD
===========================================
peter.mclartypameacs.com     +61402094238
===========================================
The sages do not consider that making no mistakes is a
blessing. They 
believe, rather, that the great virtue of man lies in his
ability to 
correct his mistakes and continually make a new man of
himself.

-- Wang Yang-Ming (1472-1529) Chinese Philosopher

_______________________________________________
Perl-XML mailing list
Perl-XMLlistserv.ActiveState.com
To unsubscribe: http:/
/listserv.ActiveState.com/mailman/mysubs

Re: LibXML using Xpath isn't returning a result set
country flaguser name
Germany
2007-09-16 23:54:13
* Peter McLarty wrote:
>/Grid/Cluster[ClusterName/text() =
'CQUGRID01']/Database[Name/text()
> = 'CS89A2']/LinuxAppServer and this works when using
query the xml
>file with XPath Explorer in Eclipse.

So how does the XML actually look like? The debugger session
is not
really helpful, if you could give a minimal XML file and a
script that
queries it, it would be much easier to see what's going on.
You might,
for example, have white space in the <Name> element
which your XPath
explorer ignores for some reason, as in

  <Name>
    CS89A2
  </Name>

Your expression would never match this. It is also possible
that you
meant to write Name, considering that ClusterName is also
an attribute.
You might also have a problem involving namespaces, e.g. if
the <Grid>
element has an xmlns='...' attribute.
-- 
Björn Höhrmann · mailto:bjoernhoehrmann.de · http://bjoern.hoehrmann.de

Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de

68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/ 
_______________________________________________
Perl-XML mailing list
Perl-XMLlistserv.ActiveState.com
To unsubscribe: http:/
/listserv.ActiveState.com/mailman/mysubs

Re: LibXML using Xpath isn't returning a result set
country flaguser name
Australia
2007-09-17 03:48:20
Hi

I have put together a simple example of the problem using some data from "XML in a nutshell

Here is the xml file
<;?xml version="1.0"?>
<?xml-stylesheet type="application/xml" href="people.xsl"?>;
<!DOCTYPE people [
 <!ATTLIST homepage xlink:type CDATA #FIXED "simple"
 &nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; &nbsp; &nbsp; xmlnslink CDATA #FIXED "http://www.w3.org/1999/xlink"&gt;
 ;<!ATTLIST person id ID #IMPLIED&gt;
]>
<people>
&nbsp; &nbsp; 
  <person born="1912" died="1954" id="p342"&gt;
 ; &nbsp; <name&gt;
   ; &nbsp; <first_name>Alan</first_name>
 &nbsp; &nbsp;  <last_name>Turing</last_name>
 &nbsp;  </name&gt;
 ; &nbsp; <!-- Did the word computer scientist exist in Turing's day? --&gt;
   ; <profession>computer scientist&lt;/profession>
&nbsp;   <profession>mathematician&lt;/profession>
&nbsp;   <profession>cryptographer&lt;/profession>
&nbsp;   <homepage xlink:href="http://www.turing.org.uk/"/>;
  </person>
&nbsp; &nbsp; 
  <person born="1918" died="1988" id="p4567">
&nbsp; &nbsp; <name&gt;
   ; &nbsp; <first_name>Richard</first_name>;
 &nbsp;   ; <middle_initial>;&#x50;</middle_initial>;
 &nbsp;   ; <last_name>Feynman</last_name>
 &nbsp;  </name&gt;
 ; &nbsp; <profession>physicist</profession&gt;
 ; &nbsp; <hobby&gt;Playing the bongoes<;/hobby>
  </person>
&nbsp; &nbsp; 
</people>
Here is a program to run that exhibits my problem.
As I understand what I am working with this should return a node list with a single node in it in the find_people_surname module




#!/usr/bin/perl -w


use strict;
use Getopt::Long;
use XML::LibXML;
use File::Copy;
use Cwd;

use constant INITDIR => './initfiles';

my $config_file = "people.xml";

my person = find_people_surname('Alan', $config_file);


sub find_people_surname{
 &nbsp; 
 &nbsp;  my people_born;
   ; my people_born_found;&nbsp; &nbsp;
 &nbsp;  my $count;
  ;  my $parser = XML::LibXML->new();
   ; my $config = $parser-&gt;parse_file($_[1]);
 &nbsp;  my people1;
 &nbsp;  my $people_born;
 ; &nbsp;
 &nbsp; 
 &nbsp;  people_born = find_people($_[1]);
 &nbsp; 
 &nbsp;  foreach $people_born(people_born) {
   ;  &nbsp;  my $query = "/people/person[born/text() = '$people_born']/name[first_name/text() ='$_[0]']/last_name";
 &nbsp;   &nbsp;  my people = $config ->findnodes($query);
   ;  &nbsp;  if (people) {
   ;  &nbsp;   &nbsp;  people1 = people;
  ;   &nbsp;  }
   ; }
   ; if (people1) {
   ;  &nbsp;  $count = 0;
 ; &nbsp;  &nbsp;  foreach my $people(people1){
 &nbsp;   &nbsp;   &nbsp;   $people_born_found[$count] = $people;
 &nbsp;   &nbsp;   &nbsp;   print "people Born $people_born_found[$count] n  ";
 ; &nbsp;  &nbsp;  }
   ; }

return people_born_found;
}

sub find_people{
   ;
 &nbsp;  my $parser = XML::LibXML->new();
   ;
 &nbsp;  my $config = $parser-&gt;parse_file($_[0]);
 &nbsp;  my $people ="";
&nbsp; &nbsp; my people;
  ;  my people1;
 &nbsp;  my $count;
  
 &nbsp;  my $query = "/people/person/born";
&nbsp;  
 &nbsp;  people = $config-&gt;findnodes($query);
 &nbsp;  if (people) {
   ;  &nbsp;  $count = 0;
 ; &nbsp;  &nbsp;  foreach my $people(people) {
   ;  &nbsp;  $people1[$count]= $people-&gt;string_value;
&nbsp; &nbsp;  &nbsp;  $count++;&nbsp;  
 &nbsp;   &nbsp;  }
   ; }
   ;

return people1;
}

Bjoern Hoehrmann wrote:
hive.bjoern.hoehrmann.de" type="cite">
* Peter McLarty wrote:
  
/Grid/Cluster[ClusterName/text() = 'CQUGRID01']/Database[Name/text()
= 'CS89A2']/LinuxAppServer and this works when using query the xml
file with XPath Explorer in Eclipse.
    

So how does the XML actually look like? The debugger session is not
really helpful, if you could give a minimal XML file and a script that
queries it, it would be much easier to see what's going on. You might,
for example, have white space in the <Name&gt; element which your XPath
explorer ignores for some reason, as in

  <Name&gt;
    CS89A2
  </Name&gt;

Your expression would never match this. It is also possible that you
meant to write Name, considering that ClusterName is also an attribute.
You might also have a problem involving namespaces, e.g. if the <Grid&gt;
element has an xmlns='...' attribute.
  

-- 
Peter McLarty
Technology Consultant
Brisbane Australia

MACM MIEE MACS
Membership Development Coordinator IEEE QLD Section
GOLD Affinity Group Chair IEEE QLD
===========================================
pameacs.com">peter.mclartypameacs.com     +61402094238
===========================================
The sages do not consider that making no mistakes is a blessing. They 
believe, rather, that the great virtue of man lies in his ability to 
correct his mistakes and continually make a new man of himself.

-- Wang Yang-Ming (1472-1529) Chinese Philosopher
Re: LibXML using Xpath isn't returning a result set
country flaguser name
Czech Republic
2007-09-17 05:01:38
Hi,

replace born/text() with just born to make your example
work.

Assuming that this is taken from a larger project where
something 
else is going on in each of the procedures that we do not
see here,
I'm restraining myself from commenting on your programming
style. 
Otherwise your code looks way too complicated for the simple
task 
it achieves.

-- Petr

On po 17. září 2007, Peter McLarty wrote:
> <?xml version="1.0"?>
> <?xml-stylesheet type="application/xml"
href="people.xsl"?>
> <!DOCTYPE people [
>  <!ATTLIST homepage xlink:type CDATA #FIXED
"simple"
>                   xmlnslink
CDATA #FIXED
> "http://www.w3.
org/1999/xlink"> <!ATTLIST person id ID
#IMPLIED>
> ]>
> <people>
>    
>   <person born="1912"
died="1954" id="p342">
>     <name>
>       <first_name>Alan</first_name>
>       <last_name>Turing</last_name>
>     </name>
>     <!-- Did the word computer scientist exist in
Turing's day?
> --> <profession>computer
scientist</profession>
>    
<profession>mathematician</profession>
>    
<profession>cryptographer</profession>
>     <homepage xlink:href="http://www.turin
g.org.uk/"/>
>   </person>
>    
>   <person born="1918"
died="1988" id="p4567">
>     <name>
>       <first_name>Richard</first_name>
>      
<middle_initial>&#x50;</middle_initial>
>       <last_name>Feynman</last_name>
>     </name>
>     <profession>physicist</profession>
>     <hobby>Playing the bongoes</hobby>
>   </person>
>    
> </people>
> Here is a program to run that exhibits my problem.
> As I understand what I am working with this should
return a node
> list with a single node in it in the
find_people_surname module
>
>
>
>
> #!/usr/bin/perl -w
>
>
> use strict;
> use Getopt::Long;
> use XML::LibXML;
> use File::Copy;
> use Cwd;
>
> use constant INITDIR => './initfiles';
>
> my $config_file = "people.xml";
>
> my person = find_people_surname('Alan', $config_file);
>
>
> sub find_people_surname{
>    
>     my people_born;
>     my people_born_found;  
>     my $count;
>     my $parser = XML::LibXML->new();
>     my $config = $parser->parse_file($_[1]);
>     my people1;
>     my $people_born;
>    
>    
>     people_born = find_people($_[1]);
>    
>     foreach $people_born(people_born) {
>         my $query = "/people/person[born/text() =
> '$people_born']/name[first_name/text()
='$_[0]']/last_name";
>         my people = $config ->findnodes($query);
>         if (people) {
>             people1 = people;
>         }
>     }
>     if (people1) {
>         $count = 0;
>         foreach my $people(people1){
>              $people_born_found[$count] =
$people;
>              print "people Born
$people_born_found[$count] n  ";
>         }
>     }
>
> return people_born_found;
> }
>
> sub find_people{
>    
>     my $parser = XML::LibXML->new();
>    
>     my $config = $parser->parse_file($_[0]);
>     my $people ="";
>     my people;
>     my people1;
>     my $count;
>    
>     my $query = "/people/person/born";
>    
>     people = $config->findnodes($query);
>     if (people) {
>         $count = 0;
>         foreach my $people(people) {
>         $people1[$count]= $people->string_value;
>         $count++;  
>         }
>     }
>    
>
> return people1;
> }
_______________________________________________
Perl-XML mailing list
Perl-XMLlistserv.ActiveState.com
To unsubscribe: http:/
/listserv.ActiveState.com/mailman/mysubs
[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )