Class: LibXML::XML::Namespaces
| Relationships & Source Files | |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Instance Chain:
self,
Enumerable
|
|
| Inherits: | Object |
| Defined in: | ext/libxml/ruby_xml_namespaces.c, lib/libxml/namespaces.rb |
Class Method Summary
-
.new(XML::Node) ⇒ Namespaces
constructor
Creates a new namespaces object.
Instance Attribute Summary
-
#default_prefix=("string")
writeonly
Assigns a name (prefix) to the default namespace.
-
#namespace ⇒ XML::Namespace
rw
Returns the current node’s namespace.
-
#namespace=(XML::Namespace)
rw
Sets the current node’s namespace.
Instance Method Summary
-
#default ⇒ XML::Namespace
Returns the default namespace for this node or nil.
-
#definitions ⇒ Array, XML::Namespace
Returns an array of
Namespaceobjects that are defined on this node. -
#each {|XML::Namespace| ... }
Iterates over the namespace objects that are in context for this node.
-
#find_by_href(href) ⇒ XML::Namespace
Searches for a namespace that has the specified href.
-
#find_by_prefix(prefix = nil) ⇒ XML::Namespace
Searches for a namespace that has the specified prefix.
-
#node ⇒ XML::Node
Returns the current node.
Constructor Details
.new(XML::Node) ⇒ Namespaces
# File 'ext/libxml/ruby_xml_namespaces.c', line 62
static VALUE rxml_namespaces_initialize(VALUE self, VALUE node)
{
xmlNodePtr xnode;
TypedData_Get_Struct(node, xmlNode, &rxml_node_data_type, xnode);
RTYPEDDATA_DATA(self) = xnode;
return self;
}
Instance Attribute Details
#default_prefix=("string") (writeonly)
# File 'lib/libxml/namespaces.rb', line 30
def default_prefix=(prefix) # Find default prefix ns = find_by_prefix(nil) raise(ArgumentError, "No default namespace was found") unless ns Namespace.new(self.node, prefix, ns.href) end
#namespace ⇒ XML::Namespace (rw)
# File 'ext/libxml/ruby_xml_namespaces.c', line 225
static VALUE rxml_namespaces_namespace_get(VALUE self)
{
xmlNodePtr xnode;
TypedData_Get_Struct(self, xmlNode, &rxml_namespaces_type, xnode);
if (xnode->ns)
return rxml_namespace_wrap(xnode->ns);
else
return Qnil;
}
#namespace=(XML::Namespace) (rw)
Sets the current node’s namespace.
Basic usage:
# Create a node
node = XML::Node.new('Envelope')
# Define the soap namespace - this does *not* put the node in the namespace
ns = 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)
# Now put the node in the soap namespace, not how the string representation changes
node.namespaces.namespace = ns
assert_equal("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"/>", node.to_s)
# File 'ext/libxml/ruby_xml_namespaces.c', line 255
static VALUE rxml_namespaces_namespace_set(VALUE self, VALUE ns)
{
xmlNodePtr xnode;
xmlNsPtr xns;
TypedData_Get_Struct(self, xmlNode, &rxml_namespaces_type, xnode);
TypedData_Get_Struct(ns, xmlNs, &rxml_namespace_type, xns);
xmlSetNs(xnode, xns);
return self;
}
Instance Method Details
#default ⇒ XML::Namespace
# File 'lib/libxml/namespaces.rb', line 15
def default find_by_prefix(nil) end
#definitions ⇒ Array, XML::Namespace
# File 'ext/libxml/ruby_xml_namespaces.c', line 84
static VALUE rxml_namespaces_definitions(VALUE self)
{
xmlNodePtr xnode;
xmlNsPtr xns;
VALUE arr;
TypedData_Get_Struct(self, xmlNode, &rxml_namespaces_type, xnode);
arr = rb_ary_new();
xns = xnode->nsDef;
while (xns)
{
VALUE anamespace = rxml_namespace_wrap(xns);
rb_ary_push(arr, anamespace);
xns = xns->next;
}
return arr;
}
#each {|XML::Namespace| ... }
# File 'ext/libxml/ruby_xml_namespaces.c', line 119
static VALUE rxml_namespaces_each(VALUE self)
{
xmlNodePtr xnode;
xmlNsPtr *nsList, *xns;
TypedData_Get_Struct(self, xmlNode, &rxml_namespaces_type, xnode);
nsList = xmlGetNsList(xnode->doc, xnode);
if (nsList == NULL)
return (Qnil);
for (xns = nsList; *xns != NULL; xns++)
{
VALUE ns = rxml_namespace_wrap(*xns);
rb_yield(ns);
}
xmlFree(nsList);
return Qnil;
}
#find_by_href(href) ⇒ XML::Namespace
Searches for a namespace that has the specified href. The search starts at the current node and works upward through the node’s parents. If a namespace is found, then an Namespace instance is returned, otherwise nil is returned.
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)
assert_equal('http://schemas.xmlsoap.org/soap/envelope/', ns.href)
# File 'ext/libxml/ruby_xml_namespaces.c', line 158
static VALUE rxml_namespaces_find_by_href(VALUE self, VALUE href)
{
xmlNodePtr xnode;
xmlNsPtr xns;
Check_Type(href, T_STRING);
TypedData_Get_Struct(self, xmlNode, &rxml_namespaces_type, xnode);
xns = xmlSearchNsByHref(xnode->doc, xnode, (xmlChar*) StringValuePtr(href));
if (xns)
return rxml_namespace_wrap(xns);
else
return Qnil;
}
#find_by_prefix(prefix = nil) ⇒ XML::Namespace
Searches for a namespace that has the specified prefix. The search starts at the current node and works upward through the node’s parents. If a namespace is found, then an Namespace instance is returned, otherwise nil is returned.
Usage:
doc = XML::Document.string('<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"/>')
ns = doc.root.namespaces.find_by_prefix('soap')
assert_equal('soap', ns.prefix)
assert_equal('http://schemas.xmlsoap.org/soap/envelope/', ns.href)
# File 'ext/libxml/ruby_xml_namespaces.c', line 190
static VALUE rxml_namespaces_find_by_prefix(VALUE self, VALUE prefix)
{
xmlNodePtr xnode;
xmlNsPtr xns;
xmlChar* xprefix = NULL;
if (!NIL_P(prefix))
{
Check_Type(prefix, T_STRING);
xprefix = (xmlChar*) StringValuePtr(prefix);
}
TypedData_Get_Struct(self, xmlNode, &rxml_namespaces_type, xnode);
xns = xmlSearchNs(xnode->doc, xnode, xprefix);
if (xns)
return rxml_namespace_wrap(xns);
else
return Qnil;
}
#node ⇒ XML::Node
Returns the current node.
# File 'ext/libxml/ruby_xml_namespaces.c', line 274
static VALUE rxml_namespaces_node_get(VALUE self)
{
xmlNodePtr xnode;
TypedData_Get_Struct(self, xmlNode, &rxml_namespaces_type, xnode);
return rxml_node_wrap(xnode);
}