if the xml changes it change

The three listed changes produce semantically equivalent XML documents. For example, if you make any or all of those changes, the document will still validate perfectly fine against the same schema. If the OP doesn't have full control over the source of the XML, those changes might happen, and the XPath using just name() would break.

Even though TIMTOWTDI, I prefer writing this

my $xpc = XML::LibXML::XPathContext->new($doc); $xpc->registerNs('p', 'http://www.crtp.it'); $xpc->registerNs('p2', 'http://www.crpt.com'); my $result = $xpc->findnodes("/p2:login/p:utenti[p:email='$email' and +p:password='$digestpass']/p:admin");

over this

my $result = $doc->findnodes(qq{ /*[ local-name()="login" and namespace-uri()="http://www.crpt.com" ] /*[ local-name()="utenti" and namespace-uri()="http://www.crtp.it" ] [ *[ local-name()="email" and namespace-uri()="http://www.crtp.it" a +nd string(node())="$email" ] and *[ local-name()="password" and namespace-uri()="http://www.crtp.it +" and string(node())="$digestpass" ] ] /*[ local-name()="admin" and namespace-uri()="http://www.crtp.it" ] });

because it's shorter, has less repetition, and uses the recommended API.

The idea is to keep the xpath stuff where it belong

In the first piece of code above, the XPath stuff is already where it belongs: in an XPath expression context. From the XPath spec:

Expression evaluation occurs with respect to a context. ... The context consists of:

Also, it's what is recommended by findnodes:

There are several possible ways to deal with namespaces in XPath:

In reply to Re^4: Xpath not working by Anonymous Monk
in thread Xpath not working by njack

Title:
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, details, 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, summary, 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.