File Coverage

blib/lib/WWW/Webrobot/UseXPath.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package WWW::Webrobot::UseXPath;
2 1     1   4 use strict;
  1         2  
  1         36  
3 1     1   5 use warnings;
  1         2  
  1         33  
4              
5             # Author: Stefan Trcek
6             # Copyright(c) 2004 ABAS Software AG
7              
8              
9 1     1   1042 use UNIVERSAL qw(isa); # *isa = \&UNIVERSAL::isa;
  1         14  
  1         8  
10 1     1   1914 use XML::XPath;
  0            
  0            
11             use XML::XPath::XMLParser;
12              
13              
14             =head1 NAME
15              
16             WWW::Webrobot::UseXPath - Apply XPath expressions to an xml string
17              
18             =head1 SYNOPSIS
19              
20             use WWW::Webrobot::UseXPath;
21             WWW::Webrobot::UseXPath -> new($xml) -> extract($xpath_expression);
22              
23             =head1 DESCRIPTION
24              
25             Apply XPath expressions to an xml string.
26              
27             =head1 METHODS
28              
29             =over
30              
31             =item WWW::Webrobot::UseXPath -> new ($xml)
32              
33             Allocate an XPath object for the xml-string $xml
34              
35             =cut
36              
37             sub new {
38             my ($proto, $xml) = @_;
39             my $self = {
40             _xml => $xml,
41             _xpath => XML::XPath -> new(xml => $xml),
42             };
43             return bless ($self, ref($proto) || $proto);
44             }
45              
46             =item $self -> extract ($expr)
47              
48             Apply an xpath expression $expr for this object.
49             The result is of type string.
50              
51             =cut
52              
53             sub extract {
54             my ($self, $expr) = @_;
55             my $node = eval {
56             $self -> {_xpath} -> find($expr);
57             };
58             return undef if $@;
59              
60             my $ret = "";
61             if ($node -> isa("XML::XPath::NodeSet")) {
62             $ret = join "\n", map { XML::XPath::XMLParser::as_string($_) } $node -> get_nodelist();
63             }
64             else {
65             $ret = $node -> value();
66             }
67             return $ret;
68             }
69              
70             =back
71              
72             =cut
73              
74             1;