123456789_123456789_123456789_123456789_123456789_

Class: LibXML::XML::Namespace

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
self, Enumerable, Comparable
Inherits: Object
Defined in: ext/libxml/ruby_xml_namespace.c,
ext/libxml/ruby_xml_namespace.c,
lib/libxml/namespace.rb

Overview

The Namespace class represents an ::LibXML::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 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)

Class Method Summary

Instance Method Summary

Constructor Details

.new(node, "prefix", "href") ⇒ 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 Namespaces#namespace method.

[ GitHub ]

  
# File 'ext/libxml/ruby_xml_namespace.c', line 46

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);
  xmlResetLastError();

  /* 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);

  DATA_PTR(self) = xns;
  return self;
}

Instance Method Details

#<=>(namespace2)

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

[ GitHub ]

  
# File 'lib/libxml/namespace.rb', line 14

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

#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
[ GitHub ]

  
# File 'lib/libxml/namespace.rb', line 37

def each
  ns = self

  while ns
    yield ns
    ns = ns.next
  end
end

#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)
[ GitHub ]

  
# File 'ext/libxml/ruby_xml_namespace.c', line 75

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_new_cstr( xns->href, NULL);
}

#nextNamespace

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)
[ GitHub ]

  
# File 'ext/libxml/ruby_xml_namespace.c', line 132

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

#node_typeNumeric

Obtain this namespace’s type identifier.

[ GitHub ]

  
# File 'ext/libxml/ruby_xml_namespace.c', line 91

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

#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)
[ GitHub ]

  
# File 'ext/libxml/ruby_xml_namespace.c', line 110

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_new_cstr( xns->prefix, NULL);
}

#to_s ⇒ "string"

Returns the string represenation of a namespace.

Usage:

namespace.to_s
[ GitHub ]

  
# File 'lib/libxml/namespace.rb', line 53

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