Solved

XML::LibXML and Xpath syntax How do I get attribute of sibling

Posted on 2016-08-15
  • Perl
  • XML
  • Scripting Languages
  • Programming Languages-Other
  • Shell Scripting
  • +1
2
116 Views
Last Modified: 2016-08-21
This is follow on fromThis Question

In the xml bellow I need to locate the next 'link' after I've located 'images' within the parent 'entry'


I found this link that suggest can use "following-sibling" can someone see what I've  done wrong?  

note I've striped the xml down to simplify

Perl script
 
!C:\strawberry\perl\bin\perl.exe

use 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");


foreach my $Entry ($xpc->findnodes("//dft:feed/dft:entry")) {

     foreach my $Images ($xpc->findnodes("/dft:title[not(\@type='text')]", $Entry)) {
         my $ImageVal = $Images->textContent;

          ####  This finds all the Images
         print "ImageVal $ImageVal\n";
         my $link = $Images->following-sibling::link[\@href];
         print "link $link\n";
         
     }

}

Open in new window


georss.xml
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml" >
  <entry>
    <title>fileName1.jpg</title>
     <link href="PathTo/fileName1.jpg" />
  </entry>
  <entry>
    <title>fileName2.jpg</title>
     <link href="PathTo/fileName2.jpg" />
  </entry>
  <entry>
    <title>fileName3.jpg</title>
     <link href="PathTo/fileName3.jpg" />
  </entry>
  <entry>
    <title type="text">fileName.pdf</title>
     <link type="application/pdf"  href="PathTo/fileName.pdf" />
  </entry>
    <georss:where>
     <point xmlns="http://www.opengis.net/gml" srsName="urn:ogc:crs:EPSG:4326">
      <pos>45.256 -71.92</pos>
     </point>
  </georss:where>

</feed>

Open in new window

0
Comment
Question by:trevor1940
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 41763918
Try it like this:
foreach my $Entry ($xpc->findnodes("//dft:feed/dft:entry")) {

     foreach my $Images ($xpc->findnodes("./dft:title[not(\@type='text')]", $Entry)) {
         my $ImageVal = $Images->textContent;

          ####  This finds all the Images
         print "ImageVal $ImageVal\n";
         my $link = $xpc->findvalue('following-sibling::dft:link[1]/@href', $Images);
         print "link $link\n";
         
     }

}

Open in new window

0
 
LVL 1

Author Closing Comment

by:trevor1940
ID: 41764348
That worked thank you
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Linux Mint 18 31 78
App holding yum lock unable to update my rpm package 1 36
video edge NVR Device Discovery Problem 4 34
xpath sql query 2008 8 43
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now