Class LibXML::XML::Namespace

  1. ext/libxml/libxml.c
  2. lib/libxml/namespace.rb
  3. show all
Parent: Object

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)

Methods

public class

  1. initialize

public instance

  1. namespace1 <=> namespace2
  2. each
  3. href
  4. next
  5. node_type
  6. prefix
  7. to_s

Included modules

  1. Comparable
  2. Enumerable

Public class methods

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.

[show source]
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;
}

Public instance methods

namespace1 <=> namespace2

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

[show source]
    # File lib/libxml/namespace.rb, line 14
14:       def <=>(other)
15:         if self.prefix.nil? and other.prefix.nil?
16:           self.href <=> other.href
17:         elsif self.prefix.nil?
18:           -1
19:         elsif other.prefix.nil?
20:           1
21:         else
22:           self.prefix <=> other.prefix
23:         end
24:       end
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
[show source]
    # File lib/libxml/namespace.rb, line 37
37:       def each
38:         ns = self
39: 
40:         while ns
41:           yield ns
42:           ns = ns.next
43:         end
44:       end
ns.href → "href"

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)
[show source]
static VALUE rxml_namespace_href_get(VALUE self)
{
  xmlNsPtr xns;
  Data_Get_Struct(self, xmlNs, xns);
  if (xns->href == NULL)
    return Qnil;
  else
    return rxml_namespace_string(xns, (const char*) xns->href);
}
ns.next → XML::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)
[show source]
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));
}
ns.node_type → num

Obtain this namespace’s type identifier.

[show source]
static VALUE rxml_namespace_node_type(VALUE self)
{
  xmlNsPtr xns;
  Data_Get_Struct(self, xmlNs, xns);
  return INT2NUM(xns->type);
}
ns.prefix → "prefix"

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)
[show source]
static VALUE rxml_namespace_prefix_get(VALUE self)
{
  xmlNsPtr xns;
  Data_Get_Struct(self, xmlNs, xns);
  if (xns->prefix == NULL)
    return Qnil;
  else
    return rxml_namespace_string(xns, (const char*) xns->prefix);
}
namespace.to_s → "string"

Returns the string represenation of a namespace.

Usage:

namespace.to_s
[show source]
    # File lib/libxml/namespace.rb, line 53
53:       def to_s
54:         if self.prefix
55:           "#{self.prefix}:#{self.href}"
56:         else
57:           self.href
58:         end
59:       end