123456789_123456789_123456789_123456789_123456789_

Class: Nokogiri::XML::Namespace

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
self, PP::Node
Inherits: Object
Defined in: ext/nokogiri/xml_namespace.c,
ext/nokogiri/xml_reader.c,
lib/nokogiri/xml/namespace.rb

Constant Summary

PP::Node - Included

COLLECTIONS

Instance Attribute Summary

Instance Method Summary

PP::Node - Included

Instance Attribute Details

#document (readonly)

[ GitHub ]

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

attr_reader :document

Instance Method Details

#deconstruct_keys(array_of_names) → Hash)

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

Valid keys and their values:

  • #prefix → (String, nil) The namespace’s prefix, or nil if there is no prefix (e.g., default namespace).

  • #href → (String) The namespace’s URI

Example

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

doc.root.elements.first.namespace
# => #(Namespace:0x35c { href = "http://nokogiri.org/ns/default" })

doc.root.elements.first.namespace.deconstruct_keys([:prefix, :href])
# => {:prefix=>nil, :href=>"http://nokogiri.org/ns/default"}

doc.root.elements.last.namespace
# => #(Namespace:0x370 {
#      prefix = "noko",
#      href = "http://nokogiri.org/ns/noko"
#      })

doc.root.elements.last.namespace.deconstruct_keys([:prefix, :href])
# => {:prefix=>"noko", :href=>"http://nokogiri.org/ns/noko"}

Since v1.14.0

[ GitHub ]

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

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

#href() → String)

Returns the URI reference for this Namespace.

Example

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

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

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

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

#inspect_attributes (private)

[ GitHub ]

  
# File 'lib/nokogiri/xml/namespace.rb', line 52

def inspect_attributes
  [:prefix, :href]
end

#prefix

Alias for Reader#prefix.