REXML, XPath and Namespace

Discussion in 'Ruby' started by hinsen, Jun 16, 2007.

  1. hinsen

    hinsen Guest

    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/" })

    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#" version="2.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:xCal="urn:ietf:params:xml:ns:xcal">
    <channel>
    <title>Zvents search for events matching within 60 miles of
    6</title>
    <link>http://www.zvents.com/search?...</link>
    <ttl>60</ttl>
    <description>Zvents search for even Germany
    returesults.</description>
    <stream_count>2</stream_count>
    <item>
    <title>MIR DETSTWA</title>
    <description>International Exhibition of Goods for
    Childg</description>
    <summary></summary>
    <phone></phone>
    <guid>http://www.zvents.com/events/show/896719-MIR-DETSTWA</guid>
    <link>http://www.zvents.com/events/show/896719-MIR-DETSTWA</link>
    <dc:creator>mBLAST</dc:creator>
    <id>896719</id>
    <parent_id></parent_id>
    <series_count></series_count>
    <geo:lat>50.12</geo:lat>
    <geo:long>8.68</geo:long>
    <pubDate>Wed, 30 May 2007 07:11:46 +0000</pubDate>
    <dc:date>2007-05-30T07:11:46+0000</dc:date>
    <xCal:dtstart>2007-10-03 00:00:00 +0000</xCal:dtstart>
    <xCal:dtend>2007-10-07 00:00:00 +0000</xCal:dtend>
    ...

    --
    Posted via http://www.ruby-forum.com/.
     
    hinsen, Jun 16, 2007
    #1
    1. Advertising

  2. Re: REXML, XPath and Namespace

    On 6/16/07, hinsen <> wrote:
    > Even this call is not
    > working (it doesn't hit any node):


    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#" version="2.0"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:xCal="urn:ietf:params:xml:ns:xcal">
    <channel>
    <title>Zvents search for events matching within 60 miles of 6</title>
    <item>
    <pubDate>Wed, 30 May 2007 07:11:46 +0000</pubDate>
    <dc:date>2007-05-30T07:11:46+0000</dc:date>
    </item>
    </channel>
    </rss>
    INPUT_XML
    => "<rss xmlns:geo=\"http://www.w3.org/2003/01/geo/wgs84_pos#\"
    version=\"2.0\"\nxmlns:dc=\"http://purl.org/dc/elements/1.1/\"\nxmlns:xCal=\"urn:ietf:params:xml:ns:xcal\">\n
    <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/" })
    => [<dc:date> ... </>]

    a.to_s
    => "<dc:date>2007-05-30T07:11:46+0000</dc:date>"
     
    Keith Fahlgren, Jun 21, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Damphyr
    Replies:
    2
    Views:
    163
    Damphyr
    Jul 16, 2003
  2. Daniel Berger

    rexml error - REXML::Validation

    Daniel Berger, Oct 12, 2004, in forum: Ruby
    Replies:
    2
    Views:
    168
    Henrik Horneber
    Oct 12, 2004
  3. Mark Volkmann

    REXML and XPath

    Mark Volkmann, Jan 27, 2006, in forum: Ruby
    Replies:
    4
    Views:
    290
    Mark Volkmann
    Jan 28, 2006
  4. Thibaut Barrère

    [newbie] REXML, each_element and XPath

    Thibaut Barrère, Mar 8, 2006, in forum: Ruby
    Replies:
    3
    Views:
    140
    Thibaut Barrère
    Mar 8, 2006
  5. Phlip
    Replies:
    0
    Views:
    161
    Phlip
    Jan 15, 2008
Loading...

Share This Page