module HTree::Elem::Trav

Public Instance Methods

attributes() click to toggle source

attributes returns attributes as a hash. The hash keys are HTree::Name objects. The hash values are HTree::Text or HTree::Location objects.

p HTree('<a name="xx" href="uu">').root.attributes
# =>
{href=>{text "uu"}, name=>{text "xx"}}

p HTree('<a name="xx" href="uu">').make_loc.root.attributes
# =>
{href=>#<HTree::Location: doc()/a/@href>, name=>#<HTree::Location: doc()/a/@name>}
# File htree/traverse.rb, line 411
def attributes
  result = {}
  each_attribute {|name, text|
    result[name] = text
  }
  result
end
each_attr() { |uname, str| ... } click to toggle source
# File htree/traverse.rb, line 419
def each_attr
  each_attribute {|name, text|
    uname = name.universal_name
    str = text.to_s
    yield uname, str
  }
end
fetch_attr(name) → string or raise IndexError click to toggle source
fetch_attr(name, default) → string or default
fetch_attr(name) {|uname| default } → string or default

fetch_attr returns an attribute value as a string.

elem may be an instance of HTree::Elem or a location points to it.

# File htree/traverse.rb, line 462
def fetch_attr(uname, *rest, &block)
  if 1 < rest.length
    raise ArgumentError, "wrong number of arguments (#{1+rest.length} for 2)"
  end
  if !rest.empty? && block_given?
    raise ArgumentError, "block supersedes default value argument"
  end
  uname = uname.universal_name if uname.respond_to? :universal_name
  return update_attribute_hash.fetch(uname) {
    if block_given?
      return yield(uname)
    elsif !rest.empty?
      return rest[0]
    else
      raise IndexError, "attribute not found: #{uname.inspect}"
    end
  }.to_s
end
fetch_attribute(name) → text or raise IndexError click to toggle source
fetch_attribute(name, default) → text or default
fetch_attribute(name) {|uname| default } → text or default

fetch_attribute returns an attribute value as a text.

elem may be an instance of HTree::Elem or a location points to it.

# File htree/traverse.rb, line 435
def fetch_attribute(uname, *rest, &block)
  if 1 < rest.length
    raise ArgumentError, "wrong number of arguments (#{1+rest.length} for 2)"
  end
  if !rest.empty? && block_given?
    raise ArgumentError, "block supersedes default value argument"
  end
  uname = uname.universal_name if uname.respond_to? :universal_name
  return update_attribute_hash.fetch(uname) {
    if block_given?
      return yield(uname)
    elsif !rest.empty?
      return rest[0]
    else
      raise IndexError, "attribute not found: #{uname.inspect}"
    end
  }
end
get_attr(uname) click to toggle source
# File htree/traverse.rb, line 486
def get_attr(uname)
  if text = update_attribute_hash[uname]
    text.to_s
  else
    nil 
  end
end
get_attribute(uname) click to toggle source
# File htree/traverse.rb, line 481
def get_attribute(uname)
  uname = uname.universal_name if uname.respond_to? :universal_name
  update_attribute_hash[uname]
end
name() click to toggle source

name returns the universal name of the element as a string.

p HTree('<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"/>').root.name
# =>
"{http://www.w3.org/1999/02/22-rdf-syntax-ns#}RDF"
# File htree/traverse.rb, line 390
def name() element_name.universal_name end
qualified_name() click to toggle source

qualified_name returns the qualified name of the element as a string.

p HTree('<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"/>').root.qualified_name
# =>
"rdf:RDF"
# File htree/traverse.rb, line 397
def qualified_name() element_name.qualified_name end
traverse_all_element() { |self| ... } click to toggle source
# File htree/traverse.rb, line 187
def traverse_all_element(&block)
  yield self
  children.each {|c| c.traverse_all_element(&block) }
end
traverse_some_element(name_set) { |self| ... } click to toggle source
# File htree/traverse.rb, line 205
def traverse_some_element(name_set, &block)
  yield self if name_set.include? self.name
  children.each {|c| c.traverse_some_element(name_set, &block) }
end