xpath find root child

7 views
Skip to first unread message

paulf

unread,
Nov 24, 2009, 6:50:44 AM11/24/09
to nokogiri-talk
doc.root.children.each { |child| puts child.name }
finds an import node
but
doc.xpath("//import")
does not find it
Should this form of xpath find it or how should it be used.
Or is there a problem with names without a prefix in a namespaced
document?

Thanks
Paul F Fraser

Mike Dalessio

unread,
Nov 24, 2009, 9:30:31 AM11/24/09
to nokogiri-talk
Hi Paul,

You should check out the searching tutorial. We have a section on XPath and namespaces.

http://nokogiri.org/tutorials/searching_a_xml_html_document.html

Cheers,
-mike



--

You received this message because you are subscribed to the Google Groups "nokogiri-talk" group.
To post to this group, send email to nokogi...@googlegroups.com.
To unsubscribe from this group, send email to nokogiri-tal...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nokogiri-talk?hl=en.





--
mike dalessio
mi...@csa.net

Paul F Fraser

unread,
Nov 24, 2009, 6:16:54 PM11/24/09
to nokogi...@googlegroups.com
Mike Dalessio wrote:
> Hi Paul,
>
> You should check out the searching tutorial. We have a section on
> XPath and namespaces.
>
> http://nokogiri.org/tutorials/searching_a_xml_html_document.html
>
> Cheers,
> -mike
>
Hi Mike,
http://gist.github.com/242324 has an example of problem.
Running ubuntu 9.04, jRuby 1.4
Any suggestions?
Paul

Aaron Patterson

unread,
Nov 24, 2009, 6:22:20 PM11/24/09
to nokogi...@googlegroups.com
Your document has xmlns default namespaces. Change your queries to
have 'xmlns' as the prefix and you should be OK:

//xmlns:annotation
//xmlns:import

You can read more info here:

http://nokogiri.org/tutorials/searching_a_xml_html_document.html

Specifically check out the namespaces section.

Hope that helps.

--
Aaron Patterson
http://tenderlovemaking.com/

Mike Dalessio

unread,
Nov 24, 2009, 6:23:06 PM11/24/09
to nokogi...@googlegroups.com
On Tue, Nov 24, 2009 at 6:16 PM, Paul F Fraser <pa...@a2zliving.com> wrote:
>
Hi Mike,
http://gist.github.com/242324 has an example of problem.
Running ubuntu 9.04, jRuby 1.4
Any suggestions?
Paul


The namespace usually needs to be included as part of the xpath query, so this will work:

puts doc.at_xpath("//xmlns:annotation", "xmlns" => "http://www.w3.org/2001/XMLSchema")
puts doc.at_xpath("//xmlns:import", "xmlns" => "http://www.w3.org/2001/XMLSchema")

Note that the prefix used in the query doesn't need to correlate to the namespace in the root node; only the URL does. So this also works:

puts doc.at_xpath("//foo:annotation", "foo" => "http://www.w3.org/2001/XMLSchema")
puts doc.at_xpath("//foo:import", "foo" => "http://www.w3.org/2001/XMLSchema")


But any namespaces declared on the root node are "included" automatically in an xpath search. So the shortest solution is this:

puts doc.at_xpath("//xmlns:annotation")
puts doc.at_xpath("//xmlns:import")

-m

Paul F Fraser

unread,
Nov 24, 2009, 6:58:07 PM11/24/09
to nokogi...@googlegroups.com
Thanks, Aaron & Mike,
For some reason the explanation in the tutorial on searching with
default namespaces did not 'click' with me for my case, but now very clear.
Regards
Paul
Reply all
Reply to author
Forward
0 new messages