# REXML, XPath and Namespace

**URL:** <https://rubytalk.org/t/rexml-xpath-and-namespace/38402>\
**Category:** ruby-talk\
**Created:** [16 June 2007 10:30 UTC](https://rubytalk.org/t/rexml-xpath-and-namespace/38402 "2007-06-16T10:30:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hinsen](https://avatars.discourse-cdn.com/v4/letter/h/ed8c4c/32.png) [@hinsen](https://rubytalk.org/u/hinsen)\
**Post date:** [16 June 2007 10:30 UTC](https://rubytalk.org/t/rexml-xpath-and-namespace/38402/1 "2007-06-16T10:30:43Z")

</div>

Hi.

I want to parse an xml file using REXML::XPath. I can easily navigate  
through the xml-tree with methods like 'XPath.match(xml, "child::\*")'  
and. But i can't  
access nodes with the structure 'prefix:name'. Even this call is not  
working (it doesn't hit any node):

sub\_tags = REXML::XPath.match(xml, "/rss/channel/item/dc:date", {  
"dc"=\>"[http://purl.org/dc/elements/1.1/](http://purl.org/dc/elements/1.1/)" })

Do you know what i'am doing wrong?  
Thanks

\<?xml version="1.0" encoding="UTF-8"?\>  
\<rss xmlns:geo="[http://www.w3.org/2003/01/geo/wgs84\_pos#](http://www.w3.org/2003/01/geo/wgs84_pos#)" version="2.0"  
xmlns:dc="[http://purl.org/dc/elements/1.1/](http://purl.org/dc/elements/1.1/)"  
xmlns:xCal="urn:ietf:params:xml:ns:xcal"\>  
&nbsp;&nbsp;\<channel\>  
&nbsp;&nbsp;&nbsp;&nbsp;\<title\>Zvents search for events matching within 60 miles of  
6\</title\>  
&nbsp;&nbsp;&nbsp;&nbsp;\<link\>[http://www.zvents.com/search?.](http://www.zvents.com/search?.)..\</link\>  
&nbsp;&nbsp;&nbsp;&nbsp;\<ttl\>60\</ttl\>  
&nbsp;&nbsp;&nbsp;&nbsp;\<description\>Zvents search for even Germany  
returesults.\</description\>  
&nbsp;&nbsp;&nbsp;&nbsp;\<stream\_count\>2\</stream\_count\>  
&nbsp;&nbsp;&nbsp;&nbsp;\<item\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<title\>MIR DETSTWA\</title\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<description\>International Exhibition of Goods for  
Childg\</description\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<summary\>\</summary\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<phone\>\</phone\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<guid\>[http://www.zvents.com/events/show/896719-MIR-DETSTWA](http://www.zvents.com/events/show/896719-MIR-DETSTWA)\</guid\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<link\>[http://www.zvents.com/events/show/896719-MIR-DETSTWA](http://www.zvents.com/events/show/896719-MIR-DETSTWA)\</link\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<dc:creator\>mBLAST\</dc:creator\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<id\>896719\</id\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<parent\_id\>\</parent\_id\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<series\_count\>\</series\_count\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<geo:lat\>50.12\</geo:lat\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<geo:long\>8.68\</geo:long\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<pubDate\>Wed, 30 May 2007 07:11:46 +0000\</pubDate\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<dc:date\>2007-05-30T07:11:46+0000\</dc:date\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<xCal:dtstart\>2007-10-03 00:00:00 +0000\</xCal:dtstart\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<xCal:dtend\>2007-10-07 00:00:00 +0000\</xCal:dtend\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Keith\_Fahlgren](https://avatars.discourse-cdn.com/v4/letter/k/e9c0ed/32.png) [@Keith\_Fahlgren](https://rubytalk.org/u/Keith_Fahlgren)\
**Post date:** [21 June 2007 16:55 UTC](https://rubytalk.org/t/rexml-xpath-and-namespace/38402/2 "2007-06-21T16:55:13Z")

</div>

This seemed to work for me:  
require 'rexml/document'  
=\> true

INPUT = \<\<INPUT\_XML  
\<rss xmlns:geo="[http://www.w3.org/2003/01/geo/wgs84\_pos#&quot](http://www.w3.org/2003/01/geo/wgs84_pos#&quot); version="2.0"  
xmlns:dc="[http://purl.org/dc/elements/1.1/&quot](http://purl.org/dc/elements/1.1/&quot);  
xmlns:xCal="urn:ietf:params:xml:ns:xcal"\>  
&nbsp;&nbsp;\<channel\>  
&nbsp;&nbsp;&nbsp;&nbsp;\<title\>Zvents search for events matching within 60 miles of 6\</title\>  
&nbsp;&nbsp;&nbsp;&nbsp;\<item\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<pubDate\>Wed, 30 May 2007 07:11:46 +0000\</pubDate\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<dc:date\>2007-05-30T07:11:46+0000\</dc:date\>  
&nbsp;&nbsp;&nbsp;&nbsp;\</item\>  
&nbsp;&nbsp;\</channel\>  
\</rss\>  
INPUT\_XML  
=\> "\<rss xmlns:geo=\"[http://www.w3.org/2003/01/geo/wgs84\_pos#\&quot](http://www.w3.org/2003/01/geo/wgs84_pos#%5C&quot);  
version=\"2.0\"\nxmlns:dc=\"[http://purl.org/dc/elements/1.1/\"\nxmlns:xCal=\"urn:ietf:params:xml:ns:xcal\&quot;&gt;\\n](http://purl.org/dc/elements/1.1/%5C%22%5Cnxmlns:xCal=%5C%22urn:ietf:params:xml:ns:xcal%5C&quot;&gt;%5C%5Cn)  
\<channel\>\n \<title\>Zvents search for events matching within 60  
miles of 6\</title\>\n \<item\>\n \<pubDate\>Wed, 30 May 2007  
07:11:46 +0000\</pubDate\>\n  
\<dc:date\>2007-05-30T07:11:46+0000\</dc:date\>\n \</item\> \n  
\</channel\> \n\</rss\> \n"

xml = REXML::Document.new(INPUT)  
=\> \<UNDEFINED\> ... \</\>

a = REXML::XPath.match(xml, "/rss/channel/item/dc:date", {  
"dc"=\>"[http://purl.org/dc/elements/1.1/&quot](http://purl.org/dc/elements/1.1/&quot); })  
=\> [\<dc:date\> ... \</\>]

a.to\_s  
=\> "\<dc:date\>2007-05-30T07:11:46+0000\</dc:date\>"

> **···**
>
> On 6/16/07, hinsen \<h.gildhoff@web.de\> wrote:
> 
> > Even this call is not  
> > working (it doesn't hit any node):
