From: Aaron Patterson Date: 2009-08-03T07:25:45+09:00 Subject: Re: [libxml]: Can't find nodes using XPath, namespaces mess On Sat, Aug 01, 2009 at 05:33:31AM +0900, Stanislaw Wozniak wrote: > Hi, > > I am having problems accessing elements in the XML documents using > XPath. My xml document looks like that: > > > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; > xsi:schemaLocation="urn:company:platform:foundation:configuration:defn:v1" > xmlns="urn:company:platform:foundation:configuration:defn:v1"> ^^^^^ That says that all nodes inside this document (if not explicitly namespaced) belong to an implicit namespace > > > > > > > > > > > > > My XPath only works when I remove all the namespaces from the root node > but I do need to access it without modifying the xml. You need to register that namespace with the libxml xpath engine. I'm not sure how you register namespaces with libxml-ruby, but with nokogiri, I would do this: doc = Nokogiri::XML(xml) doc.xpath('//ns:attribute', 'ns' => 'urn:company:platform:foundation:configuration:defn:v1') Nokogiri will automatically register root level namespaces, so you could also do this: doc = Nokogiri::XML(xml) doc.xpath('//xmlns:attribute') I know there is a way to do this with libxml-ruby, I just don't know the syntax off the top of my head. Look through the libxml-ruby documentation for "find", and I'm sure you'll find how to register namespaces. -- Aaron Patterson http://tenderlovemaking.com/