Andreas Pinkert wrote:


> Now I want my application to get the author of the book called "lala".


##### quirky hack:
$:.unshift 'rexml/1.2.5/'
require 'rexml/document'
include REXML

str=
'<?xml version="1.0" ?>
<INVENTORY>
<BOOK>
<TITLE>lala</TITLE>
<AUTHOR>hans</AUTHOR>
</BOOK>
<BOOK>
<TITLE>lele</TITLE>
<AUTHOR>hens</AUTHOR>
</BOOK>
</INVENTORY>'

doc = Document.new str
XPath.each doc,'//AUTHOR' do |res|
   if res.previous_sibling.previous_sibling.text == 'lala'
     puts res.text
   end
end
#####

The above really is neither general nor elegant. Some depends on your 
DTD, some on REXML.


> In my perfect world something like that would work:
> 
> doc.each_element("BOOK") {|book| puts book.element("AUTHOR") if 
>   "lala" == book.element("TITLE")}


In my perfect world, the whole thing would be solved with one short line 
of code, and a nice XPath. Perhaps that's possible with more recent 
versions of REXML.


> And it seems impossible to get the parent of an element.


.parent.

Do p element.methods:

["whitespace", "raw", "add_attributes", "each_element_with_text", 
"delete_elemen
t", "delete_namespace", "namespace", "context", "text=", "write", 
"add_attribute
", "next_element", "prefixes", "__9409__", "root", "context=", 
"has_attributes?"
, "get_text", "get_elements", "add_namespace", "elements", "add_text", 
"has_text
?", "each_element_with_attribute", "attributes", "delete_attribute", 
"text", "pr
evious_element", "each_element", "has_elements?", "add_element", 
"__9377__", "cl
one", "name=", "prefix=", "expanded_name", "local_name", "has_name?", 
"fully_exp
anded_name", "prefix", "name", "replace_child", "delete_at", "each", 
"size", "in
sert_before", "delete", "add", "to_a", "insert_after", "[]", "push", 
"[]=", "eac
h_child", "each_index", "<<", "index", "delete_if", "unshift", "min", 
"find_all"
, "entries", "each_with_index", "map", "detect", "include?", "collect", 
"find",
"member?", "reject", "grep", "max", "select", "sort", 
"previous_sibling", "next_
sibling", "bytes", "parent", "previous_sibling=", "next_sibling=", 
"document", "
parent=", "remove", "replace_with", "read_with_substitution", "to_s", 
"previous_
sibling_node", "write_with_substitution", "indent", "next_sibling_node", 
"==", "
===", "singleton_methods", "tainted?", "eql?", "methods", "dup", "nil?", 
"is_a?"
, "method", "instance_of?", "class", "instance_variables", "send", 
"frozen?", "_
_id__", "__send__", "=~", "hash", "protected_methods", "untaint", 
"respond_to?",
  "public_methods", "taint", "equal?", "kind_of?", "inspect", "display", 
"instanc
e_eval", "type", "freeze", "id", "extend", "private_methods"]


Tobi


-- 
http://www.pinkjuice.com/