Sha256: 29cf2cefd1ad142004ce3eeff3432feee52d6c531e680990f3094a498af63613

Contents?: true

Size: 1.01 KB

Versions: 4

Compression:

Stored size: 1.01 KB

Contents

#:invisible:
$:.unshift "../lib" #<=
#:visible:
require 'xml/xxpath'

d=REXML::Document.new <<EOS
  <foo>
    <bar>
      <baz key="work">Java</baz>
      <baz key="play">Ruby</baz>
    </bar>
    <bar>
      <baz key="ab">hello</baz>
      <baz key="play">scrabble</baz>
      <baz key="xy">goodbye</baz>
    </bar>
    <more>
      <baz key="play">poker</baz>
    </more>
  </foo>
EOS


####read access
path=XML::XXPath.new("/foo/bar[2]/baz")

## path.all(document) gives all elements matching path in document
path.all(d)#<=

## loop over them
path.each(d){|elt| puts elt.text}#<=

## the first of those
path.first(d)#<=

## no match here (only three "baz" elements)
path2=XML::XXPath.new("/foo/bar[2]/baz[4]")
path2.all(d)#<=

#:handle_exceptions:
## "first" raises XML::XXPathError in such cases...
path2.first(d)#<=
#:no_exceptions:

##...unless we allow nil returns
path2.first(d,:allow_nil=>true)#<=

##attribute nodes can also be returned
keysPath=XML::XXPath.new("/foo/*/*/@key")

keysPath.all(d).map{|attr|attr.text}#<=

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
xml-mapping-0.9.1 examples/xpath_usage.intin.rb
pdf-labels-1.0.0 vendor/xml-mapping/examples/xpath_usage.intin.rb
xml-mapping-0.8 examples/xpath_usage.intin.rb
xml-mapping-0.8.1 examples/xpath_usage.intin.rb