Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: XML Namespaces

by haukex (Archbishop)
on Oct 11, 2017 at 07:51 UTC ( [id://1201143]=note: print w/replies, xml ) Need Help??


in reply to XML Namespaces

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).

Replies are listed 'Best First'.
Re^2: XML Namespaces
by simsrw73 (Novice) on Oct 11, 2017 at 21:07 UTC

    Thanks, haukex and everyone else that responded. I am stuck with the xml output as it is produced by another application, Logos Bible Software. (concordance of various bible translations that I'm munging, then comparing against MS Office spellchecker to create custom dictionary files.)

    I've made the change re: $xpc->find. Thanks for pointing that out; that makes sense.

    The code works and this isn't a mission critical script so it shouldn't bother me, but it's still a little unclear and I hate that. But I've been writing some test scripts and some of it is starting to sync in. Thanks for confirming that I'm on the right path.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1201143]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-04-24 06:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found