Class: LibXML::XML::Attr
- Inherits:
-
Object
- Object
- LibXML::XML::Attr
- Includes:
- Enumerable
- Defined in:
- ext/libxml/ruby_xml_attr.c,
lib/libxml/attr.rb,
ext/libxml/ruby_xml_attr.c
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!
Instance Method Summary collapse
-
#child ⇒ Object
Obtain this attribute’s child attribute(s).
-
#child? ⇒ Boolean
call-seq: attr.child? -> (true|false).
-
#doc ⇒ XML::Document
Returns this attribute’s document.
-
#doc? ⇒ Boolean
call-seq: attr.doc? -> (true|false).
- #each_sibling(&blk) ⇒ Object (also: #each_attr, #each)
-
#initialize(node, "name", "value") ⇒ Object
constructor
Creates a new attribute for the node.
-
#last ⇒ Object
Obtain the last attribute.
-
#last? ⇒ Boolean
call-seq: attr.last? -> (true|false).
-
#name ⇒ Object
Obtain this attribute’s name.
-
#namespaces ⇒ Object
call-seq: attr.namespacess -> XML::Namespaces.
-
#next ⇒ Object
Obtain the next attribute.
-
#next? ⇒ Boolean
call-seq: attr.next? -> (true|false).
-
#node_type ⇒ Numeric
Obtain this node’s type identifier.
-
#node_type_name ⇒ Object
Returns this node’s type name.
-
#ns ⇒ Object
Obtain this attribute’s associated XML::NS, if any.
-
#ns? ⇒ Boolean
call-seq: attr.ns? -> (true|false).
-
#parent ⇒ Object
Obtain this attribute node’s parent.
-
#parent? ⇒ Boolean
call-seq: attr.parent? -> (true|false).
-
#prev ⇒ Object
Obtain the previous attribute.
-
#prev? ⇒ Boolean
call-seq: attr.prev? -> (true|false).
-
#remove! ⇒ nil
Removes this attribute from it’s parent.
-
#siblings(node, &blk) ⇒ Object
Iterates nodes and attributes.
- #to_a ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
-
#value ⇒ Object
Obtain the value of this attribute.
-
#value=(val) ⇒ Object
Sets the value of this attribute.
Constructor Details
#initialize(node, "name", "value") ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'ext/libxml/ruby_xml_attr.c', line 72
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;
}
|
Instance Method Details
#child ⇒ Object
Obtain this attribute’s child attribute(s).
117 118 119 120 121 122 123 124 125 |
# File 'ext/libxml/ruby_xml_attr.c', line 117
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);
}
|
#child? ⇒ Boolean
call-seq:
attr.child? -> (true|false)
Returns whether this attribute has child attributes.
13 14 15 |
# File 'lib/libxml/attr.rb', line 13 def child? not self.children.nil? end |
#doc ⇒ XML::Document
Returns this attribute’s document.
doc.root.attributes.get_attribute('name').doc == doc
136 137 138 139 140 141 142 143 144 |
# File 'ext/libxml/ruby_xml_attr.c', line 136
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);
}
|
#doc? ⇒ Boolean
call-seq:
attr.doc? -> (true|false)
Determine whether this attribute is associated with an XML::Document.
22 23 24 |
# File 'lib/libxml/attr.rb', line 22 def doc? not self.doc.nil? end |
#each_sibling(&blk) ⇒ Object Also known as: each_attr, each
97 98 99 |
# File 'lib/libxml/attr.rb', line 97 def each_sibling(&blk) siblings(self,&blk) end |
#last ⇒ Object
Obtain the last attribute.
152 153 154 155 156 157 158 159 160 |
# File 'ext/libxml/ruby_xml_attr.c', line 152
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);
}
|
#last? ⇒ Boolean
call-seq:
attr.last? -> (true|false)
Determine whether this is the last attribute.
30 31 32 |
# File 'lib/libxml/attr.rb', line 30 def last? self.last.nil? end |
#name ⇒ Object
Obtain this attribute’s name.
168 169 170 171 172 173 174 175 176 177 |
# File 'ext/libxml/ruby_xml_attr.c', line 168
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);
}
|
#namespaces ⇒ Object
call-seq:
attr.namespacess -> XML::Namespaces
Returns this node’s XML::Namespaces object, which is used to access the namespaces associated with this node.
57 58 59 |
# File 'lib/libxml/attr.rb', line 57 def namespaces @namespaces ||= XML::Namespaces.new(self) end |
#next ⇒ Object
Obtain the next attribute.
185 186 187 188 189 190 191 192 193 |
# File 'ext/libxml/ruby_xml_attr.c', line 185
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);
}
|
#next? ⇒ Boolean
call-seq:
attr.next? -> (true|false)
Determine whether there is a next attribute.
38 39 40 |
# File 'lib/libxml/attr.rb', line 38 def next? not self.next.nil? end |
#node_type ⇒ Numeric
Obtain this node’s type identifier.
201 202 203 204 205 206 |
# File 'ext/libxml/ruby_xml_attr.c', line 201
static VALUE rxml_attr_node_type(VALUE self)
{
xmlAttrPtr xattr;
TypedData_Get_Struct(self, xmlAttr, &rxml_attr_type, xattr);
return INT2NUM(xattr->type);
}
|
#node_type_name ⇒ Object
Returns this node’s type name
79 80 81 82 83 84 85 |
# 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 |
#ns ⇒ Object
Obtain this attribute’s associated XML::NS, if any.
214 215 216 217 218 219 220 221 222 |
# File 'ext/libxml/ruby_xml_attr.c', line 214
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);
}
|
#ns? ⇒ Boolean
call-seq:
attr.ns? -> (true|false)
Determine whether this attribute has an associated namespace.
47 48 49 |
# File 'lib/libxml/attr.rb', line 47 def ns? not self.ns.nil? end |
#parent ⇒ Object
Obtain this attribute node’s parent.
230 231 232 233 234 235 236 237 238 |
# File 'ext/libxml/ruby_xml_attr.c', line 230
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);
}
|
#parent? ⇒ Boolean
call-seq:
attr.parent? -> (true|false)
Determine whether this attribute has a parent.
66 67 68 |
# File 'lib/libxml/attr.rb', line 66 def parent? not self.parent.nil? end |
#prev ⇒ Object
Obtain the previous attribute.
246 247 248 249 250 251 252 253 254 |
# File 'ext/libxml/ruby_xml_attr.c', line 246
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);
}
|
#prev? ⇒ Boolean
call-seq:
attr.prev? -> (true|false)
Determine whether there is a previous attribute.
74 75 76 |
# File 'lib/libxml/attr.rb', line 74 def prev? not self.prev.nil? 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.
265 266 267 268 269 270 271 272 273 274 |
# File 'ext/libxml/ruby_xml_attr.c', line 265
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;
}
|
#siblings(node, &blk) ⇒ Object
Iterates nodes and attributes
88 89 90 91 92 93 94 95 |
# 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 ⇒ Object
111 112 113 114 115 116 |
# File 'lib/libxml/attr.rb', line 111 def to_a inject([]) do |ary,a| ary << [a.name, a.value] ary end end |
#to_h ⇒ Object
104 105 106 107 108 109 |
# File 'lib/libxml/attr.rb', line 104 def to_h inject({}) do |h,a| h[a.name] = a.value h end end |
#to_s ⇒ Object
118 119 120 |
# File 'lib/libxml/attr.rb', line 118 def to_s "#{name} = #{value}" end |
#value ⇒ Object
Obtain the value of this attribute.
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'ext/libxml/ruby_xml_attr.c', line 282
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;
}
|
#value=(val) ⇒ Object
Sets the value of this attribute.
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'ext/libxml/ruby_xml_attr.c', line 305
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);
}
|