Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Dear monks,

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):

<objects> <obj> <id>1</id> <version>2</version> <refs> <id>2</id> <id>5</id> </refs> </obj> <obj> <id>2</id> <version>2</version> </obj> <obj> <id>3</id> <version>2</version> <refs> <id>2</id> <id>4</id> </refs> </obj> <obj> <id>4</id> <version>2</version> </obj> <obj> <id>5</id> <version></version> </obj> </objects>

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:

use strict; use warnings; use XML::LibXML; use XML::LibXML::XPathContext; sub getObjAll { my ($topList, $actNode, $idNodeList, $expression, $value) = @_; my $top = $topList->[0]; $expression =~ s/:1:/'$value'/g; my $failed; foreach my $idNode (@{$idNodeList}) { my $id = $idNode->textContent; my $nodes = $top->findnodes("/objects/obj[id='$id' and $expression +]"); return unless @{$nodes}; } return $idNodeList; }; my $dom = XML::LibXML->load_xml(location => 'foo.xml'); my $xc = XML::LibXML::XPathContext->new($dom); $xc->registerFunction('getObjAll', \&getObjAll); my @nodes = $xc->findnodes("/objects/obj[getObjAll(/, ., ./refs/id, " . "'./version=:1:', ./version)]"); foreach my $node (@nodes) { print $node->toString(1); print "\n"; }

(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.

In reply to XML::LibXML + XML::LibXML::XPathContext: can this be simplified? by Darkwing

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (10)
    As of 2020-02-27 15:25 GMT
    Find Nodes?
      Voting Booth?
      What numbers are you going to focus on primarily in 2020?

      Results (120 votes). Check out past polls.