Class: LibXML::XML::Namespace

Inherits:
Object
  • Object
show all
Includes:
Comparable, Enumerable
Defined in:
ext/libxml/ruby_xml_namespace.c,
lib/libxml/namespace.rb,
ext/libxml/ruby_xml_namespace.c

Overview

The Namespace class represents an XML namespace. To add a namespace to a node, create a new instance of this class. Note that this does not assign the node to the namespace. To do that see the XML::Namespaces#namespace method.

Usage:

node = XML::Node.new('<Envelope>')
XML::Namespace.new(node, 'soap', 'http://schemas.xmlsoap.org/soap/envelope/')
assert_equal("<Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"/>", node.to_s)
assert_nil(node.namespaces.namespace)

Direct Known Subclasses

NS

Instance Method Summary collapse

Constructor Details

#initialize(node, "prefix", "href") ⇒ XML::Namespace

Create a new namespace and adds it to the specified node. Note this does not assign the node to the namespace. To do that see the XML::Namespaces#namespace method.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'ext/libxml/ruby_xml_namespace.c', line 62

static VALUE rxml_namespace_initialize(VALUE self, VALUE node, VALUE prefix,
    VALUE href)
{
  xmlNodePtr xnode;
  xmlChar *xmlPrefix;
  xmlNsPtr xns;

  Check_Type(node, T_DATA);
  Data_Get_Struct(node, xmlNode, xnode);

  /* Prefix can be null - that means its the default namespace */
  xmlPrefix = NIL_P(prefix) ? NULL : (xmlChar *)StringValuePtr(prefix);
  xns = xmlNewNs(xnode, (xmlChar*) StringValuePtr(href), xmlPrefix);

  if (!xns)
    rxml_raise(&xmlLastError);

  xns->_private = (void*)self;
  DATA_PTR(self) = xns;
  return self;
}

Instance Method Details

#<=>(other) ⇒ Object

call-seq:

namespace1 <=> namespace2

Compares two namespace objects. Namespace objects are considered equal if their prefixes and hrefs are the same.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/libxml/namespace.rb', line 12

def <=>(other)
  if self.prefix.nil? and other.prefix.nil?
    self.href <=> other.href
  elsif self.prefix.nil?
    -1
  elsif other.prefix.nil?
    1
  else
    self.prefix <=> other.prefix
  end
end

#eachObject

call-seq:

namespace.each {|ns| .. }

libxml stores namespaces in memory as a linked list. Use the each method to iterate over the list. Note the first namespace in the loop is the current namespace.

Usage:

namespace.each do |ns|
  ..
end


35
36
37
38
39
40
41
42
# File 'lib/libxml/namespace.rb', line 35

def each
  ns = self

  while ns
    yield ns
    ns = ns.next
  end
end

#hrefObject

Usage:

doc = XML::Document.string('<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"/>')
ns = doc.root.namespaces.find_by_href('http://schemas.xmlsoap.org/soap/envelope/')
assert_equal('http://schemas.xmlsoap.org/soap/envelope/', ns.href)


94
95
96
97
98
99
100
101
102
# File 'ext/libxml/ruby_xml_namespace.c', line 94

static VALUE rxml_namespace_href_get(VALUE self)
{
  xmlNsPtr xns;
  Data_Get_Struct(self, xmlNs, xns);
  if (xns->href == NULL)
    return Qnil;
  else
    return rb_str_new2((const char*) xns->href);
}

#nextXML::Namespace

Obtain the next namespace.

Usage:

doc = XML::Document.string('<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"/>')
ns = doc.root.namespaces.find_by_href('http://schemas.xmlsoap.org/soap/envelope/')
assert_nil(ns.next)

Returns:



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

static VALUE rxml_namespace_next(VALUE self)
{
  xmlNsPtr xns;
  Data_Get_Struct(self, xmlNs, xns);
  if (xns == NULL || xns->next == NULL)
    return (Qnil);
  else
    return (rxml_namespace_wrap(xns->next, NULL));
}

#node_typeNumeric

Obtain this namespace’s type identifier.

Returns:

  • (Numeric)


110
111
112
113
114
115
# File 'ext/libxml/ruby_xml_namespace.c', line 110

static VALUE rxml_namespace_node_type(VALUE self)
{
  xmlNsPtr xns;
  Data_Get_Struct(self, xmlNs, xns);
  return INT2NUM(xns->type);
}

#prefixObject

Obtain the namespace’s prefix.

Usage:

doc = XML::Document.string('<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"/>')
ns = doc.root.namespaces.find_by_href('http://schemas.xmlsoap.org/soap/envelope/')
assert_equal('soap', ns.prefix)


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

static VALUE rxml_namespace_prefix_get(VALUE self)
{
  xmlNsPtr xns;
  Data_Get_Struct(self, xmlNs, xns);
  if (xns->prefix == NULL)
    return Qnil;
  else
    return rb_str_new2((const char*) xns->prefix);
}

#to_sObject

call-seq:

namespace.to_s -> "string"

Returns the string represenation of a namespace.

Usage:

namespace.to_s


51
52
53
54
55
56
57
# File 'lib/libxml/namespace.rb', line 51

def to_s
  if self.prefix
    "#{self.prefix}:#{self.href}"
  else
    self.href
  end
end