Hey Monks,

I'm trying to migrate some code from XML::Twig to XML::LibXML for it's performance advantages. I've been trying with great difficulty to use XML::LibXML to parse dynamically created XML in which the element namespaces are not known prior to parsing. I don't care what the default namespaces are, as long as they don't get in the way.

I'm attempting to use findnodes with the recommended (from perlmonks) XML::LibXML::XPathContext module for dealing with the namespaces. Using registerNs doesn't appear to have any visible effect when printing the XML as a string, so I use $node->setNamespace to accomplish that.

my $parser = XML::LibXML->new(); my $doc = $parser->parse_file($file); my $t = $doc->getDocumentElement; foreach my $node ($t->findnodes('TransactionList/Transaction/XML')) { my $xml = $node->firstChild; my $xc = XML::LibXML::XPathContext->new($xml); foreach (qw( summary histFile fooScore )) { $xc->registerNs($_,"urn:$_"); $node->setNamespace("urn:$_",$_,0); } my $path = '//fooResponse/info/fooTransaction/transactionDetail/histFi +le:transactionSummary/summary:PacManScore'; my @nodes = $xc->findnodes($path);
I've read the two related posts to this topic: 555011 242028

...but I'm still stuck. It appears that when I encounter an element with a default namespace, findnodes refuses to match it without registering some prefix to the URI. I just want to be able to retrieve the nodes using an XPath like:

//fooResponse/info/fooTransaction/transactionDetail/histFile:transacti +onSummary/summary:PacManScore

And if I want to create new elements with arbitrary namespaces, I don't want to have to manage the namespaces at every turn.

Why does this work?
//*/info/fooTransaction/transactionDetail/*
But not this?
//*/info/fooTransaction/transactionDetail/histFile:transactionSummary/ +*

Please help! :)

I have an XML file like:
<Extract> <Service> <Name>FooService</Name> <Type>Response</Type> </Service> <TransactionList> <Transaction> <Tran_Id>90dc-2f633156cbf6</Tran_Id> <XML> <fooResponse xmlns="http://some.arbitrary.url/" xmlns:ns="http +://another.arbitrary.url/" xmlns:xsi="http://www.w3.org/2001/XMLSchem +a-instance"> <info xmlns=""> <servicePreferences> <ns:requestingCode xsi:nil="true"/> </servicePreferences> <fooTransaction xmlns:summary="foo"> <transactionDetail> <histFile:transactionSummary xmlns:histFile="http://ye +t.another.arbitrary.url/"> <summary:PacManScore>95.133</summary:PacManScore> <summary:QBertScore>95.133</summary:QBertScore> <summary:FroggerScore>95.133</summary:FroggerScore> </histFile:transactionSummary> </transactionDetail> </fooTransaction> </info> </fooResponse> </XML> </Transaction> </TransactionList> </Extract>

In reply to XML::LibXML findnodes and namespaces by shamu

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.