if not supplied, will be set to the default value. If a String, the name of
this object will be set to the argument. If an Element, the object will be shallowly cloned; name,
attributes, and namespaces will be copied. Children will not be
copied. If a Source, the source will be scanned
and parsed for an Element, and all child
elements will be recursively parsed as well.
parent:
if supplied, must be a Parent, and will be used
as the parent of this object.
context:
If supplied, must be a hash containing context items. Context items
include:
:respect_whitespace the value of this is :all or an array
of strings being the names of the elements to respect whitespace for.
Defaults to :all.
:compress_whitespace the value can be :all or an array of
strings being the names of the elements to ignore whitespace on. Overrides
:respect_whitespace.
:ignore_whitespace_nodes the
value can be :all or an array of strings being the names of the
elements in which to ignore whitespace-only nodes. If this is set, Text nodes which contain only whitespace will not be
added to the document tree.
:raw can be :all, or an array of strings being the names
of the elements to process in raw mode. In raw mode, special characters in
text is not converted to or from entities.
A REXML::StreamListener (or something
that implements the same method set) that will be notified when events
occur.
s = Source.new( "<a>some text</a>" )
l = Listener.new
class << l
def tag_start name, attributes
p "NEW ELEMENT, NAME OF #{name}"
end
end
Element::parse_stream s, l
Evaluates to the root element of the document that this element belongs to.
If this element doesn't belong to a document, but does belong to another Element, the parent's root will be returned, until
the earliest ancestor is found.
d = Document.new '<a><b><c/></b></a>'
a = d[1] ; c = a[1][1]
d.root # These all evaluate to the same Element,
a.root # namely, <a>
c.root #
Evaluates to true if whitespace is respected for this element.
This is the case if:
Neither :respect_whitespace nor :compress_whitespace has
any value
The context has :respect_whitespace set to :all or an
array containing the name of this element, and
:compress_whitespace isn't set to :all or an array
containing the name of this element.
The evaluation is tested against expanded_name, and so is namespace
sensitive.
Evaluates to true if raw mode is set for this element. This is the
case if the context has :raw set to :all or an array
containing the name of this element.
The evaluation is tested against expanded_name, and so is namespace
sensitive.
Evalutas to the URI for a prefix, or the empty string if no such namespace
is declared for this element. Evaluates recursively for ancestors. Returns
the default namespace, if there is one.
prefix:
the prefix to search for. If not supplied, returns the default namespace if
one exists
Returns:
the namespace URI as a String, or nil if no such namespace exists.
a = Element.new("a")
a.add_namespace("foo", "bar") # shorthand for previous line
a.add_namespace("twiddle")
Removes a namespace from this node. This only works if the namespace is
actually declared in this node. If no argument is passed, deletes the
default namespace.
Must be an Element, String, or
Integer. If Element, the element is
removed. If String, the element is found (via XPath) and removed. <em>This means that any
parent can remove any descendant.<em> If Integer, the Element indexed by that number will be removed.
A convenience method which returns the String value of the first
child text element, if one exists, and nil otherwise.
Note that an element may have multiple Text
elements, perhaps separated by other children. Be aware that this
method only returns the first Text node.
doc = Document.new "<p>some text <b>this is bold!</b> more text</p>"
# The element 'p' has two text elements, "some text " and " more text".
doc.root.text #-> "some text "
Returns the first child Text node, if any, or
nil otherwise. This method returns the actual Text node, rather than the String content.
doc = Document.new "<p>some text <b>this is bold!</b> more text</p>"
# The element 'p' has two text elements, "some text " and " more text".
doc.root.get_text.value #-> "some text "
Sets the first Text child of this object. See
text() for a discussion about Text children.
If a Text child already exists, the child is
replaced by this content. This means that Text
content can be deleted by calling this method with a nil argument. In this
case, the next Text child becomes the first Text child. In no case is the order of any siblings
disturbed.
text:
If a String, a new Text child is created and added
to this Element as the first Text child. If Text, the text
is set as the first Child element. If nil, then
any existing first Text child is removed.
Adds an attribute to this element, overwriting any existing attribute by
the same name.
key:
can be either an Attribute or a String. If an
Attribute, the attribute is added to the list
of Element attributes. If String, the argument
is used as the name of the new attribute, and the value parameter must be
supplied.
value:
Required if key is a String, and ignored if the first argument is
an Attribute. This is a String, and is used as
the value of the new Attribute.
either an Attribute or a String. In either
case, the attribute is found by matching the attribute name to the
argument, and then removed. If no attribute is found, no action is taken.
Returns:
the attribute removed, or nil if this Element
did not contain a matching attribute