line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::MediaMosa::XPath::Helper; |
2
|
3
|
|
|
3
|
|
16
|
use Catmandu::Sane; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
27
|
|
3
|
3
|
|
|
3
|
|
3340
|
use XML::LibXML; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
use XML::LibXML::XPathContext; |
5
|
|
|
|
|
|
|
use Catmandu::Util qw(io :is); |
6
|
|
|
|
|
|
|
use Exporter qw(import); |
7
|
|
|
|
|
|
|
our @EXPORT_OK=qw(get_children xpath); |
8
|
|
|
|
|
|
|
our %EXPORT_TAGS = (all=>[@EXPORT_OK]); |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub get_children { |
11
|
|
|
|
|
|
|
my($xpath,$is_hash) = @_; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
my $hash = {}; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
if($xpath){ |
16
|
|
|
|
|
|
|
for my $child($xpath->find('child::*')->get_nodelist()){ |
17
|
|
|
|
|
|
|
my $name = $child->nodeName(); |
18
|
|
|
|
|
|
|
my $value = $child->textContent(); |
19
|
|
|
|
|
|
|
if($is_hash){ |
20
|
|
|
|
|
|
|
$hash->{ $name } = $value; |
21
|
|
|
|
|
|
|
}else{ |
22
|
|
|
|
|
|
|
$hash->{$name} //= []; |
23
|
|
|
|
|
|
|
push @{ $hash->{$name} },$value if is_string($value); |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
$hash; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
sub xpath { |
31
|
|
|
|
|
|
|
my $str = $_[0]; |
32
|
|
|
|
|
|
|
my $xpath; |
33
|
|
|
|
|
|
|
if(is_scalar_ref($str)){ |
34
|
|
|
|
|
|
|
my $xml = XML::LibXML->load_xml(IO => io($str)); |
35
|
|
|
|
|
|
|
$xpath = XML::LibXML::XPathContext->new($xml); |
36
|
|
|
|
|
|
|
}elsif(-f $str){ |
37
|
|
|
|
|
|
|
my $xml = XML::LibXML->load_xml(location => $str); |
38
|
|
|
|
|
|
|
$xpath = XML::LibXML::XPathContext->new($xml); |
39
|
|
|
|
|
|
|
}elsif(is_glob_ref($str)){ |
40
|
|
|
|
|
|
|
my $xml = XML::LibXML->load_xml(IO => io($str)); |
41
|
|
|
|
|
|
|
$xpath = XML::LibXML::XPathContext->new($xml); |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
return $xpath; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
1; |