Class: Nokogiri::XML::Namespace

Inherits:
Object
  • Object
show all
Includes:
PP::Node
Defined in:
lib/nokogiri/xml/namespace.rb,
ext/nokogiri/xml_namespace.c

Constant Summary

Constants included from PP::Node

PP::Node::COLLECTIONS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PP::Node

#inspect, #pretty_print

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



8
9
10
# File 'lib/nokogiri/xml/namespace.rb', line 8

def document
  @document
end

Instance Method Details

#deconstruct_keys(keys) ⇒ Object

:call-seq: deconstruct_keys(array_of_names) → Hash

Returns a hash describing the Namespace, to use in pattern matching.

Valid keys and their values:
- +prefix+ 


46
47
48
# File 'lib/nokogiri/xml/namespace.rb', line 46

def deconstruct_keys(keys)
  { prefix: prefix, href: href }
end

#hrefObject

:call-seq:

href() 

Returns the URI reference for this Namespace.

Example

doc = Nokogiri::XML.parse("<?xml version=\"1.0\"?>\n<root xmlns=\"http://nokogiri.org/ns/default\" xmlns:noko=\"http://nokogiri.org/ns/noko\">\n  <child1 foo=\"abc\" noko:bar=\"def\"/>\n  <noko:child2 foo=\"qwe\" noko:bar=\"rty\"/>\n</root>\n")

doc.root.elements.first.namespace.href
# => "http://nokogiri.org/ns/default"

doc.root.elements.last.namespace.href
# => "http://nokogiri.org/ns/noko"


126
127
128
129
130
131
132
133
134
135
# File 'ext/nokogiri/xml_namespace.c', line 126

static VALUE
href(VALUE self)
{
  xmlNsPtr ns;

  Noko_Namespace_Get_Struct(self, xmlNs, ns);
  if (!ns->href) { return Qnil; }

  return NOKOGIRI_STR_NEW2(ns->href);
}

#prefixObject

:call-seq:

prefix() 

Return the prefix for this Namespace, or nil if there is no prefix (e.g., default namespace).

Example

doc = Nokogiri::XML.parse("<?xml version=\"1.0\"?>\n<root xmlns=\"http://nokogiri.org/ns/default\" xmlns:noko=\"http://nokogiri.org/ns/noko\">\n  <child1 foo=\"abc\" noko:bar=\"def\"/>\n  <noko:child2 foo=\"qwe\" noko:bar=\"rty\"/>\n</root>\n")

doc.root.elements.first.namespace.prefix
# => nil

doc.root.elements.last.namespace.prefix
# => "noko"


93
94
95
96
97
98
99
100
101
102
# File 'ext/nokogiri/xml_namespace.c', line 93

static VALUE
prefix(VALUE self)
{
  xmlNsPtr ns;

  Noko_Namespace_Get_Struct(self, xmlNs, ns);
  if (!ns->prefix) { return Qnil; }

  return NOKOGIRI_STR_NEW2(ns->prefix);
}