To correct my first sentence of the parent and expand the explanation a little:

Your line $doc->documentElement->setNamespace("http://www.crtp.it","p"); does not work, and the documentation of XML::LibXML::Element->setNamespace explains why:

The function fails if it is required to create a declaration associating the prefix with the namespace URI but the element already carries a declaration with the same prefix but different namespace URI.

Which means you can't re-use the "p" prefix as it's already in use on the element; you'd have to pick a different prefix, then the setNamespace call would work and you wouldn't need to do registerNs on two namespaces.

However, you would still need to make one XML::LibXML::XPathContext->registerNs set-up as shown in the parent.

So TIMTOWTDI; personally I wouldn't manipulate the XML, but adjust my XPath to fit the XML (this is the solution shown in the parent, and you could remove the setNamespace call from your code).


In reply to Re^2: 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.