First, there's an error in the XML.

<MyImportantNode> xsi:type="foo" GeneralID="Random1" >
should be
<MyImportantNode xsi:type="foo" GeneralID="Random1" >

(Same goes for the other two.)

4 of the 5 paths you tried in the loop start with "/". That means they start looking at the root of the tree. That instantly makes them wrong since you obviously want something that's in or below $nodeybits.

Another major problem is that you look for the nodes in the wrong namespace throughout the body of the loop. You don't even use the xpc!


<?xml version="1.0" encoding="UTF-8"?> <RootTag SchemaVersion="1.1" xmlns="http://www.wow.com/BlahML" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocati +on="http://www.wow.com/BlahML BlahML1_1.xsd"> <MyTagA Tag="XXX">Some Text A</MyTagA> <MyTagB Tag="XXX">Some Text A</MyTagB> <MiddleTag xsi:type="foo" magicvalue="wow"> <MyImportantNode xsi:type="foo" GeneralID="Random1" > <CannotGetTagA>YYYYYYYYYYY</CannotGetTagA> <CannotGetTagB>ZZZZZZZZZZZ</CannotGetTagB> </MyImportantNode> <MyImportantNode xsi:type="foo" GeneralID="Random2" > <CannotGetTagA>YYYYYYYYYY22</CannotGetTagA> <CannotGetTagB>ZZZZZZZZZZ22</CannotGetTagB> </MyImportantNode> <MyImportantNode xsi:type="foo" GeneralID="Random3" > <CannotGetTagA>YYYYYYYYY333</CannotGetTagA> <CannotGetTagB>ZZZZZZZZZ333</CannotGetTagB> </MyImportantNode> </MiddleTag> </RootTag>
use strict; use warnings; use feature qw( say ); use XML::LibXML; my $parser = XML::LibXML->new(); my $doc = $parser->parse_file('test.xml'); my $xpc = XML::LibXML::XPathContext->new($doc); $xpc->registerNs(theNS => 'http://www.wow.com/BlahML'); for my $important_node ( $xpc->findnodes('//theNS:MiddleTag/theNS:MyImportantNode') ) { say $important_node->getAttribute('GeneralID'); for my $cannot_get_a_node ( $xpc->findnodes('theNS:CannotGetTagA', $important_node) ) { say $cannot_get_a_node->textContent(); } }

In reply to Re: LibXML, Namespaces, xpath expression - This should be simple. by ikegami
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.