Skip to content

Commit 7fcc72b

Browse files
Enhanced RDoc for Element (ruby#48)
1 parent 2d50558 commit 7fcc72b

File tree

1 file changed

+97
-39
lines changed

1 file changed

+97
-39
lines changed

lib/rexml/element.rb

Lines changed: 97 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -288,37 +288,78 @@ def raw
288288
# Namespaces #
289289
#################################################
290290

291-
# Evaluates to an +Array+ containing the prefixes (names) of all defined
292-
# namespaces at this context node.
293-
# doc = Document.new("<a xmlns:x='1' xmlns:y='2'><b/><c xmlns:z='3'/></a>")
294-
# doc.elements['//b'].prefixes # -> ['x', 'y']
291+
# :call-seq:
292+
# prefixes -> array_of_namespace_prefixes
293+
#
294+
# Returns an array of the string prefixes (names) of all defined namespaces
295+
# in the element and its ancestors:
296+
#
297+
# xml_string = <<-EOT
298+
# <root>
299+
# <a xmlns:x='1' xmlns:y='2'>
300+
# <b/>
301+
# <c xmlns:z='3'/>
302+
# </a>
303+
# </root>
304+
# EOT
305+
# d = REXML::Document.new(xml_string, {compress_whitespace: :all})
306+
# d.elements['//a'].prefixes # => ["x", "y"]
307+
# d.elements['//b'].prefixes # => ["x", "y"]
308+
# d.elements['//c'].prefixes # => ["x", "y", "z"]
309+
#
295310
def prefixes
296311
prefixes = []
297312
prefixes = parent.prefixes if parent
298313
prefixes |= attributes.prefixes
299314
return prefixes
300315
end
301316

317+
# :call-seq:
318+
# namespaces -> array_of_namespace_names
319+
#
320+
# Returns a hash of all defined namespaces
321+
# in the element and its ancestors:
322+
#
323+
# xml_string = <<-EOT
324+
# <root>
325+
# <a xmlns:x='1' xmlns:y='2'>
326+
# <b/>
327+
# <c xmlns:z='3'/>
328+
# </a>
329+
# </root>
330+
# EOT
331+
# d = REXML::Document.new(xml_string)
332+
# d.elements['//a'].namespaces # => {"x"=>"1", "y"=>"2"}
333+
# d.elements['//b'].namespaces # => {"x"=>"1", "y"=>"2"}
334+
# d.elements['//c'].namespaces # => {"x"=>"1", "y"=>"2", "z"=>"3"}
335+
#
302336
def namespaces
303337
namespaces = {}
304338
namespaces = parent.namespaces if parent
305339
namespaces = namespaces.merge( attributes.namespaces )
306340
return namespaces
307341
end
308342

309-
# Evaluates to the URI for a prefix, or the empty string if no such
310-
# namespace is declared for this element. Evaluates recursively for
311-
# ancestors. Returns the default namespace, if there is one.
312-
# prefix::
313-
# the prefix to search for. If not supplied, returns the default
314-
# namespace if one exists
315-
# Returns::
316-
# the namespace URI as a String, or nil if no such namespace
317-
# exists. If the namespace is undefined, returns an empty string
318-
# doc = Document.new("<a xmlns='1' xmlns:y='2'><b/><c xmlns:z='3'/></a>")
319-
# b = doc.elements['//b']
320-
# b.namespace # -> '1'
321-
# b.namespace("y") # -> '2'
343+
# :call-seq:
344+
# namespace(prefix = nil) -> string_uri or nil
345+
#
346+
# Returns the string namespace URI for the element,
347+
# possibly deriving from one of its ancestors.
348+
#
349+
# xml_string = <<-EOT
350+
# <root>
351+
# <a xmlns='1' xmlns:y='2'>
352+
# <b/>
353+
# <c xmlns:z='3'/>
354+
# </a>
355+
# </root>
356+
# EOT
357+
# d = REXML::Document.new(xml_string)
358+
# b = d.elements['//b']
359+
# b.namespace # => "1"
360+
# b.namespace('y') # => "2"
361+
# b.namespace('nosuch') # => nil
362+
#
322363
def namespace(prefix=nil)
323364
if prefix.nil?
324365
prefix = prefix()
@@ -334,19 +375,24 @@ def namespace(prefix=nil)
334375
return ns
335376
end
336377

337-
# Adds a namespace to this element.
338-
# prefix::
339-
# the prefix string, or the namespace URI if +uri+ is not
340-
# supplied
341-
# uri::
342-
# the namespace URI. May be nil, in which +prefix+ is used as
343-
# the URI
344-
# Evaluates to: this Element
345-
# a = Element.new("a")
346-
# a.add_namespace("xmlns:foo", "bar" )
347-
# a.add_namespace("foo", "bar") # shorthand for previous line
348-
# a.add_namespace("twiddle")
349-
# puts a #-> <a xmlns:foo='bar' xmlns='twiddle'/>
378+
# :call-seq:
379+
# add_namespace(prefix, uri = nil) -> self
380+
#
381+
# Adds a namespace to the element; returns +self+.
382+
#
383+
# With the single argument +prefix+,
384+
# adds a namespace using the given +prefix+ and the namespace URI:
385+
#
386+
# e = REXML::Element.new('foo')
387+
# e.add_namespace('bar')
388+
# e.namespaces # => {"xmlns"=>"bar"}
389+
#
390+
# With both arguments +prefix+ and +uri+ given,
391+
# adds a namespace using both arguments:
392+
#
393+
# e.add_namespace('baz', 'bat')
394+
# e.namespaces # => {"xmlns"=>"bar", "baz"=>"bat"}
395+
#
350396
def add_namespace( prefix, uri=nil )
351397
unless uri
352398
@attributes["xmlns"] = prefix
@@ -357,16 +403,28 @@ def add_namespace( prefix, uri=nil )
357403
self
358404
end
359405

360-
# Removes a namespace from this node. This only works if the namespace is
361-
# actually declared in this node. If no argument is passed, deletes the
362-
# default namespace.
406+
# :call-seq:
407+
# delete_namespace(namespace = 'xmlns') -> self
408+
#
409+
# Removes a namespace from the element.
410+
#
411+
# With no argument, removes the default namespace:
412+
#
413+
# d = REXML::Document.new "<a xmlns:foo='bar' xmlns='twiddle'/>"
414+
# d.to_s # => "<a xmlns:foo='bar' xmlns='twiddle'/>"
415+
# d.root.delete_namespace # => <a xmlns:foo='bar'/>
416+
# d.to_s # => "<a xmlns:foo='bar'/>"
417+
#
418+
# With argument +namespace+, removes the specified namespace:
419+
#
420+
# d.root.delete_namespace('foo')
421+
# d.to_s # => "<a/>"
422+
#
423+
# Does nothing if no such namespace is found:
424+
#
425+
# d.root.delete_namespace('nosuch')
426+
# d.to_s # => "<a/>"
363427
#
364-
# Evaluates to: this element
365-
# doc = Document.new "<a xmlns:foo='bar' xmlns='twiddle'/>"
366-
# doc.root.delete_namespace
367-
# puts doc # -> <a xmlns:foo='bar'/>
368-
# doc.root.delete_namespace 'foo'
369-
# puts doc # -> <a/>
370428
def delete_namespace namespace="xmlns"
371429
namespace = "xmlns:#{namespace}" unless namespace == 'xmlns'
372430
attribute = attributes.get_attribute(namespace)

0 commit comments

Comments
 (0)