2

We usually write our Search Path in findnodes() function as below

//parentNode[subNode/text() = 'CPUUSAGE']/subNode

what is I want to match a part of the text here and find all the nodes?

something like

//parentNode[subNode/text() =~ '/CPUUSAGE'/]/subNode

Obviously this is Invalid xPath...

Any thoughts how to achieve this?

I know I can first find the nodes and then try to match the textContent. But Can we do that in one shot directly in findnodes()?

matt
  • 78,533
  • 8
  • 163
  • 197
marks
  • 45
  • 4

2 Answers2

2

XPath 1.0 (which libxml implements) doesn’t include any built-in support for regular expressions. In the example you give, which uses a fairly simple regular expression, you could use the contains function to achieve a similar result:

//parentNode[subNode[contains(text(), 'CPUUSAGE')]]/subNode

(As an aside that’s an odd expression – you’d probably really want something like //parentNode/subNode[contains(text(), 'CPUUSAGE')] but I realise it’s just an example.)

There are some other string functions that could be useful in creating other simple queries.

You could create your own custom XPath function to filter nodes based on a regular expression, in fact the docs for the Perl LibXML module includes an example of doing just that.

XPath 2.0 does have support for using regular expressions with a group of string functions. Unless you have an XPath 2.0 processor that will not be too useful.

matt
  • 78,533
  • 8
  • 163
  • 197
0

XML::Twig has support for regular expressions in its xpaths.

The following is an xpath that I used in an answer to this SO question: Updating xml attribute value based on other with Perl

project[string(path) =~ /\bopensource\b/]/revision

I also created a second answer so that I could experiment with how XML::LibXML could be used to solve the same problem, and in that case I just iterated over all projects and did the regex filtering manually.

Community
  • 1
  • 1
Miller
  • 34,962
  • 4
  • 39
  • 60