Hi - I'm using XML::LibXML to read/parse various xml files. I'm running into an issue where the xml files contain namespaces in different places (either at the top of page or within the xml tags). Additionally, the namespace numbers change per xml file for the same tags. So I wrote a script to parse the namespaces and use them accordingly. The script only works for when the namespaces are at the top of the page. When the namespaces are within the tags, I'm running into the following error:

XPath error : Undefined namespace prefix
error : xmlXPathCompiledEval: evaluation failed

The code is straight forward, but I must be missing something obvious. I tried putting in the full page, changing "//" to "/" with full path. I tried other tags at different levels in the xml hierarchy, but the only time it works is when there isn't a namespace used. Any ideas on how to get this working? Thanks.
#$rpt is just a namespace number retrieved from sub and is correct for + each file. $nsdevices="//ns" . $rpt. ":device"; my @devices = $doc->findnodes($nsdevices); print "nsdevices:$nsdevices\n";

XML snippet for where it fails
<?xml version='1.0' encoding='utf-8'?><Notify xmlns:xsd="http://www.w3 +.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-inst +ance" xmlns="http://docs.oasis-open.org/wsn/b-2"> <NotificationMessage> <Topic Dialect="http://docs.oasis-open.org/wsn/t-1/TopicExpression/Sim +ple">TOPICNAME</Topic> <ProducerReference> <Address xmlns="http://www.w3.org/2005/08/addressing">address</Address +> <Metadata xmlns="http://www.w3.org/2005/08/addressing"> <ns2:MessageID xmlns:ns2="http://www.w3.org/2005/08/addressing">msgid< +/ns2:MessageID> </Metadata> </ProducerReference> <Message> <ns1:rpt xmlns:ns1="http://www.url.com/path/for/rpt"> <ns1:reportObject> <ns1:device timestamp="2016-01-01T00:00:00.000-00:00">

In reply to XML::LibXML & namespaces by breezykatt

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.