Class: LibXML::XML::Attr

Inherits:
Object
  • Object
show all
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 'xml'

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

Constructor Details

#initialize(node, "name", "value") ⇒ Object

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')


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'ext/libxml/ruby_xml_attr.c', line 86

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;
}

Instance Method Details

#childObject

Obtain this attribute’s child attribute(s).



132
133
134
135
136
137
138
139
140
# File 'ext/libxml/ruby_xml_attr.c', line 132

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

call-seq:

attr.child? -> (true|false)

Returns whether this attribute has child attributes.

Returns:

  • (Boolean)


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

def child?
  not self.children.nil?
end

#docXML::Document

Returns this attribute’s document.

doc.root.attributes.get_attribute('name').doc == doc

Returns:



151
152
153
154
155
156
157
158
159
# File 'ext/libxml/ruby_xml_attr.c', line 151

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

call-seq:

attr.doc? -> (true|false)

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

Returns:

  • (Boolean)


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

def doc?
  not self.doc.nil?
end

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



85
86
87
# File 'lib/libxml/attr.rb', line 85

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

#lastObject

Obtain the last attribute.



167
168
169
170
171
172
173
174
175
# File 'ext/libxml/ruby_xml_attr.c', line 167

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

call-seq:

attr.last? -> (true|false)

Determine whether this is the last attribute.

Returns:

  • (Boolean)


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

def last?
  self.last.nil?
end

#nameObject

Obtain this attribute’s name.



183
184
185
186
187
188
189
190
191
192
# File 'ext/libxml/ruby_xml_attr.c', line 183

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

  if (xattr->name == NULL)
    return Qnil;
  else
    return rb_str_new2((const char*) xattr->name);
}

#nextObject

Obtain the next attribute.



200
201
202
203
204
205
206
207
208
# File 'ext/libxml/ruby_xml_attr.c', line 200

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

call-seq:

attr.next? -> (true|false)

Determine whether there is a next attribute.

Returns:

  • (Boolean)


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

def next?
  not self.next.nil?
end

#node_typeNumeric

Obtain this node’s type identifier.

Returns:

  • (Numeric)


216
217
218
219
220
221
# File 'ext/libxml/ruby_xml_attr.c', line 216

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

#node_type_nameObject

Returns this node’s type name



67
68
69
70
71
72
73
# File 'lib/libxml/attr.rb', line 67

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

#nsObject

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



229
230
231
232
233
234
235
236
237
# File 'ext/libxml/ruby_xml_attr.c', line 229

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);
}

#ns?Boolean

call-seq:

attr.ns? -> (true|false)

Determine whether this attribute has an associated namespace.

Returns:

  • (Boolean)


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

def ns?
  not self.ns.nil?
end

#parentObject

Obtain this attribute node’s parent.



245
246
247
248
249
250
251
252
253
# File 'ext/libxml/ruby_xml_attr.c', line 245

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

call-seq:

attr.parent? -> (true|false)

Determine whether this attribute has a parent.

Returns:

  • (Boolean)


54
55
56
# File 'lib/libxml/attr.rb', line 54

def parent?
  not self.parent.nil?
end

#prevObject

Obtain the previous attribute.



261
262
263
264
265
266
267
268
269
# File 'ext/libxml/ruby_xml_attr.c', line 261

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

call-seq:

attr.prev? -> (true|false)

Determine whether there is a previous attribute.

Returns:

  • (Boolean)


62
63
64
# File 'lib/libxml/attr.rb', line 62

def prev?
  not self.prev.nil?
end

#remove!nil

Removes this attribute from it’s parent.

Returns:

  • (nil)


277
278
279
280
281
282
283
284
285
286
287
288
# File 'ext/libxml/ruby_xml_attr.c', line 277

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) ⇒ Object

Iterates nodes and attributes



76
77
78
79
80
81
82
83
# File 'lib/libxml/attr.rb', line 76

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

#to_aObject



99
100
101
102
103
104
# File 'lib/libxml/attr.rb', line 99

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

#to_hObject



92
93
94
95
96
97
# File 'lib/libxml/attr.rb', line 92

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

#to_sObject



106
107
108
# File 'lib/libxml/attr.rb', line 106

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

#valueObject

Obtain the value of this attribute.



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'ext/libxml/ruby_xml_attr.c', line 296

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 = rb_str_new2((const char*) value);
    xmlFree(value);
  }
  return result;
}

#value=(val) ⇒ Object

Sets the value of this attribute.



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'ext/libxml/ruby_xml_attr.c', line 319

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);
}