class LibXML::XML::Attr
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!
Public Class Methods
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')
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; }
Public Instance Methods
Obtain this attribute's child attribute(s).
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); }
Returns whether this attribute has child attributes.
# File lib/libxml/attr.rb, line 12 def child? not self.children.nil? end
Returns this attribute's document.
doc.root.attributes.get_attribute('name').doc == doc
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); }
Determine whether this attribute is associated with an XML::Document.
# File lib/libxml/attr.rb, line 21 def doc? not self.doc.nil? end
# File lib/libxml/attr.rb, line 96 def each_sibling(&blk) siblings(self,&blk) end
Obtain the last attribute.
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); }
Determine whether this is the last attribute.
# File lib/libxml/attr.rb, line 29 def last? self.last.nil? end
Obtain this attribute's name.
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((const char*) xattr->name, NULL); }
Returns this node's XML::Namespaces object, which is used to access the namespaces associated with this node.
# File lib/libxml/attr.rb, line 56 def namespaces @namespaces ||= XML::Namespaces.new(self) end
Obtain the next attribute.
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); }
Determine whether there is a next attribute.
# File lib/libxml/attr.rb, line 37 def next? not self.next.nil? end
Obtain this node's type identifier.
static VALUE rxml_attr_node_type(VALUE self) { xmlAttrPtr xattr; Data_Get_Struct(self, xmlAttr, xattr); return INT2NUM(xattr->type); }
Returns this node's type name
# File lib/libxml/attr.rb, line 78 def node_type_name if node_type == Node::ATTRIBUTE_NODE 'attribute' else raise(UnknownType, "Unknown node type: %n", node.node_type); end end
Obtain this attribute's associated XML::NS, if any.
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); }
Determine whether this attribute has an associated namespace.
# File lib/libxml/attr.rb, line 46 def ns? not self.ns.nil? end
Obtain this attribute node's parent.
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); }
Determine whether this attribute has a parent.
# File lib/libxml/attr.rb, line 65 def parent? not self.parent.nil? end
Obtain the previous attribute.
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); }
Determine whether there is a previous attribute.
# File lib/libxml/attr.rb, line 73 def prev? not self.prev.nil? end
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.
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; }
Iterates nodes and attributes
# File lib/libxml/attr.rb, line 87 def siblings(node, &blk) if n = node loop do blk.call(n) break unless n = n.next end end end
# File lib/libxml/attr.rb, line 110 def to_a inject([]) do |ary,a| ary << [a.name, a.value] ary end end
# File lib/libxml/attr.rb, line 103 def to_h inject({}) do |h,a| h[a.name] = a.value h end end
# File lib/libxml/attr.rb, line 117 def to_s "#{name} = #{value}" end
Obtain the value of this attribute.
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((const char*) value, NULL); xmlFree(value); } return result; }
Sets the value of this attribute.
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); }