Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: XML::LibXML findnodes and namespaces

by roman (Monk)
on Mar 29, 2007 at 10:32 UTC ( #607194=note: print w/replies, xml ) Need Help??


in reply to XML::LibXML findnodes and namespaces

I am not completely clear about your intention and may repeat something you already know:

XML::LibXML::XPathContext->registerNs doesn't modify the context node (document) in any way, it only maps the prefixes to namespaces. This mapping has nothing common with namespace mapping in any element.

When looking for nodes via:

$xc->findnodes('//*/info/fooTransaction/transactionDetail/histFile:tra +nsactionSummary/*')
the namespace qualified element names are compared. In xpath context the histFile:transactionSummary is mapped to <urn:histFile>transactionSummary, while the fully qualified name of histFile::transactionSummary element in your document is <http://yet.another.arbitrary.url/>transactionSummary, so they don't match.

In XPath you can also look by local or qualified name of the element.

# looking by local-name $xc->findnodes('//*/info/fooTransaction/transactionDetail/*[local-name +() = "transactionSummary"]/*'); # looking by qualified name $xc->findnodes('//*/info/fooTransaction/transactionDetail/*[name() = " +histFile:transactionSummary"]/*');
In this case you can call findnodes method on any node, you don't need the XML::LibXML::XPathContext with its prefix => namespace mapping:
$doc->findnodes('//*/info/fooTransaction/transactionDetail/*[name() = +"histFile:transactionSummary"]/*');

Replies are listed 'Best First'.
Re^2: XML::LibXML findnodes and namespaces
by shamu (Acolyte) on Mar 29, 2007 at 20:47 UTC

    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:

    fooResponse/info/fooTransaction/transactionDetail/histFile:transactionSummary/summary:PacManScore

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

      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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2022-01-29 00:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (74 votes). Check out past polls.

    Notices?