Commit 1cb6ad27 authored by Srikanth Chikkulapelly's avatar Srikanth Chikkulapelly
Browse files

Added FindNodesNS function, that finds nodes based on XPATH query in the...

Added FindNodesNS function, that finds nodes based on XPATH query in the specified namespace and not in the node namespace.
parent 530e492a
......@@ -11,7 +11,7 @@ use Exporter;
use vars qw(@ISA @EXPORT);
@ISA = "Exporter";
@EXPORT = qw(Parse ParseFile FindNodes FindFirst FindElement FindAttr IsLanNode IsLocalNode GetNodeId GetVirtualId GetManagerId SetText GetText CreateDocument AddElement);
@EXPORT = qw(Parse ParseFile FindNodes FindNodesNS FindFirst FindElement FindAttr IsLanNode IsLocalNode GetNodeId GetVirtualId GetManagerId SetText GetText CreateDocument AddElement);
use English;
use XML::LibXML;
......@@ -88,6 +88,33 @@ sub FindNodes($$)
}
}
# Returns a NodeList for a given XPath using a given namespace as
# context. 'n' is defined to be the prefix for the given namespace.
sub FindNodesNS($$$)
{
my ($path, $node, $nsURI) = @_;
my $result = undef;
eval {
my $xc = XML::LibXML::XPathContext->new();
if (defined($nsURI)) {
$xc->registerNs('n', $nsURI);
} else {
$path =~ s/\bn://g;
}
$result = $xc->findnodes($path, $node);
};
if ($@) {
if (! defined($nsURI)) {
$nsURI = "undefined";
}
cluck "Failed to find nodes using XPath path='$path', nsURI='$nsURI': $@\n";
return XML::LibXML::NodeList->new();
} else {
return $result;
}
}
# Returns the first Node which matches a given XPath against a given
# node. If that node is not of the specified type, returns
# undefined. Works like FindNodes.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment