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

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


65
66
67
68
69
70
71
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
# File 'ext/libxml/ruby_xml_attr.c', line 65

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

Instance Method Details

#childObject

Obtain this attribute’s child attribute(s).



110
111
112
113
114
115
116
117
118
# File 'ext/libxml/ruby_xml_attr.c', line 110

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)


13
14
15
# File 'lib/libxml/attr.rb', line 13

def child?
  not self.children.nil?
end

#docXML::Document

Returns this attribute’s document.

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

Returns:



129
130
131
132
133
134
135
136
137
# File 'ext/libxml/ruby_xml_attr.c', line 129

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)


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

#lastObject

Obtain the last attribute.



145
146
147
148
149
150
151
152
153
# File 'ext/libxml/ruby_xml_attr.c', line 145

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)


30
31
32
# File 'lib/libxml/attr.rb', line 30

def last?
  self.last.nil?
end

#nameObject

Obtain this attribute’s name.



161
162
163
164
165
166
167
168
169
170
# File 'ext/libxml/ruby_xml_attr.c', line 161

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( xattr->name, NULL);
}

#namespacesObject

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

#nextObject

Obtain the next attribute.



178
179
180
181
182
183
184
185
186
# File 'ext/libxml/ruby_xml_attr.c', line 178

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)


38
39
40
# File 'lib/libxml/attr.rb', line 38

def next?
  not self.next.nil?
end

#node_typeNumeric

Obtain this node’s type identifier.

Returns:

  • (Numeric)


194
195
196
197
198
199
# File 'ext/libxml/ruby_xml_attr.c', line 194

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



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

#nsObject

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



207
208
209
210
211
212
213
214
215
# File 'ext/libxml/ruby_xml_attr.c', line 207

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

#ns?Boolean

call-seq:

attr.ns? -> (true|false)

Determine whether this attribute has an associated namespace.

Returns:

  • (Boolean)


47
48
49
# File 'lib/libxml/attr.rb', line 47

def ns?
  not self.ns.nil?
end

#parentObject

Obtain this attribute node’s parent.



223
224
225
226
227
228
229
230
231
# File 'ext/libxml/ruby_xml_attr.c', line 223

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)


66
67
68
# File 'lib/libxml/attr.rb', line 66

def parent?
  not self.parent.nil?
end

#prevObject

Obtain the previous attribute.



239
240
241
242
243
244
245
246
247
# File 'ext/libxml/ruby_xml_attr.c', line 239

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)


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.

Returns:

  • (nil)


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

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

#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_aObject



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_hObject



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_sObject



118
119
120
# File 'lib/libxml/attr.rb', line 118

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

#valueObject

Obtain the value of this attribute.



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

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( value, NULL);
    xmlFree(value);
  }
  return result;
}

#value=(val) ⇒ Object

Sets the value of this attribute.



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'ext/libxml/ruby_xml_attr.c', line 300

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