Nodes are the primary objects that make up an XML document. The node class represents most node types that are found in an XML document (but not LibXML::XML::Attributes, see LibXML::XML::Attr). It exposes libxml’s full API for creating, querying moving and deleting node objects. Many of these methods are documented in the DOM Level 3 specification found at: www.w3.org/TR/DOM-Level-3-Core/.
Methods
public class
public instance
-
curr_node << "Some text"
curr_node << node - ==
-
property
node["name"] - node["name"]=
- attribute?
- attribute_decl?
- attributes
- attributes?
- base_uri
- base_uri=
- empty?
- cdata?
- child
- child?
- children
- children?
- clone
- comment?
- content
- content=
- content_stripped
- context
- copy
- debug
- doc
- docbook_doc?
- doctype?
- document?
- dtd?
- dup
- each
- each_attr
- each_child
- each_element
- element?
- element_decl?
- empty?
- entity?
- entity_ref?
- eql?
- find
- find_first
- first
- first?
- fragment?
- html_doc?
- inner_xml
- lang
- lang=
- last
- last?
- line_num
- name
- name=
- namespace?
- namespacess
- next
- next=
- next?
- type
- node_type_name
- notation?
- output_escaping=
- output_escaping?
- parent
- parent?
- path
- pi?
- pointer
- prev
- prev=
- prev?
- properties
- properties?
- property
- remove!
- sibling=
- space_preserve
- space_preserve=
- text?
- to_s
- xinclude_end?
- xinclude_start?
- xlink?
- xlink_type
- xlink_type_name
Included modules
- Enumerable
Constants
| SPACE_DEFAULT | = | INT2NUM(0) |
| SPACE_PRESERVE | = | INT2NUM(1) |
| SPACE_NOT_INHERIT | = | INT2NUM(-1) |
| XLINK_ACTUATE_AUTO | = | INT2NUM(1) |
| XLINK_ACTUATE_NONE | = | INT2NUM(0) |
| XLINK_ACTUATE_ONREQUEST | = | INT2NUM(2) |
| XLINK_SHOW_EMBED | = | INT2NUM(2) |
| XLINK_SHOW_NEW | = | INT2NUM(1) |
| XLINK_SHOW_NONE | = | INT2NUM(0) |
| XLINK_SHOW_REPLACE | = | INT2NUM(3) |
| XLINK_TYPE_EXTENDED | = | INT2NUM(2) |
| XLINK_TYPE_EXTENDED_SET | = | INT2NUM(3) |
| XLINK_TYPE_NONE | = | INT2NUM(0) |
| XLINK_TYPE_SIMPLE | = | INT2NUM(1) |
| ELEMENT_NODE | = | INT2FIX(XML_ELEMENT_NODE) |
| ATTRIBUTE_NODE | = | INT2FIX(XML_ATTRIBUTE_NODE) |
| TEXT_NODE | = | INT2FIX(XML_TEXT_NODE) |
| CDATA_SECTION_NODE | = | INT2FIX(XML_CDATA_SECTION_NODE) |
| ENTITY_REF_NODE | = | INT2FIX(XML_ENTITY_REF_NODE) |
| ENTITY_NODE | = | INT2FIX(XML_ENTITY_NODE) |
| PI_NODE | = | INT2FIX(XML_PI_NODE) |
| COMMENT_NODE | = | INT2FIX(XML_COMMENT_NODE) |
| DOCUMENT_NODE | = | INT2FIX(XML_DOCUMENT_NODE) |
| DOCUMENT_TYPE_NODE | = | INT2FIX(XML_DOCUMENT_TYPE_NODE) |
| DOCUMENT_FRAG_NODE | = | INT2FIX(XML_DOCUMENT_FRAG_NODE) |
| NOTATION_NODE | = | INT2FIX(XML_NOTATION_NODE) |
| HTML_DOCUMENT_NODE | = | INT2FIX(XML_HTML_DOCUMENT_NODE) |
| DTD_NODE | = | INT2FIX(XML_DTD_NODE) |
| ELEMENT_DECL | = | INT2FIX(XML_ELEMENT_DECL) |
| ATTRIBUTE_DECL | = | INT2FIX(XML_ATTRIBUTE_DECL) |
| ENTITY_DECL | = | INT2FIX(XML_ENTITY_DECL) |
| NAMESPACE_DECL | = | INT2FIX(XML_NAMESPACE_DECL) |
| XINCLUDE_START | = | INT2FIX(XML_XINCLUDE_START) |
| XINCLUDE_END | = | INT2FIX(XML_XINCLUDE_END) |
| DOCB_DOCUMENT_NODE | = | INT2FIX(XML_DOCB_DOCUMENT_NODE) |
| DOCB_DOCUMENT_NODE | = | Qnil |
Public class methods
Creates a new element with the specified name, content and namespace. The content and namespace may be nil.
static VALUE rxml_node_initialize(int argc, VALUE *argv, VALUE self)
{
VALUE name;
VALUE content;
VALUE ns;
xmlNodePtr xnode = NULL;
xmlNsPtr xns = NULL;
rb_scan_args(argc, argv, "12", &name, &content, &ns);
name = rb_obj_as_string(name);
if (!NIL_P(ns))
Data_Get_Struct(ns, xmlNs, xns);
xnode = xmlNewNode(xns, (xmlChar*) StringValuePtr(name));
if (xnode == NULL)
rxml_raise(&xmlLastError);
/* Link the Ruby object to the libxml object and vice-versa. */
xnode->_private = (void*) self;
DATA_PTR(self) = xnode;
if (!NIL_P(content))
rxml_node_content_set(self, content);
return self;
}
Create a new CDATA node, optionally setting the node’s content.
static VALUE rxml_node_new_cdata(int argc, VALUE *argv, VALUE klass)
{
VALUE content = Qnil;
xmlNodePtr xnode;
rb_scan_args(argc, argv, "01", &content);
if (NIL_P(content))
{
xnode = xmlNewCDataBlock(NULL, NULL, 0);
}
else
{
content = rb_obj_as_string(content);
xnode = xmlNewCDataBlock(NULL, (xmlChar*) StringValuePtr(content),
RSTRING_LEN(content));
}
if (xnode == NULL)
rxml_raise(&xmlLastError);
return rxml_node_wrap(xnode);
}
Create a new comment node, optionally setting the node’s content.
static VALUE rxml_node_new_comment(int argc, VALUE *argv, VALUE klass)
{
VALUE content = Qnil;
xmlNodePtr xnode;
rb_scan_args(argc, argv, "01", &content);
if (NIL_P(content))
{
xnode = xmlNewComment(NULL);
}
else
{
content = rb_obj_as_string(content);
xnode = xmlNewComment((xmlChar*) StringValueCStr(content));
}
if (xnode == NULL)
rxml_raise(&xmlLastError);
return rxml_node_wrap(xnode);
}
Create a new text node.
static VALUE rxml_node_new_text(VALUE klass, VALUE content)
{
xmlNodePtr xnode;
Check_Type(content, T_STRING);
content = rb_obj_as_string(content);
xnode = xmlNewText((xmlChar*) StringValueCStr(content));
if (xnode == NULL)
rxml_raise(&xmlLastError);
return rxml_node_wrap(xnode);
}
Public instance methods
curr_node << node
Add the specified text or XML::Node as a new child node to the current node.
If the specified argument is a string, it should be a raw string that contains unescaped XML special characters. Entity references are not supported.
The method will return the current node.
static VALUE rxml_node_content_add(VALUE self, VALUE obj)
{
xmlNodePtr xnode;
VALUE str;
xnode = rxml_get_xnode(self);
/* XXX This should only be legal for a CDATA type node, I think,
* resulting in a merge of content, as if a string were passed
* danj 070827
*/
if (rb_obj_is_kind_of(obj, cXMLNode))
{
rxml_node_modify_dom(self, obj, xmlAddChild);
}
else
{
str = rb_obj_as_string(obj);
if (NIL_P(str) || TYPE(str) != T_STRING)
rb_raise(rb_eTypeError, "invalid argument: must be string or XML::Node");
xmlNodeAddContent(xnode, (xmlChar*) StringValuePtr(str));
}
return self;
}
node["name"] → "string"
Obtain the named pyroperty.
static VALUE rxml_node_attribute_get(VALUE self, VALUE name)
{
VALUE attributes = rxml_node_attributes_get(self);
return rxml_attributes_attribute_get(attributes, name);
}
Set the named property.
static VALUE rxml_node_property_set(VALUE self, VALUE name, VALUE value)
{
VALUE attributes = rxml_node_attributes_get(self);
return rxml_attributes_attribute_set(attributes, name, value);
}
Specifies if this is an attribute node
# File lib/libxml/node.rb, line 207 207: def attribute? 208: node_type == ATTRIBUTE_NODE 209: end
Specifies if this is an attribute declaration node
# File lib/libxml/node.rb, line 212 212: def attribute_decl? 213: node_type == ATTRIBUTE_DECL 214: end
Returns the XML::Attributes for this node.
static VALUE rxml_node_attributes_get(VALUE self)
{
xmlNodePtr xnode;
xnode = rxml_get_xnode(self);
return rxml_attributes_new(xnode);
}
Determines whether this node has attributes
# File lib/libxml/node.rb, line 9 9: def attributes? 10: attributes.length > 0 11: end
Obtain this node’s base URI.
static VALUE rxml_node_base_uri_get(VALUE self)
{
xmlNodePtr xnode;
xmlChar* base_uri;
VALUE result = Qnil;
xnode = rxml_get_xnode(self);
if (xnode->doc == NULL)
return (result);
base_uri = xmlNodeGetBase(xnode->doc, xnode);
if (base_uri)
{
result = rxml_str_new2((const char*) base_uri, xnode->doc ? xnode->doc->encoding : NULL);
xmlFree(base_uri);
}
return (result);
}
Set this node’s base URI.
static VALUE rxml_node_base_uri_set(VALUE self, VALUE uri)
{
xmlNodePtr xnode;
Check_Type(uri, T_STRING);
xnode = rxml_get_xnode(self);
if (xnode->doc == NULL)
return (Qnil);
xmlNodeSetBase(xnode, (xmlChar*) StringValuePtr(uri));
return (Qtrue);
}
Determine whether this node is an empty or whitespace only text-node.
static VALUE rxml_node_empty_q(VALUE self)
{
xmlNodePtr xnode;
xnode = rxml_get_xnode(self);
if (xnode == NULL)
return (Qnil);
return ((xmlIsBlankNode(xnode) == 1) ? Qtrue : Qfalse);
}
Specifies if this is an CDATA node
# File lib/libxml/node.rb, line 217 217: def cdata? 218: node_type == CDATA_SECTION_NODE 219: end
Returns this node’s children as an array.
# File lib/libxml/node.rb, line 131 131: def children 132: entries 133: end
Create a shallow copy of the node. To create a deep copy call Node#copy(true)
# File lib/libxml/node.rb, line 15 15: def clone 16: copy(false) 17: end
Specifies if this is an comment node
# File lib/libxml/node.rb, line 222 222: def comment? 223: node_type == COMMENT_NODE 224: end
Obtain this node’s content as a string.
static VALUE rxml_node_content_get(VALUE self)
{
xmlNodePtr xnode;
xmlChar *content;
VALUE result = Qnil;
xnode = rxml_get_xnode(self);
content = xmlNodeGetContent(xnode);
if (content)
{
result = rxml_str_new2((const char *) content, xnode->doc ? xnode->doc->encoding : NULL);
xmlFree(content);
}
return result;
}
Set this node’s content to the specified string.
static VALUE rxml_node_content_set(VALUE self, VALUE content)
{
xmlNodePtr xnode;
Check_Type(content, T_STRING);
xnode = rxml_get_xnode(self);
// XXX docs indicate need for escaping entites, need to be done? danj
xmlNodeSetContent(xnode, (xmlChar*) StringValuePtr(content));
return (Qtrue);
}
Obtain this node’s stripped content.
Deprecated: Stripped content can be obtained via the content method.
static VALUE rxml_node_content_stripped_get(VALUE self)
{
xmlNodePtr xnode;
xmlChar* content;
VALUE result = Qnil;
xnode = rxml_get_xnode(self);
if (!xnode->content)
return result;
content = xmlNodeGetContent(xnode);
if (content)
{
result = rxml_str_new2((const char*) content, xnode->doc ? xnode->doc->encoding : NULL);
xmlFree(content);
}
return (result);
}
Returns a new XML::XPathContext for the current node.
Namespaces is an optional array of XML::NS objects
# File lib/libxml/node.rb, line 53 53: def context(nslist = nil) 54: if not self.doc 55: raise(TypeError, "A node must belong to a document before a xpath context can be created") 56: end 57: 58: context = XPath::Context.new(self) 59: context.node = self 60: context.register_namespaces_from_node(self) 61: context.register_namespaces_from_node(self.doc.root) 62: context.register_namespaces(nslist) if nslist 63: context 64: end
Creates a copy of this node. To create a shallow copy set the deep parameter to false. To create a deep copy set the deep parameter to true.
static VALUE rxml_node_copy(VALUE self, VALUE deep)
{
xmlNodePtr xnode;
xmlNodePtr xcopy;
int recursive = (deep == Qnil || deep == Qfalse) ? 0 : 1;
xnode = rxml_get_xnode(self);
xcopy = xmlCopyNode(xnode, recursive);
if (xcopy)
return rxml_node_wrap(xcopy);
else
return Qnil;
}
Print libxml debugging information to stdout. Requires that libxml was compiled with debugging enabled.
static VALUE rxml_node_debug(VALUE self)
{
#ifdef LIBXML_DEBUG_ENABLED
xmlNodePtr xnode;
xnode = rxml_get_xnode(self);
xmlDebugDumpNode(NULL, xnode, 2);
return Qtrue;
#else
rb_warn("libxml was compiled without debugging support.")
return Qfalse;
#endif
}
Obtain the XML::Document this node belongs to.
static VALUE rxml_node_doc(VALUE self)
{
xmlDocPtr xdoc = NULL;
xmlNodePtr xnode = rxml_get_xnode(self);
switch (xnode->type)
{
case XML_DOCUMENT_NODE:
#ifdef LIBXML_DOCB_ENABLED
case XML_DOCB_DOCUMENT_NODE:
#endif
case XML_HTML_DOCUMENT_NODE:
case XML_NAMESPACE_DECL:
break;
case XML_ATTRIBUTE_NODE:
xdoc = (xmlDocPtr)((xmlAttrPtr) xnode->doc);
break;
default:
xdoc = xnode->doc;
}
if (xdoc == NULL)
return (Qnil);
else if (xdoc->_private)
return (VALUE) xdoc->_private;
else
/* This can happen by calling Reader#expand.doc */
rb_raise(eXMLError, "Document is not accessible to Ruby (hint - did you call Reader#expand?)");
}
Specifies if this is an docbook node
# File lib/libxml/node.rb, line 227 227: def docbook_doc? 228: node_type == DOCB_DOCUMENT_NODE 229: end
Specifies if this is an doctype node
# File lib/libxml/node.rb, line 232 232: def doctype? 233: node_type == DOCUMENT_TYPE_NODE 234: end
Specifies if this is an document node
# File lib/libxml/node.rb, line 237 237: def document? 238: node_type == DOCUMENT_NODE 239: end
Specifies if this is an DTD node
# File lib/libxml/node.rb, line 242 242: def dtd? 243: node_type == DTD_NODE 244: end
Create a shallow copy of the node. To create a deep copy call Node#copy(true)
# File lib/libxml/node.rb, line 43 43: def dup 44: copy(false) 45: end
Iterates over this node’s children, including text nodes, element nodes, etc. If you wish to iterate only over child elements, use XML::Node#each_element.
doc = XML::Document.new('model/books.xml')
doc.root.each {|node| puts node}
static VALUE rxml_node_each(VALUE self)
{
xmlNodePtr xnode;
xmlNodePtr xcurrent;
xnode = rxml_get_xnode(self);
xcurrent = xnode->children;
while (xcurrent)
{
/* The user could remove this node, so first stache
away the next node. */
xmlNodePtr xnext = xcurrent->next;
rb_yield(rxml_node_wrap(xcurrent));
xcurrent = xnext;
}
return Qnil;
}
——- Traversal —————- Iterates over this node’s attributes.
doc = XML::Document.new('model/books.xml')
doc.root.each_attr {|attr| puts attr}
# File lib/libxml/node.rb, line 103 103: def each_attr 104: attributes.each do |attr| 105: yield(attr) 106: end 107: end
Iterates over this node’s child elements (nodes that have a node_type == ELEMENT_NODE).
doc = XML::Document.new('model/books.xml')
doc.root.each_element {|element| puts element}
# File lib/libxml/node.rb, line 114 114: def each_element 115: each do |node| 116: yield(node) if node.node_type == ELEMENT_NODE 117: end 118: end
Specifies if this is an element node
# File lib/libxml/node.rb, line 247 247: def element? 248: node_type == ELEMENT_NODE 249: end
Specifies if this is an element declaration node
# File lib/libxml/node.rb, line 257 257: def element_decl? 258: node_type == ELEMENT_DECL 259: end
Determine whether this node is an empty or whitespace only text-node.
static VALUE rxml_node_empty_q(VALUE self)
{
xmlNodePtr xnode;
xnode = rxml_get_xnode(self);
if (xnode == NULL)
return (Qnil);
return ((xmlIsBlankNode(xnode) == 1) ? Qtrue : Qfalse);
}
Specifies if this is an entity node
# File lib/libxml/node.rb, line 252 252: def entity? 253: node_type == ENTITY_NODE 254: end
Specifies if this is an entity reference node
# File lib/libxml/node.rb, line 262 262: def entity_ref? 263: node_type == ENTITY_REF_NODE 264: end
Test equality between the two nodes. Two nodes are equal if they are the same node or have the same XML representation.
static VALUE rxml_node_eql_q(VALUE self, VALUE other)
{
if(self == other)
{
return Qtrue;
}
else if (NIL_P(other))
{
return Qfalse;
}
else
{
VALUE self_xml;
VALUE other_xml;
if (rb_obj_is_kind_of(other, cXMLNode) == Qfalse)
rb_raise(rb_eTypeError, "Nodes can only be compared against other nodes");
self_xml = rxml_node_to_s(0, NULL, self);
other_xml = rxml_node_to_s(0, NULL, other);
return(rb_funcall(self_xml, rb_intern("=="), 1, other_xml));
}
}
Return nodes matching the specified xpath expression. For more information, please refer to the documentation for XML::Document#find.
Namespaces is an optional array of XML::NS objects
# File lib/libxml/node.rb, line 74 74: def find(xpath, nslist = nil) 75: self.context(nslist).find(xpath) 76: end
Return the first node matching the specified xpath expression. For more information, please refer to the documentation for the find method.
# File lib/libxml/node.rb, line 84 84: def find_first(xpath, nslist = nil) 85: find(xpath, nslist).first 86: end
Returns this node’s first child node if any.
static VALUE rxml_node_first_get(VALUE self)
{
xmlNodePtr xnode;
xnode = rxml_get_xnode(self);
if (xnode->children)
return (rxml_node_wrap(xnode->children));
else
return (Qnil);
}
Determines whether this node has a first node
# File lib/libxml/node.rb, line 126 126: def first? 127: not first.nil? 128: end
Specifies if this is a fragment node
# File lib/libxml/node.rb, line 267 267: def fragment? 268: node_type == DOCUMENT_FRAG_NODE 269: end
Specifies if this is a html document node
# File lib/libxml/node.rb, line 272 272: def html_doc? 273: node_type == HTML_DOCUMENT_NODE 274: end
node.inner_xml(:indent => true, :encoding => 'UTF-8', :level => 0) → "string"
Converts a node’s children, to a string representation. To include the node, use XML::Node#to_s. For more information about the supported options, see XML::Node#to_s.
# File lib/libxml/node.rb, line 26 26: def inner_xml(options = Hash.new) 27: io = nil 28: self.each do |node| 29: xml = node.to_s(options) 30: # Create the string IO here since we now know the encoding 31: io = create_string_io(xml) unless io 32: io << xml 33: end 34: 35: io.string 36: end
Obtain the language set for this node, if any. This is set in XML via the xml:lang attribute.
static VALUE rxml_node_lang_get(VALUE self)
{
xmlNodePtr xnode;
xmlChar *lang;
VALUE result = Qnil;
xnode = rxml_get_xnode(self);
lang = xmlNodeGetLang(xnode);
if (lang)
{
result = rxml_str_new2((const char*) lang, xnode->doc ? xnode->doc->encoding : NULL);
xmlFree(lang);
}
return (result);
}
Set the language for this node. This affects the value of the xml:lang attribute.
static VALUE rxml_node_lang_set(VALUE self, VALUE lang)
{
xmlNodePtr xnode;
Check_Type(lang, T_STRING);
xnode = rxml_get_xnode(self);
xmlNodeSetLang(xnode, (xmlChar*) StringValuePtr(lang));
return (Qtrue);
}
Obtain the last child node of this node, if any.
static VALUE rxml_node_last_get(VALUE self)
{
xmlNodePtr xnode;
xnode = rxml_get_xnode(self);
if (xnode->last)
return (rxml_node_wrap(xnode->last));
else
return (Qnil);
}
Determines whether this node has a last node
# File lib/libxml/node.rb, line 146 146: def last? 147: not last.nil? 148: end
Obtain the line number (in the XML document) that this node was read from. If default_line_numbers is set false (the default), this method returns zero.
static VALUE rxml_node_line_num(VALUE self)
{
xmlNodePtr xnode;
long line_num;
xnode = rxml_get_xnode(self);
if (!xmlLineNumbersDefaultValue)
rb_warn(
"Line numbers were not retained: use XML::Parser::default_line_numbers=true");
line_num = xmlGetLineNo(xnode);
if (line_num == -1)
return (Qnil);
else
return (INT2NUM((long) line_num));
}
Obtain this node’s name.
static VALUE rxml_node_name_get(VALUE self)
{
xmlNodePtr xnode;
const xmlChar *name;
xnode = rxml_get_xnode(self);
switch (xnode->type)
{
case XML_DOCUMENT_NODE:
#ifdef LIBXML_DOCB_ENABLED
case XML_DOCB_DOCUMENT_NODE:
#endif
case XML_HTML_DOCUMENT_NODE:
{
xmlDocPtr doc = (xmlDocPtr) xnode;
name = doc->URL;
break;
}
case XML_ATTRIBUTE_NODE:
{
xmlAttrPtr attr = (xmlAttrPtr) xnode;
name = attr->name;
break;
}
case XML_NAMESPACE_DECL:
{
xmlNsPtr ns = (xmlNsPtr) xnode;
name = ns->prefix;
break;
}
default:
name = xnode->name;
break;
}
if (xnode->name == NULL)
return (Qnil);
else
return (rxml_str_new2((const char*) name, xnode->doc ? xnode->doc->encoding : NULL));
}
Set this node’s name.
static VALUE rxml_node_name_set(VALUE self, VALUE name)
{
xmlNodePtr xnode;
const xmlChar *xname;
Check_Type(name, T_STRING);
xnode = rxml_get_xnode(self);
xname = (const xmlChar*)StringValuePtr(name);
/* Note: calling xmlNodeSetName() for a text node is ignored by libXML. */
xmlNodeSetName(xnode, xname);
return (Qtrue);
}
Specifies if this is a namespace node (not if it has a namepsace)
# File lib/libxml/node.rb, line 278 278: def namespace? 279: node_type == NAMESPACE_DECL 280: end
Returns this node’s XML::Namespaces object, which is used to access the namespaces associated with this node.
# File lib/libxml/node.rb, line 94 94: def namespaces 95: @namespaces ||= XML::Namespaces.new(self) 96: end
Returns the next sibling node if one exists.
static VALUE rxml_node_next_get(VALUE self)
{
xmlNodePtr xnode;
xnode = rxml_get_xnode(self);
if (xnode->next)
return (rxml_node_wrap(xnode->next));
else
return (Qnil);
}
Adds the specified node as the next sibling of the current node. If the node already exists in the document, it is first removed from its existing context. Any adjacent text nodes will be merged together, meaning the returned node may be different than the original node.
static VALUE rxml_node_next_set(VALUE self, VALUE next)
{
return rxml_node_modify_dom(self, next, xmlAddNextSibling);
}
Determines whether this node has a next node
# File lib/libxml/node.rb, line 136 136: def next? 137: not self.next.nil? 138: end
Obtain this node’s type identifier.
static VALUE rxml_node_type(VALUE self)
{
xmlNodePtr xnode;
xnode = rxml_get_xnode(self);
return (INT2NUM(xnode->type));
}
Returns this node’s type name
# File lib/libxml/node.rb, line 154 154: def node_type_name 155: case node_type 156: # Most common choices first 157: when ATTRIBUTE_NODE 158: 'attribute' 159: when DOCUMENT_NODE 160: 'document_xml' 161: when ELEMENT_NODE 162: 'element' 163: when TEXT_NODE 164: 'text' 165: 166: # Now the rest 167: when ATTRIBUTE_DECL 168: 'attribute_decl' 169: when CDATA_SECTION_NODE 170: 'cdata' 171: when COMMENT_NODE 172: 'comment' 173: when DOCB_DOCUMENT_NODE 174: 'document_docbook' 175: when DOCUMENT_FRAG_NODE 176: 'fragment' 177: when DOCUMENT_TYPE_NODE 178: 'doctype' 179: when DTD_NODE 180: 'dtd' 181: when ELEMENT_DECL 182: 'elem_decl' 183: when ENTITY_DECL 184: 'entity_decl' 185: when ENTITY_NODE 186: 'entity' 187: when ENTITY_REF_NODE 188: 'entity_ref' 189: when HTML_DOCUMENT_NODE 190: 'document_html' 191: when NAMESPACE_DECL 192: 'namespace' 193: when NOTATION_NODE 194: 'notation' 195: when PI_NODE 196: 'pi' 197: when XINCLUDE_START 198: 'xinclude_start' 199: when XINCLUDE_END 200: 'xinclude_end' 201: else 202: raise(UnknownType, "Unknown node type: %n", node.node_type); 203: end 204: end
Specifies if this is a notation node
# File lib/libxml/node.rb, line 283 283: def notation? 284: node_type == NOTATION_NODE 285: end
element_node.output_escaping = true|false
attribute_node.output_escaping = true|false
Controls whether this text node or the immediate text node children of an element or attribute node escapes their output. Any other type of node will simply ignore this operation.
Text nodes which are added to an element or attribute node will be affected by any previous setting of this property.
static VALUE rxml_node_output_escaping_set(VALUE self, VALUE bool)
{
xmlNodePtr xnode;
xnode = rxml_get_xnode(self);
switch (xnode->type) {
case XML_TEXT_NODE:
xnode->name = (bool!=Qfalse && bool!=Qnil) ? xmlStringText : xmlStringTextNoenc;
break;
case XML_ELEMENT_NODE:
case XML_ATTRIBUTE_NODE:
{
const xmlChar *name = (bool!=Qfalse && bool!=Qnil) ? xmlStringText : xmlStringTextNoenc;
xmlNodePtr tmp;
for (tmp = xnode->children; tmp; tmp = tmp->next)
if (tmp->type == XML_TEXT_NODE)
tmp->name = name;
}
break;
default:
return Qnil;
}
return (bool!=Qfalse && bool!=Qnil) ? Qtrue : Qfalse;
}
element_node.output_escaping? → (true|false|nil)
attribute_node.output_escaping? → (true|false|nil)
other_node.output_escaping? → (nil)
Determine whether this node escapes it’s output or not.
Text nodes return only true or false. Element and attribute nodes examine their immediate text node children to determine the value. Any other type of node always returns nil.
If an element or attribute node has at least one immediate child text node and all the immediate text node children have the same output_escaping? value, that value is returned. Otherwise, nil is returned.
static VALUE rxml_node_output_escaping_q(VALUE self)
{
xmlNodePtr xnode;
xnode = rxml_get_xnode(self);
switch (xnode->type) {
case XML_TEXT_NODE:
return xnode->name==xmlStringTextNoenc ? Qfalse : Qtrue;
case XML_ELEMENT_NODE:
case XML_ATTRIBUTE_NODE:
{
xmlNodePtr tmp = xnode->children;
const xmlChar *match = NULL;
/* Find the first text node and use it as the reference. */
while (tmp && tmp->type != XML_TEXT_NODE)
tmp = tmp->next;
if (! tmp)
return Qnil;
match = tmp->name;
/* Walk the remaining text nodes until we run out or one doesn't match. */
while (tmp && (tmp->type != XML_TEXT_NODE || match == tmp->name))
tmp = tmp->next;
/* We're left with either the mismatched node or the aggregate result. */
return tmp ? Qnil : (match==xmlStringTextNoenc ? Qfalse : Qtrue);
}
break;
default:
return Qnil;
}
}
Obtain this node’s parent node, if any.
static VALUE rxml_node_parent_get(VALUE self)
{
xmlNodePtr xnode;
xnode = rxml_get_xnode(self);
if (xnode->parent)
return (rxml_node_wrap(xnode->parent));
else
return (Qnil);
}
Determines whether this node has a parent node
# File lib/libxml/node.rb, line 121 121: def parent? 122: not parent.nil? 123: end
Obtain this node’s path.
static VALUE rxml_node_path(VALUE self)
{
xmlNodePtr xnode;
xmlChar *path;
xnode = rxml_get_xnode(self);
path = xmlGetNodePath(xnode);
if (path == NULL)
return (Qnil);
else
return (rxml_str_new2((const char*) path, xnode->doc ? xnode->doc->encoding : NULL));
}
Specifies if this is a processiong instruction node
# File lib/libxml/node.rb, line 288 288: def pi? 289: node_type == PI_NODE 290: end
Evaluates an XPointer expression relative to this node.
static VALUE rxml_node_pointer(VALUE self, VALUE xptr_str)
{
return (rxml_xpointer_point2(self, xptr_str));
}
Obtain the previous sibling, if any.
static VALUE rxml_node_prev_get(VALUE self)
{
xmlNodePtr xnode;
xmlNodePtr node;
xnode = rxml_get_xnode(self);
switch (xnode->type)
{
case XML_DOCUMENT_NODE:
#ifdef LIBXML_DOCB_ENABLED
case XML_DOCB_DOCUMENT_NODE:
#endif
case XML_HTML_DOCUMENT_NODE:
case XML_NAMESPACE_DECL:
node = NULL;
break;
case XML_ATTRIBUTE_NODE:
{
xmlAttrPtr attr = (xmlAttrPtr) xnode;
node = (xmlNodePtr) attr->prev;
}
break;
default:
node = xnode->prev;
break;
}
if (node == NULL)
return (Qnil);
else
return (rxml_node_wrap(node));
}
Adds the specified node as the previous sibling of the current node. If the node already exists in the document, it is first removed from its existing context. Any adjacent text nodes will be merged together, meaning the returned node may be different than the original node.
static VALUE rxml_node_prev_set(VALUE self, VALUE prev)
{
return rxml_node_modify_dom(self, prev, xmlAddPrevSibling);
}
Determines whether this node has a previous node
# File lib/libxml/node.rb, line 141 141: def prev? 142: not prev.nil? 143: end
# File lib/libxml/properties.rb, line 11 11: def properties 12: warn('Node#properties is deprecated. Use Node#attributes instead.') 13: self.attributes 14: end
# File lib/libxml/properties.rb, line 16 16: def properties? 17: warn('Node#properties? is deprecated. Use Node#attributes? instead.') 18: self.attributes? 19: end
# File lib/libxml/properties.rb, line 6 6: def property(name) 7: warn('Node#properties is deprecated. Use Node#[] instead.') 8: self[name] 9: end
Removes this node and its children from the document tree by setting its document, parent and siblings to nil. You can add the returned node back into a document. Otherwise, the node will be freed once any references to it go out of scope.
static VALUE rxml_node_remove_ex(VALUE self)
{
xmlNodePtr xnode, xresult;
xnode = rxml_get_xnode(self);
/* First unlink the node from its parent. */
xmlUnlinkNode(xnode);
/* Now copy the node we want to remove and make the
current Ruby object point to it. We do this because
a node has a number of dependencies on its parent
document - its name (if using a dictionary), entities,
namespaces, etc. For a node to live on its own, it
needs to get its own copies of this information.*/
xresult = xmlDocCopyNode(xnode, NULL, 1);
/* Now free the original node. */
xmlFreeNode(xnode);
/* Now wrap the new node */
RDATA(self)->data = xresult;
xresult->_private = (void*) self;
/* Now return the removed node so the user can
do something with it.*/
return self;
}
Adds the specified node as the end of the current node’s list of siblings. If the node already exists in the document, it is first removed from its existing context. Any adjacent text nodes will be merged together, meaning the returned node may be different than the original node.
static VALUE rxml_node_sibling_set(VALUE self, VALUE sibling)
{
return rxml_node_modify_dom(self, sibling, xmlAddSibling);
}
Determine whether this node preserves whitespace.
static VALUE rxml_node_space_preserve_get(VALUE self)
{
xmlNodePtr xnode;
xnode = rxml_get_xnode(self);
return (INT2NUM(xmlNodeGetSpacePreserve(xnode)));
}
Control whether this node preserves whitespace.
static VALUE rxml_node_space_preserve_set(VALUE self, VALUE bool)
{
xmlNodePtr xnode;
xnode = rxml_get_xnode(self);
if (TYPE(bool) == T_FALSE)
xmlNodeSetSpacePreserve(xnode, 0);
else
xmlNodeSetSpacePreserve(xnode, 1);
return (Qnil);
}
Specifies if this is a text node
# File lib/libxml/node.rb, line 293 293: def text? 294: node_type == TEXT_NODE 295: end
node.to_s(:indent => true, :encoding => 'UTF-8', :level => 0) → "string"
Converts a node, and all of its children, to a string representation. To include only the node’s children, use the the XML::Node#inner_xml method.
You may provide an optional hash table to control how the string is generated. Valid options are:
:indent - Specifies if the string should be indented. The default value is true. Note that indentation is only added if both :indent is true and XML.indent_tree_output is true. If :indent is set to false, then both indentation and line feeds are removed from the result.
:level - Specifies the indentation level. The amount of indentation is equal to the (level * number_spaces) + number_spaces, where libxml defaults the number of spaces to 2. Thus a level of 0 results in 2 spaces, level 1 results in 4 spaces, level 2 results in 6 spaces, etc.
:encoding - Specifies the output encoding of the string. It defaults to XML::Encoding::UTF8. To change it, use one of the XML::Encoding encoding constants.
static VALUE rxml_node_to_s(int argc, VALUE *argv, VALUE self)
{
VALUE result = Qnil;
VALUE options = Qnil;
xmlNodePtr xnode;
xmlCharEncodingHandlerPtr encodingHandler;
xmlOutputBufferPtr output;
int level = 0;
int indent = 1;
const char *xencoding = "UTF-8";
rb_scan_args(argc, argv, "01", &options);
if (!NIL_P(options))
{
VALUE rencoding, rindent, rlevel;
Check_Type(options, T_HASH);
rencoding = rb_hash_aref(options, ID2SYM(rb_intern("encoding")));
rindent = rb_hash_aref(options, ID2SYM(rb_intern("indent")));
rlevel = rb_hash_aref(options, ID2SYM(rb_intern("level")));
if (rindent == Qfalse)
indent = 0;
if (rlevel != Qnil)
level = NUM2INT(rlevel);
if (rencoding != Qnil)
{
xencoding = xmlGetCharEncodingName((xmlCharEncoding)NUM2INT(rencoding));
if (!xencoding)
rb_raise(rb_eArgError, "Unknown encoding value: %d", NUM2INT(rencoding));
}
}
encodingHandler = xmlFindCharEncodingHandler(xencoding);
output = xmlAllocOutputBuffer(encodingHandler);
xnode = rxml_get_xnode(self);
xmlNodeDumpOutput(output, xnode->doc, xnode, level, indent, xencoding);
xmlOutputBufferFlush(output);
if (output->conv)
result = rxml_str_new2((const char*) output->conv->content, xencoding);
else
result = rxml_str_new2((const char*) output->buffer->content, xencoding);
xmlOutputBufferClose(output);
return result;
}
Specifies if this is an xinclude end node
# File lib/libxml/node.rb, line 298 298: def xinclude_end? 299: node_type == XINCLUDE_END 300: end
Specifies if this is an xinclude start node
# File lib/libxml/node.rb, line 303 303: def xinclude_start? 304: node_type == XINCLUDE_START 305: end
Determine whether this node is an xlink node.
static VALUE rxml_node_xlink_q(VALUE self)
{
xmlNodePtr xnode;
xlinkType xlt;
xnode = rxml_get_xnode(self);
xlt = xlinkIsLink(xnode->doc, xnode);
if (xlt == XLINK_TYPE_NONE)
return (Qfalse);
else
return (Qtrue);
}
Obtain the type identifier for this xlink, if applicable. If this is not an xlink node (see xlink?), will return nil.
static VALUE rxml_node_xlink_type(VALUE self)
{
xmlNodePtr xnode;
xlinkType xlt;
xnode = rxml_get_xnode(self);
xlt = xlinkIsLink(xnode->doc, xnode);
if (xlt == XLINK_TYPE_NONE)
return (Qnil);
else
return (INT2NUM(xlt));
}
Obtain the type name for this xlink, if applicable. If this is not an xlink node (see xlink?), will return nil.
static VALUE rxml_node_xlink_type_name(VALUE self)
{
xmlNodePtr xnode;
xlinkType xlt;
xnode = rxml_get_xnode(self);
xlt = xlinkIsLink(xnode->doc, xnode);
switch (xlt)
{
case XLINK_TYPE_NONE:
return (Qnil);
case XLINK_TYPE_SIMPLE:
return (rxml_str_new2("simple", xnode->doc ? xnode->doc->encoding : NULL));
case XLINK_TYPE_EXTENDED:
return (rxml_str_new2("extended", xnode->doc ? xnode->doc->encoding : NULL));
case XLINK_TYPE_EXTENDED_SET:
return (rxml_str_new2("extended_set", xnode->doc ? xnode->doc->encoding : NULL));
default:
rb_fatal("Unknowng xlink type, %d", xlt);
}
}