XML::XPath::Helper::String - Helper functions for xpath expression.
Version 1.00
use XML::LibXML; use XML::XPath::Helper::String qw(quoted_string one_of_quoted); my @nodes = $myXML->findnodes('subnode=' . quoted_string("value with '")); my @other_nodes = $myXML->findnodes('foo_node[' . one_of_quoted("bar_node", "x'''y", "z") ']');
This modules provides functions that helps building xpath expressions. The functions are exported on demand, you can use the :all tag to export all functions.
:all
quoted_string(ARG)
This function makes it easier to create xpath expressions seaching for values that contains single quotes. The problem with xpath is that it does not support an escape character, so you have to use a concat(...) in such cases. This function creates a concat(...) expression if needed.
concat(...)
ARG must be a string or a reference to an array of strings. If it is a string, the the function returns a string. If it is an array reference, then the function returns an array reference.
ARG
For each string in ARG the function does the following: if the string does not contain any single quote, then the result is the string enclosed in single quotes. So this
print(quoted_string("hello"), "\n");
prints:
'hello'
But this
print(quoted_string("'this' that \"x\" '''"), "\n");
concat("'",'this',"'",' that "x" ',"'''")
one_of_quoted(VALUES, NAME)
one_of_quoted(VALUES)
This function creates an xpath expressions checking if NAME contains one of the values in VALUES. It calls quoted_string to handle single quotes correctly. Example:
NAME
VALUES
quoted_string
This
print(one_of_quoted(["'a'", "b'''cd", "e"], "foo"), "\n");
prints
foo=concat("'",'a',"'") or foo=concat('b',"'''",'cd') or foo='e'
If NAME is not specified, then the function returns a closure that takes one argument and produces the expression when called. Example:
my $closure = one_of_quoted(["'a'", "b'''cd", "e"]); print($closure->("foo"), "\n", $closure->("bar"), "\n");
foo=concat("'",'a',"'") or foo=concat('b',"'''",'cd') or foo='e' bar=concat("'",'a',"'") or bar=concat('b',"'''",'cd') or bar='e'
not_one_of_quoted(VALUES, NAME)
not_one_of_quoted(VALUES)
Like one_of_quoted but creates an xpath expressions checking if NAME contains none of the values in VALUES. Example:
one_of_quoted
This:
print(not_one_of_quoted("foo", "'a'", "b'''cd", "e"), "\n");
foo!=concat("'",'a',"'") and foo!=concat('b',"'''",'cd') and foo!='e'
Abdul al Hazred, <451 at gmx.eu>
<451 at gmx.eu>
Please report any bugs or feature requests to bug-xml-xpath-helper-string at rt.cpan.org, or through the web interface at https://rt.cpan.org/NoAuth/ReportBug.html?Queue=XML-XPath-Helper-String. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-xml-xpath-helper-string at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc XML::XPath::Helper::String
You can also look for information at:
RT: CPAN's request tracker (report bugs here)
https://rt.cpan.org/NoAuth/Bugs.html?Dist=XML-XPath-Helper-String
CPAN Ratings
https://cpanratings.perl.org/d/XML-XPath-Helper-String
Search CPAN
https://metacpan.org/release/XML-XPath-Helper-String
This software is copyright (c) 2022 by Abdul al Hazred.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
XML::LibXML
1 POD Error
The following errors were encountered while parsing the POD:
'=item' outside of any '=over'
To install XML::XPath::Helper::String, copy and paste the appropriate command in to your terminal.
cpanm
cpanm XML::XPath::Helper::String
CPAN shell
perl -MCPAN -e shell install XML::XPath::Helper::String
For more information on module installation, please visit the detailed CPAN module installation guide.