123456789_123456789_123456789_123456789_123456789_

Class: LibXML::XML::Attr

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
self, Enumerable
Inherits: Object
Defined in: ext/libxml/ruby_xml_attr.c,
ext/libxml/ruby_xml_attr.c,
lib/libxml/attr.rb

Overview

Provides access to an attribute defined on an element.

Basic Usage:

require 'test_helper'

doc = XML::Document.new(<some_file>)
attribute = doc.root.attributes.get_attribute_ns('http://www.w3.org/1999/xlink', 'href')
attribute.name == 'href'
attribute.value == 'http://www.mydocument.com'
attribute.remove!

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(node, "name", "value")

Creates a new attribute for the node.

node: The Node that will contain the attribute name: The name of the attribute value: The value of the attribute

attr = XML::Attr.new(doc.root, 'name', 'libxml')
[ GitHub ]

  
# File 'ext/libxml/ruby_xml_attr.c', line 65

static VALUE rxml_attr_initialize(int argc, VALUE *argv, VALUE self)
{
  VALUE node = argv[0];
  VALUE name = argv[1];
  VALUE value = argv[2];
  VALUE ns = (argc == 4 ? argv[3] : Qnil);

  xmlNodePtr xnode;
  xmlAttrPtr xattr;

  if (argc < 3 || argc > 4)
    rb_raise(rb_eArgError, "Wrong number of arguments (3 or 4)");

  Check_Type(name, T_STRING);
  Check_Type(value, T_STRING);

  Data_Get_Struct(node, xmlNode, xnode);

  if (xnode->type != XML_ELEMENT_NODE)
    rb_raise(rb_eArgError, "Attributes can only be created on element nodes.");

  if (NIL_P(ns))
  {
    xattr = xmlNewProp(xnode, (xmlChar*)StringValuePtr(name), (xmlChar*)StringValuePtr(value));
  }
  else
  {
    xmlNsPtr xns;
    Data_Get_Struct(ns, xmlNs, xns);
    xattr = xmlNewNsProp(xnode, xns, (xmlChar*)StringValuePtr(name), (xmlChar*)StringValuePtr(value));
  }

  if (!xattr)
    rb_raise(rb_eRuntimeError, "Could not create attribute.");

  DATA_PTR( self) = xattr;
  return self;
}

Instance Attribute Details

#childnode (readonly)

Obtain this attribute’s child attribute(s).

[ GitHub ]

  
# File 'ext/libxml/ruby_xml_attr.c', line 110

static VALUE rxml_attr_child_get(VALUE self)
{
  xmlAttrPtr xattr;
  Data_Get_Struct(self, xmlAttr, xattr);
  if (xattr->children == NULL)
    return Qnil;
  else
    return rxml_node_wrap((xmlNodePtr) xattr->children);
}

#child?Boolean (readonly)

Returns whether this attribute has child attributes.

[ GitHub ]

  
# File 'lib/libxml/attr.rb', line 13

def child?
  not self.children.nil?
end

#docXML::Document (readonly)

Returns this attribute’s document.

doc.root.attributes.get_attribute('name').doc == doc
[ GitHub ]

  
# File 'ext/libxml/ruby_xml_attr.c', line 129

static VALUE rxml_attr_doc_get(VALUE self)
{
  xmlAttrPtr xattr;
  Data_Get_Struct(self, xmlAttr, xattr);
  if (xattr->doc == NULL)
    return Qnil;
  else
    return rxml_document_wrap(xattr->doc);
}

#doc?Boolean (readonly)

Determine whether this attribute is associated with an Document.

[ GitHub ]

  
# File 'lib/libxml/attr.rb', line 22

def doc?
  not self.doc.nil?
end

#lastnode (readonly)

Obtain the last attribute.

[ GitHub ]

  
# File 'ext/libxml/ruby_xml_attr.c', line 145

static VALUE rxml_attr_last_get(VALUE self)
{
  xmlAttrPtr xattr;
  Data_Get_Struct(self, xmlAttr, xattr);
  if (xattr->last == NULL)
    return Qnil;
  else
    return rxml_node_wrap(xattr->last);
}

#last?Boolean (readonly)

Determine whether this is the last attribute.

[ GitHub ]

  
# File 'lib/libxml/attr.rb', line 30

def last?
  self.last.nil?
end

#nextnode (readonly)

Obtain the next attribute.

[ GitHub ]

  
# File 'ext/libxml/ruby_xml_attr.c', line 178

static VALUE rxml_attr_next_get(VALUE self)
{
  xmlAttrPtr xattr;
  Data_Get_Struct(self, xmlAttr, xattr);
  if (xattr->next == NULL)
    return Qnil;
  else
    return rxml_attr_wrap(xattr->next);
}

#next?Boolean (readonly)

Determine whether there is a next attribute.

[ GitHub ]

  
# File 'lib/libxml/attr.rb', line 38

def next?
  not self.next.nil?
end

#nsnamespace (readonly)

Obtain this attribute’s associated XML::NS, if any.

[ GitHub ]

  
# File 'ext/libxml/ruby_xml_attr.c', line 207

static VALUE rxml_attr_ns_get(VALUE self)
{
  xmlAttrPtr xattr;
  Data_Get_Struct(self, xmlAttr, xattr);
  if (xattr->ns == NULL)
    return Qnil;
  else
    return rxml_namespace_wrap(xattr->ns);
}

#ns?Boolean (readonly)

Determine whether this attribute has an associated namespace.

[ GitHub ]

  
# File 'lib/libxml/attr.rb', line 47

def ns?
  not self.ns.nil?
end

#parentnode (readonly)

Obtain this attribute node’s parent.

[ GitHub ]

  
# File 'ext/libxml/ruby_xml_attr.c', line 223

