XPath Syntax help in conjunction with perl XML::LibXML
I’m trying to extract some data from the georss.xml file below using the perl script I’m struggling to get the value of the following
<feed sc:countryCodes=”US” # need US
<category term="Wanted" label="Wanted" url="http://otherurl.com/pathto/FINDME" /> # need value of term where value of url contains FINDME
The value of the 2 entry/title fields but know which one has type="text" / value = fileName.pdf
<point xmlns="http://www.opengis.net/gml" srsName="urn:ogc:crs:EPSG:4326> # need value of srsName
perl script
#!C:\strawberry\perl\bin\perl.exeuse strict;use warnings;use XML::LibXML;use XML::LibXML::XPathContext;use Data::Dump qw(dump);my $filename = 'georss.xml';my $dom = XML::LibXML->load_xml(location => $filename);my $xpc = XML::LibXML::XPathContext->new($dom);$xpc->registerNs(dft => "http://www.w3.org/2005/Atom");$xpc->registerNs(georss => "http://www.georss.org/georss");my $title = $xpc->findnodes('//dft:feed/dft:title');print "title $title\n"; # GOOD#my $point = $xpc->findnodes('//dft:feed/georss:where/dft:Point/dft:pos'); ## this doesn't find anythingmy $point = $xpc->findnodes('//dft:feed/georss:where'); $point =~ s/^\s*//; # clean white space unsure why but had loads $point =~ s/\s*$//; print "point $point\n"; # GOODforeach my $Etitle ($dom->findnodes('//dft:feed/dft:title)) { print "Etitle $Etitle\n"; # prints <title type="text">fileName.pdf</title> my $EtitleVal = $Etitle->findvalue('./title'); if($Etilte =~ m/jpg/){ print "Image $EtitleVal\n"; # prints Image } elsif($Etilte =~ m/pdf/){ print "PDF $EtitleVal\n"; # prints <title type="text">fileName.pdf</title> } }
So are you saying by getting the my $EtitleVal = $Etitle->data; then testing / pattern matching for m/jpg/ is the way to do this and not test if"<title type="text">?
foreach my $Entry ($dom->findnodes("//dft:feed/dft:entry")) { foreach my $Images ($dom->findnodes("//dft:title[not(\@type='text')]", $Entry)) { my $ImageVal = $Images->textContent; #### This finds all the Images }}
Is there a way of testing if <entry> contains a namespace or xsi:schemaLocation I searched google but found nothing possibly because not sure what to search for ie XPATH node has namespace
I just noticed I missed one follow up question apparently.
I am not sure on how to test for namespace nodes
For a parser it is only relevant to know which is the default namespace and which prefixes are bound to which namespace at a specific location, regardless of at which level the binding is declared
note that XPath allows you to look for all namespace nodes
//namespace::*
that could help you to get the namespace node on your current node
I closed this because the task has been pulled however may need to revisit
For my own interest
If you can't test for namespace nodes directly but can find the child via "(\@type='text')" then get the parent <entry> then search back down for <link> thus ensuring <type> and other siblings are dealt with together?
Any idea about the other issues?