Method: REXML::Element#text

Defined in:
lib/rexml/element.rb

#text(path = nil) ⇒ Object

:call-seq:

text(xpath = nil) -> text_string or nil

Returns the text string from the first text node child in a specified element, if it exists, nil otherwise.

With no argument, returns the text from the first text node in self:

d = REXML::Document.new "<p>some text <b>this is bold!</b> more text</p>"
d.root.text.class # => String
d.root.text       # => "some text "

With argument xpath, returns text from the first text node in the element that matches xpath:

d.root.text(1) # => "this is bold!"

Note that an element may have multiple text nodes, possibly separated by other non-text children, as above. Even so, the returned value is the string text from the first such node.

Note also that the text note is retrieved by method get_text, and so is always normalized text.



1023
1024
1025
1026
# File 'lib/rexml/element.rb', line 1023

def text( path = nil )
  rv = get_text(path)
  rv&.value
end