Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re^2: XML::LibXML findnodes and namespaces

by shamu (Acolyte)
on Mar 29, 2007 at 20:47 UTC ( #607353=note: print w/replies, xml ) Need Help??

in reply to Re: XML::LibXML findnodes and namespaces
in thread XML::LibXML findnodes and namespaces

What I'm really trying to do is use an XPath in a findnodes call without any concern or regard for namespaces.

Using XML::LibXML::XPathContext, I was able to create a virtual mapping to my fooResponse element...I was pointing to the wrong node.

I still would like to be able to use an XPath like:


...without having to traverse the DOM and create/update namespaces along the way.

I have a big list of XPath elements that I want to use with findnodes, hopefully without having to modify them to support "local-name" and such.

Is what I'm asking that difficult to accomplish?

Replies are listed 'Best First'.
Re^3: XML::LibXML findnodes and namespaces
by roman (Monk) on Mar 30, 2007 at 11:13 UTC

    I think without modification to your XPath, the task is not only difficult, but impossible. I don't see any possibility how plain fooResponse in your XPath expression could match with namespace qualified fooResponse element in your document.

    If you want to use XPath and match names regardless namespaces, the name() test is the only solution I can find. The original XPath expressions can be turned to name() variant programmatically (below is my first try).

    sub name_matching_xpath { my ($xpath) = @_; return join '/', map { /^([a-z0-9:-]+)(.*)$/i ? "*[name() = '$1']$2" : $_; } sp +lit '/', $xpath; }

      Thanks for your help roman. I resorted to a similar solution of replacing my prefixed elements like so...

      $search_path =~ s#([^/:]*:[^/]*)#*[name() = "$1"]#g;

      I've solved my default namespace issue by just doing a namespace lookup and creating an associated prefix. Not exactly the solution I was hoping for, but it works.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2022-01-20 23:21 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (57 votes). Check out past polls.