The "right" way to go about it is what you're doing, register all the namespaces in the context object, and use all those prefixes in the XPath expression. This way, you don't have to care if the namespace prefixes in the target XML change (however unlikely that may be), as long as the URIs stay the same.

Note the documentation of XML::LibXML::Node's findnodes: "There are several possible ways to deal with namespaces in XPath: ... The recommended way is to use the XML::LibXML::XPathContext module ... Another possibility is to use prefixes declared in the queried document (if known)."

When you write $cell->find('./ss:Data'), that's actually not using the context, instead it's using the second aforementioned option of using the prefixes declared in the document. The "right" way is to use the context object for every query: $xpc->find('./ss:Data',$cell). I tested this by changing every one of the prefixes in your XML file, and the find(nodes) still worked fine. This hopefully also answers your question: yes, the context object is necessary in this scenario.

By the way, if you have the possibility to save this Excel spreadsheet as XLS or XLSX, there's Spreadsheet::Read, which will parse those formats (via Spreadsheet::ParseExcel resp. Spreadsheet::ParseXLSX).


In reply to Re: XML Namespaces by haukex
in thread XML Namespaces by simsrw73

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.