I have been trying unsuccessfully to extract text from inside a fairly simple XML document that includes names spaces using LibXML.

Previously I had been using XPATH and it worked a treat...but the parsing was just too slow. After writing to the author (Matt Seargent) he suggested that I use LibXML as in his words it was "much much faster"

However..my XPATH expressions no longer seemed to work. I solved part of this problem by declaring a namespace but have been unable to craft the XPATH expression needed to get the text I am after.

I have simplified the problem below maintaining all the important structural elements and am hoping someone might be able to throw me a bone as to what I am doing wrong.

Example XML and the source code I am using is below

I would appreciate any thoughts at this stage as myself and Mr Google have come up empty handed.

Regards

Steddy.


=========================== <?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 XML::LibXML; my $version = "v0.1 - Dances with XML"; my $parser = XML::LibXML->new(); $parser->recover_silently(1); my $doc = $parser->parse_file('test.xml'); #<-- The above XML my $xpc = XML::LibXML::XPathContext->new($doc); $xpc->registerNs(theNS => 'http://www.wow.com/BlahML'); my @MiddleNodeBits = $xpc->findnodes('//theNS:MiddleTag'); foreach my $nodeybits (@MiddleNodeBits) { # [1] Works but only gives me "Random1". Expected Random1,2 and 3 print $nodeybits->findnodes('//@GeneralID')->string_value . "\n"; # [2] Works...but gives me the entire XML once. Not very useful. print $nodeybits->findnodes('//*')->string_value . "\n"; ############################################## # No output from lines below-Not sure why? ############################################## # [3] print $nodeybits->findnodes('//CannotGetTagA')->string_value . "\ +n"; # [4] print $nodeybits->findnodes('/RootTag/MiddleTag[1]/MyImportantNod +e[1]/CannotGetTagA[1]/text()')->string_value . "\n"; # [5] print $nodeybits->findnodes('theNS:RootTag/MiddleTag[1]/MyImportan +tNode[1]/CannotGetTagA[1]/text()')->string_value . "\n"; # [6] print $nodeybits->findnodes('//RootTag/MiddleTag[1]/MyImportantNod +e/CannotGetTagA/text()')->string_value . "\n"; } exit(0);

In reply to 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.