Class: LibXML::XML::Namespace
- Inherits:
-
Object
- Object
- LibXML::XML::Namespace
- 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)
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
call-seq: namespace1 <=> namespace2.
-
#each ⇒ Object
call-seq: namespace.each {|ns| .. }.
-
#href ⇒ Object
Usage:.
-
#initialize(node, "prefix", "href") ⇒ XML::Namespace
constructor
Create a new namespace and adds it to the specified node.
-
#next ⇒ XML::Namespace
Obtain the next namespace.
-
#node_type ⇒ Numeric
Obtain this namespace’s type identifier.
-
#prefix ⇒ Object
Obtain the namespace’s prefix.
-
#to_s ⇒ Object
call-seq: namespace.to_s -> “string”.
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.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'ext/libxml/ruby_xml_namespace.c', line 51
static VALUE rxml_namespace_initialize(VALUE self, VALUE node, VALUE prefix,
VALUE href)
{
xmlNodePtr xnode;
xmlChar *xmlPrefix;
xmlNsPtr xns;
TypedData_Get_Struct(node, xmlNode, &rxml_node_data_type, 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);
RTYPEDDATA_DATA(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.
14 15 16 17 18 19 20 21 22 23 24 |
# 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 ⇒ Object
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
37 38 39 40 41 42 43 44 |
# File 'lib/libxml/namespace.rb', line 37 def each ns = self while ns yield ns ns = ns.next end end |
#href ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'ext/libxml/ruby_xml_namespace.c', line 79
static VALUE rxml_namespace_href_get(VALUE self)
{
xmlNsPtr xns;
TypedData_Get_Struct(self, xmlNs, &rxml_namespace_type, xns);
if (xns->href == NULL)
return Qnil;
else
return rxml_new_cstr( xns->href, NULL);
}
|
#next ⇒ XML::Namespace
136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'ext/libxml/ruby_xml_namespace.c', line 136
static VALUE rxml_namespace_next(VALUE self)
{
xmlNsPtr xns;
TypedData_Get_Struct(self, xmlNs, &rxml_namespace_type, xns);
/* Guard against libxml2's XPath hack where xns->next stores a parent
element pointer instead of the next namespace (see xmlXPathNodeSetAddNs
in xpath.c). xmlNs.type and xmlNode.type share the same struct offset,
so checking the type is safe even when next points to an xmlNode. */
if (xns == NULL || xns->next == NULL || xns->next->type != XML_LOCAL_NAMESPACE)
return (Qnil);
return rxml_namespace_wrap(xns->next);
}
|
#node_type ⇒ Numeric
Obtain this namespace’s type identifier.
95 96 97 98 99 100 |
# File 'ext/libxml/ruby_xml_namespace.c', line 95
static VALUE rxml_namespace_node_type(VALUE self)
{
xmlNsPtr xns;
TypedData_Get_Struct(self, xmlNs, &rxml_namespace_type, xns);
return INT2NUM(xns->type);
}
|
#prefix ⇒ Object
114 115 116 117 118 119 120 121 122 |
# File 'ext/libxml/ruby_xml_namespace.c', line 114
static VALUE rxml_namespace_prefix_get(VALUE self)
{
xmlNsPtr xns;
TypedData_Get_Struct(self, xmlNs, &rxml_namespace_type, xns);
if (xns->prefix == NULL)
return Qnil;
else
return rxml_new_cstr( xns->prefix, NULL);
}
|
#to_s ⇒ Object
call-seq:
namespace.to_s -> "string"
Returns the string represenation of a namespace.
Usage:
namespace.to_s
53 54 55 56 57 58 59 |
# File 'lib/libxml/namespace.rb', line 53 def to_s if self.prefix "#{self.prefix}:#{self.href}" else self.href end end |