XML::LibXML + XML::LibXML::XPathContext: can this be simplified?by Darkwing (Sexton)
|on Oct 08, 2019 at 13:16 UTC||Need Help??|
Darkwing has asked for the wisdom of the Perl Monks concerning the following question:
i'm maintaining a perl application (a command line tool using XML::LibXML) where users can write a config file containing rules. If present, then this rules are applied to the input (an xml file) and matches are reported.
Simplified example (foo.xml):
Each object in this simplified example has an id and a version and my have a refs referencing other objects by one or more id.
Actually, these rules are basically xpath expressions checking nodes of an object, and i use findnodes() from XML::LibXML to evaluate. But now it is required to also check nodes of referenced objects. For example "The versions of the referenced objects should all be the same as the version of the current object". It seems to me that this cannot be done via xpath and XML::LibXML's findnodes - right?
I finally found the following solution, it uses XML::LibXML::XPathContext custom xpath functions:
(in my application, the xpath expression in $xc->findnodes(...) would be taken from the user's config file)
It works, but I find it ugly that one must pass the actual node and the root node. Any way to get around this? Are there other possible improvements?
PS: it would not be practical to change to another xml module since my application consists of many, many classes and <XML::LibXML> is heavily used.