Hi

There was something wrong with you example XML, the MyImportantNode elements were closed before defining the attributes.

Assuming that was an error in posting, I think that your confusion comes from a number of reasons:

In 1 it is something to do with the way you are chaining findnodes and string_value. If you replace it with:

say "[1a]" . $nodeybits->findvalue('//@GeneralID');

you'll get Random1Random2Random3. If you want to deal with each value, you'll need a loop:

say "[1b]" . $_->findvalue('.') foreach $nodeybits->findnodes('*/@GeneralID');

Having said that, it's a bit confusing that you're using nodeybits (the MiddleTag node) but then running XPath expressions beginning with "//", which will start at the root.

2 does what I'd expect, given the above: the first tag that matches that will be the root element, and calling string_value on that will return the entire text of the file.

The remaining ones are all because you're using $nodeybits. This is not an XPathContext object. For the examples you've given, you could use $xpc. But presumably this is cut down from a bigger program where you're doing XPath relative to the node you're looking at in the loop.

EDIT see ikegami's reply

my $inner_xpc = XML::LibXML::XPathContext->new($nodeybits); $inner_xpc->registerNs( theNS => 'http://www.wow.com/BlahML'); # [3] print $inner_xpc->findnodes('//theNS:CannotGetTagA')->string_value . " +\n";

Worked for me and you could use $inner_xpc for your remaining queries, being careful to be consistent about using the namespace prefix (in 5, you have theNS:RootTag but miss it off for all the other element names).

HTH

FalseVinylShrub

Disclaimer: Please review and test code, and use at your own risk... If I answer a question, I would like to hear if and how you solved your problem.


In reply to Re: LibXML, Namespaces, xpath expression - This should be simple. by FalseVinylShrub
in thread LibXML, Namespaces, xpath expression - This should be simple. by alittlebitdifferent

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.