Class LibXML::XML::Attr

  1. ext/libxml/libxml.c
  2. lib/libxml/attr.rb
  3. show all
Parent: Object

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!

Included modules

  1. Enumerable

Public class methods

attr.initialize(node, "name", "value")

Creates a new attribute for the node.

node: The XML::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')
[show source]
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.");

  xattr->_private = (void *) self;
  DATA_PTR( self) = xattr;
  return self;
}

Public instance methods

attr.child → node

Obtain this attribute’s child attribute(s).

[show source]
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);
}
attr.child? → (true|false)

Returns whether this attribute has child attributes.

[show source]
    # File lib/libxml/attr.rb, line 13
13:       def child?
14:         not self.children.nil?
15:       end
attr.doc → XML::Document

Returns this attribute’s document.

doc.root.attributes.get_attribute('name').doc == doc
[show source]
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);
}
attr.doc? → (true|false)

Determine whether this attribute is associated with an XML::Document.

[show source]
    # File lib/libxml/attr.rb, line 22
22:       def doc?
23:         not self.doc.nil?
24:       end
each (&blk)

Alias for each_sibling

each_attr (&blk)

Alias for each_sibling

each_sibling (&blk)
[show source]
    # File lib/libxml/attr.rb, line 97
97:       def each_sibling(&blk)
98:         siblings(self,&blk)
99:       end
attr.last → node

Obtain the last attribute.

[show source]
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);
}
attr.last? → (true|false)

Determine whether this is the last attribute.

[show source]
    # File lib/libxml/attr.rb, line 30
30:       def last?
31:         self.last.nil?
32:       end
attr.name → "name"

Obtain this attribute’s name.

[show source]
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_str_new2((const char*) xattr->name, (xattr->doc ? xattr->doc->encoding : NULL));
}
attr.namespacess → XML::Namespaces

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

[show source]
    # File lib/libxml/attr.rb, line 57
57:       def namespaces
58:         @namespaces ||= XML::Namespaces.new(self)
59:       end
attr.next → node

Obtain the next attribute.

[show source]
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);
}
attr.next? → (true|false)

Determine whether there is a next attribute.

[show source]
    # File lib/libxml/attr.rb, line 38
38:       def next?
39:         not self.next.nil?
40:       end
attr.node_type → num

Obtain this node’s type identifier.

[show source]
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

[show source]
    # File lib/libxml/attr.rb, line 79
79:       def node_type_name
80:         if node_type == Node::ATTRIBUTE_NODE
81:           'attribute'
82:         else
83:           raise(UnknownType, "Unknown node type: %n", node.node_type);
84:         end
85:       end
attr.ns → namespace

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

[show source]
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, NULL);
}
attr.ns? → (true|false)

Determine whether this attribute has an associated namespace.

[show source]
    # File lib/libxml/attr.rb, line 47
47:       def ns?
48:         not self.ns.nil?
49:       end
attr.parent → node

Obtain this attribute node’s parent.

[show source]
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);
}
attr.parent? → (true|false)

Determine whether this attribute has a parent.

[show source]
    # File lib/libxml/attr.rb, line 66
66:       def parent?
67:         not self.parent.nil?
68:       end
attr.prev → node

Obtain the previous attribute.

[show source]
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);
}
attr.prev? → (true|false)

Determine whether there is a previous attribute.

[show source]
    # File lib/libxml/attr.rb, line 74
74:       def prev?
75:         not self.prev.nil?
76:       end
node.remove! → nil

Removes this attribute from it’s parent.

[show source]
static VALUE rxml_attr_remove_ex(VALUE self)
{
  xmlAttrPtr xattr;
  Data_Get_Struct(self, xmlAttr, xattr);

  if (xattr->_private == NULL)
    xmlRemoveProp(xattr);
  else
    xmlUnlinkNode((xmlNodePtr) xattr);

  return Qnil;;
}
siblings (node, &blk)

Iterates nodes and attributes

[show source]
    # File lib/libxml/attr.rb, line 88
88:       def siblings(node, &blk)
89:         if n = node
90:           loop do
91:             blk.call(n)
92:             break unless n = n.next
93:           end
94:         end
95:       end
to_a ()
[show source]
     # File lib/libxml/attr.rb, line 111
111:       def to_a
112:         inject([]) do |ary,a| 
113:           ary << [a.name, a.value]
114:           ary
115:         end
116:       end
to_h ()
[show source]
     # File lib/libxml/attr.rb, line 104
104:       def to_h
105:         inject({}) do |h,a|
106:           h[a.name] = a.value
107:           h
108:         end
109:       end
to_s ()
[show source]
     # File lib/libxml/attr.rb, line 118
118:       def to_s
119:         "#{name} = #{value}"
120:       end
attr.value → "value"

Obtain the value of this attribute.

[show source]
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_str_new2((const char*) value, (xattr->doc ? xattr->doc->encoding : NULL));
    xmlFree(value);
  }
  return result;
}
attr.value = "value"

Sets the value of this attribute.

[show source]
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);
}