static VALUE rxml_attr_parent_get(VALUE self)
{
  xmlAttrPtr xattr;
  Data_Get_Struct(self, xmlAttr, xattr);
  if (xattr->parent == NULL)
    return Qnil;
  else
    return rxml_node_wrap(xattr->parent);
}

#parent?Boolean (readonly)

Determine whether this attribute has a parent.

[ GitHub ]

  
# File 'lib/libxml/attr.rb', line 66

def parent?
  not self.parent.nil?
end

#prevnode (readonly)

Obtain the previous attribute.

[ GitHub ]

  
# File 'ext/libxml/ruby_xml_attr.c', line 239

static VALUE rxml_attr_prev_get(VALUE self)
{
  xmlAttrPtr xattr;
  Data_Get_Struct(self, xmlAttr, xattr);
  if (xattr->prev == NULL)
    return Qnil;
  else
    return rxml_attr_wrap(xattr->prev);
}

#prev?Boolean (readonly)

Determine whether there is a previous attribute.

[ GitHub ]

  
# File 'lib/libxml/attr.rb', line 74

def prev?
  not self.prev.nil?
end

#value ⇒ "value" (rw)

Obtain the value of this attribute.

[ GitHub ]

  
# File 'ext/libxml/ruby_xml_attr.c', line 277

VALUE rxml_attr_value_get(VALUE self)
{
  xmlAttrPtr xattr;
  xmlChar *value;
  VALUE result = Qnil;

  Data_Get_Struct(self, xmlAttr, xattr);
  value = xmlNodeGetContent((xmlNodePtr)xattr);

  if (value != NULL)
  {
    result = rxml_new_cstr( value, NULL);
    xmlFree(value);
  }
  return result;
}

#value=("value") (rw)

Sets the value of this attribute.

[ GitHub ]

  
# File 'ext/libxml/ruby_xml_attr.c', line 300

VALUE rxml_attr_value_set(VALUE self, VALUE val)
{
  xmlAttrPtr xattr;

  Check_Type(val, T_STRING);
  Data_Get_Struct(self, xmlAttr, xattr);

  if (xattr->ns)
    xmlSetNsProp(xattr->parent, xattr->ns, xattr->name,
        (xmlChar*) StringValuePtr(val));
  else
    xmlSetProp(xattr->parent, xattr->name, (xmlChar*) StringValuePtr(val));

  return (self);
}

Instance Method Details

#each(&blk)

Alias for #each_sibling.

[ GitHub ]

  
# File 'lib/libxml/attr.rb', line 102

alias :each :each_sibling

#each_attr(&blk)

Alias for #each_sibling.

[ GitHub ]

  
# File 'lib/libxml/attr.rb', line 101

alias :each_attr :each_sibling

#each_sibling(&blk) Also known as: #each_attr, #each

[ GitHub ]

  
# File 'lib/libxml/attr.rb', line 97

def each_sibling(&blk)
  siblings(self,&blk)
end

#name ⇒ "name"

Obtain this attribute’s name.

[ GitHub ]

  
# File 'ext/libxml/ruby_xml_attr.c', line 161

static VALUE rxml_attr_name_get(VALUE self)
{
  xmlAttrPtr xattr;
  Data_Get_Struct(self, xmlAttr, xattr);

  if (xattr->name == NULL)
    return Qnil;
  else
    return rxml_new_cstr( xattr->name, NULL);
}

#namespacessXML::Namespaces

Returns this node’s Namespaces object, which is used to access the namespaces associated with this node.

[ GitHub ]

  
# File 'lib/libxml/attr.rb', line 57

def namespaces
  @namespaces ||= XML::Namespaces.new(self)
end

#node_typeNumeric

Obtain this node’s type identifier.

[ GitHub ]

  
# File 'ext/libxml/ruby_xml_attr.c', line 194

static VALUE rxml_attr_node_type(VALUE self)
{
  xmlAttrPtr xattr;
  Data_Get_Struct(self, xmlAttr, xattr);
  return INT2NUM(xattr->type);
}

#node_type_name

Returns this node’s type name

[ GitHub ]

  
# File 'lib/libxml/attr.rb', line 79

def node_type_name
  if node_type == Node::ATTRIBUTE_NODE
    'attribute'
  else
    raise(UnknownType, "Unknown node type: %n", node.node_type);
  end
end

#remove!nil

Removes this attribute from it’s parent. Note the attribute and its content is freed and can no longer be used. If you try to use it you will get a segmentation fault.

[ GitHub ]

  
# File 'ext/libxml/ruby_xml_attr.c', line 258

static VALUE rxml_attr_remove_ex(VALUE self)
{
  xmlAttrPtr xattr;
  Data_Get_Struct(self, xmlAttr, xattr);
  xmlRemoveProp(xattr);

  RDATA(self)->data = NULL;
  RDATA(self)->dfree = NULL;
  RDATA(self)->dmark = NULL;

  return Qnil;
}

#siblings(node, &blk)

Iterates nodes and attributes

[ GitHub ]

  
# File 'lib/libxml/attr.rb', line 88

def siblings(node, &blk)
  if n = node
    loop do
      blk.call(n)
      break unless n = n.next
    end
  end
end

#to_a

[ GitHub ]

  
# File 'lib/libxml/attr.rb', line 111

def to_a
  inject([]) do |ary,a| 
    ary << [a.name, a.value]
    ary
  end
end

#to_h

[ GitHub ]

  
# File 'lib/libxml/attr.rb', line 104

def to_h
  inject({}) do |h,a|
    h[a.name] = a.value
    h
  end
end

#to_s

[ GitHub ]

  
# File 'lib/libxml/attr.rb', line 118

def to_s
  "#{name} = #{value}"
end