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
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);
TypedData_Get_Struct(node, xmlNode, &rxml_node_data_type, 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;
TypedData_Get_Struct(ns, xmlNs, &rxml_namespace_type, xns);
xattr = xmlNewNsProp(xnode, xns, (xmlChar*)StringValuePtr(name), (xmlChar*)StringValuePtr(value));
}
if (!xattr)
rb_raise(rb_eRuntimeError, "Could not create attribute.");
RTYPEDDATA_DATA( self) = xattr;
return self;
}
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')
Public Instance Methods
Source
static VALUE rxml_attr_child_get(VALUE self)
{
xmlAttrPtr xattr;
TypedData_Get_Struct(self, xmlAttr, &rxml_attr_type, xattr);
if (xattr->children == NULL)
return Qnil;
else
return rxml_node_wrap((xmlNodePtr) xattr->children);
}
Obtain this attribute’s child attribute(s).
Source
# File lib/libxml/attr.rb 13 def child? 14 not self.children.nil? 15 end
Returns whether this attribute has child attributes.
Source
static VALUE rxml_attr_doc_get(VALUE self)
{
xmlAttrPtr xattr;
TypedData_Get_Struct(self, xmlAttr, &rxml_attr_type, xattr);
if (xattr->doc == NULL)
return Qnil;
else
return rxml_registry_lookup(xattr->doc);
}
Returns this attribute’s document.
doc.root.attributes.get_attribute('name').doc == doc
Source
# File lib/libxml/attr.rb 22 def doc? 23 not self.doc.nil? 24 end
Determine whether this attribute is associated with an XML::Document.
Source
# File lib/libxml/attr.rb 97 def each_sibling(&blk) 98 siblings(self,&blk) 99 end
Source
static VALUE rxml_attr_last_get(VALUE self)
{
xmlAttrPtr xattr;
TypedData_Get_Struct(self, xmlAttr, &rxml_attr_type, xattr);
if (xattr->last == NULL)
return Qnil;
else
return rxml_node_wrap(xattr->last);
}
Obtain the last attribute.
Source
# File lib/libxml/attr.rb 30 def last? 31 self.last.nil? 32 end
Determine whether this is the last attribute.
Source
static VALUE rxml_attr_name_get(VALUE self)
{
xmlAttrPtr xattr;
TypedData_Get_Struct(self, xmlAttr, &rxml_attr_type, xattr);
if (xattr->name == NULL)
return Qnil;
else
return rxml_new_cstr( xattr->name, NULL);
}
Obtain this attribute’s name.
Source
# File lib/libxml/attr.rb 57 def namespaces 58 @namespaces ||= XML::Namespaces.new(self) 59 end
Returns this node’s XML::Namespaces object, which is used to access the namespaces associated with this node.
Source
static VALUE rxml_attr_next_get(VALUE self)
{
xmlAttrPtr xattr;
TypedData_Get_Struct(self, xmlAttr, &rxml_attr_type, xattr);
if (xattr->next == NULL)
return Qnil;
else
return rxml_attr_wrap(xattr->next);
}
Obtain the next attribute.
Source
# File lib/libxml/attr.rb 38 def next? 39 not self.next.nil? 40 end
Determine whether there is a next attribute.
Source
static VALUE rxml_attr_node_type(VALUE self)
{
xmlAttrPtr xattr;
TypedData_Get_Struct(self, xmlAttr, &rxml_attr_type, xattr);
return INT2NUM(xattr->type);
}
Obtain this node’s type identifier.
Source
# File lib/libxml/attr.rb 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
Returns this node’s type name
Source
static VALUE rxml_attr_ns_get(VALUE self)
{
xmlAttrPtr xattr;
TypedData_Get_Struct(self, xmlAttr, &rxml_attr_type, xattr);
if (xattr->ns == NULL)
return Qnil;
else
return rxml_namespace_wrap(xattr->ns);
}
Obtain this attribute’s associated XML::NS, if any.
Source
# File lib/libxml/attr.rb 47 def ns? 48 not self.ns.nil? 49 end
Determine whether this attribute has an associated namespace.
Source
static VALUE rxml_attr_parent_get(VALUE self)
{
xmlAttrPtr xattr;
TypedData_Get_Struct(self, xmlAttr, &rxml_attr_type, xattr);
if (xattr->parent == NULL)
return Qnil;
else
return rxml_node_wrap(xattr->parent);
}
Obtain this attribute node’s parent.
Source
# File lib/libxml/attr.rb 66 def parent? 67 not self.parent.nil? 68 end
Determine whether this attribute has a parent.
Source
static VALUE rxml_attr_prev_get(VALUE self)
{
xmlAttrPtr xattr;
TypedData_Get_Struct(self, xmlAttr, &rxml_attr_type, xattr);
if (xattr->prev == NULL)
return Qnil;
else
return rxml_attr_wrap(xattr->prev);
}
Obtain the previous attribute.
Source
# File lib/libxml/attr.rb 74 def prev? 75 not self.prev.nil? 76 end
Determine whether there is a previous attribute.
Source
static VALUE rxml_attr_remove_ex(VALUE self)
{
xmlAttrPtr xattr;
TypedData_Get_Struct(self, xmlAttr, &rxml_attr_type, xattr);
xmlRemoveProp(xattr);
RTYPEDDATA_DATA(self) = NULL;
return Qnil;
}
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.
Source
# File lib/libxml/attr.rb 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
Iterates nodes and attributes
Source
# File lib/libxml/attr.rb 111 def to_a 112 inject([]) do |ary,a| 113 ary << [a.name, a.value] 114 ary 115 end 116 end
Source
# File lib/libxml/attr.rb 104 def to_h 105 inject({}) do |h,a| 106 h[a.name] = a.value 107 h 108 end 109 end
Source
VALUE rxml_attr_value_get(VALUE self)
{
xmlAttrPtr xattr;
xmlChar *value;
VALUE result = Qnil;
TypedData_Get_Struct(self, xmlAttr, &rxml_attr_type, xattr);
value = xmlNodeGetContent((xmlNodePtr)xattr);
if (value != NULL)
{
result = rxml_new_cstr( value, NULL);
xmlFree(value);
}
return result;
}
Obtain the value of this attribute.
Source
VALUE rxml_attr_value_set(VALUE self, VALUE val)
{
xmlAttrPtr xattr;
Check_Type(val, T_STRING);
TypedData_Get_Struct(self, xmlAttr, &rxml_attr_type, 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);
}
Sets the value of this attribute.