Class: Nokogiri::XML::Attr
| Relationships & Source Files | |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Class Chain:
self,
Node
|
|
|
Instance Chain:
|
|
| Inherits: |
Nokogiri::XML::Node
|
| Defined in: | ext/nokogiri/xml_attr.c, lib/nokogiri/xml/attr.rb |
Overview
Attr represents a Attr node in an xml document.
Constant Summary
PP::Node - Included
Searchable - Included
::Nokogiri::ClassResolver - Included
Node - Inherited
ATTRIBUTE_DECL, ATTRIBUTE_NODE, CDATA_SECTION_NODE, COMMENT_NODE, DECONSTRUCT_KEYS, DECONSTRUCT_METHODS, DOCB_DOCUMENT_NODE, DOCUMENT_FRAG_NODE, DOCUMENT_NODE, DOCUMENT_TYPE_NODE, DTD_NODE, ELEMENT_DECL, ELEMENT_NODE, ENTITY_DECL, ENTITY_NODE, ENTITY_REF_NODE, HTML_DOCUMENT_NODE, IMPLIED_XPATH_CONTEXTS, NAMESPACE_DECL, NOTATION_NODE, PI_NODE, TEXT_NODE, XINCLUDE_END, XINCLUDE_START
Class Method Summary
-
.new(document, name)
constructor
Create a new
Attrelement on thedocumentwithname
Node - Inherited
| .new | documented in lib/nokogiri/xml/node.rb. |
Instance Attribute Summary
-
#value=(content)
(also: #content=)
writeonly
Set the value for this
Attrtocontent.
Node - Inherited
| #blank? |
|
| #cdata? | Returns true if this is a |
| #children | :category: Traversing Document Structure. |
| #children= | Set the content for this |
| #comment? | Returns true if this is a |
| #content | [Returns]. |
| #content= | Set the content of this node to |
| #default_namespace= | Adds a default namespace supplied as a string |
| #document | :category: Traversing Document Structure. |
| #document? | Returns true if this is a |
| #elem? | Alias for Node#element?. |
| #element? | Returns true if this is an |
| #fragment? | Returns true if this is a |
| #html? | Returns true if this is an |
| #inner_html | Get the inner_html for this node’s Node#children |
| #inner_html= | Set the content for this |
| #inner_text | Alias for Node#content. |
| #lang | Searches the language of a node, i.e. |
| #lang= | Set the language of a node, i.e. |
| #line |
|
| #line= | Sets the line for this |
| #name | Alias for Node#node_name. |
| #namespace |
|
| #namespace= | Set the default namespace on this node (as would be defined with an “xmlns=” attribute in |
| #native_content= | Set the content of this node to |
| #next | Alias for Node#next_sibling. |
| #next= | Alias for Node#add_next_sibling. |
| #node_name | Returns the name for this |
| #node_name= | Set the name for this |
| #parent | |
| #parent= | |
| #previous | Alias for Node#previous_sibling. |
| #previous= | Alias for Node#add_previous_sibling. |
| #processing_instruction? | Returns true if this is a |
| #read_only? | Is this a read only node? |
| #text | Alias for Node#content. |
| #text? | Returns true if this is a |
| #to_str | Alias for Node#content. |
| #xml? | Returns true if this is an |
| #prepend_newline?, #data_ptr? | |
Instance Method Summary
-
#deconstruct_keys(array_of_names) → Hash)
Returns a hash describing the
Attr, to use in pattern matching. -
#to_s
Alias for Node#content.
-
#value
writeonly
Alias for Node#content.
- #inspect_attributes private
Node - Inherited
| #<< | Add |
| #<=> | |
| #== |
|
| #[] | Fetch an attribute from this node. |
| #[]= | Update the attribute |
| #accept | Accept a visitor. |
| #add_child | Appends specified Nodes to the children of |
| #add_class | Ensure HTML |
| #add_namespace | Alias for Node#add_namespace_definition. |
| #add_namespace_definition | :category: Manipulating Document Structure. |
| #add_next_sibling | Insert |
| #add_previous_sibling | Insert |
| #after | Insert |
| #ancestors | |
| #append_class | Add HTML |
| #attr | Alias for Node#[]. |
| #attribute | :category: Working With |
| #attribute_nodes | :category: Working With |
| #attribute_with_ns | :category: Working With |
| #attributes | Fetch this node’s attributes. |
| #before | Insert |
| #canonicalize, | |
| #child | :category: Traversing Document Structure. |
| #classes | Fetch CSS class names of a |
| #clone | Clone this node. |
| #create_external_subset | Create an external subset. |
| #create_internal_subset | Create the internal subset of a document. |
| #css_path | Get the path to this node as a |
| #deconstruct_keys | Returns a hash describing the |
| #decorate! | Decorate this node with the decorators set up in this node’s |
| #delete | Alias for Node#remove_attribute. |
| #description | Fetch the |
| #do_xinclude | Do xinclude substitution on the subtree below node. |
| #dup | Duplicate this node. |
| #each | Iterate over each attribute name and value pair for this |
| #element_children | [Returns]. |
| #elements | Alias for Node#element_children. |
| #encode_special_chars | Encode any special characters in |
| #external_subset | Get the external subset. |
| #first_element_child |
|
| #fragment | Create a |
| #get_attribute | Alias for Node#[]. |
| #has_attribute? | Alias for Node#key?. |
| #initialize | Create a new node with |
| #internal_subset | Get the internal subset. |
| #key? | Returns true if |
| #keys | Get the attribute names for this |
| #kwattr_add | Ensure that values are present in a keyword attribute. |
| #kwattr_append | Add keywords to a Node’s keyword attribute, regardless of duplication. |
| #kwattr_remove | Remove keywords from a keyword attribute. |
| #kwattr_values | Fetch values from a keyword attribute of a |
| #last_element_child |
|
| #matches? | Returns true if this |
| #namespace_definitions | [Returns]. |
| #namespace_scopes |
|
| #namespaced_key? | Returns true if |
| #namespaces | Fetch all the namespaces on this node and its ancestors. |
| #next_element | Returns the next |
| #next_sibling | Returns the next sibling node. |
| #node_type | Get the type for this |
| #parse | Parse |
| #path | Returns the path associated with this |
| #pointer_id | [Returns]. |
| #prepend_child | Add |
| #previous_element | Returns the previous |
| #previous_sibling | Returns the previous sibling node. |
| #remove | Alias for Node#unlink. |
| #remove_attribute | Remove the attribute named |
| #remove_class | Remove HTML |
| #replace | Replace this |
| #serialize | Serialize Node using |
| #set_attribute | Alias for Node#[]=. |
| #swap | Swap this |
| #to_html | Serialize this |
| #to_s | Turn this node in to a string. |
| #to_xhtml | Serialize this |
| #to_xml | Serialize this |
| #traverse | Yields all children to |
| #type | Alias for Node#node_type. |
| #unlink | Unlink this node from its current context. |
| #value? | Does this Node’s attributes include <value>. |
| #values | Get the attribute values for this |
| #wrap | Wrap this |
| #write_html_to | Write Node as |
| #write_to | Serialize this node or document to |
| #write_xhtml_to | Write Node as XHTML to |
| #write_xml_to | Write Node as |
| #add_child_node_and_reparent_attrs, #add_sibling, | |
| #compare | |
| #dump_html | Returns the |
| #get | Get the value for |
| #html_standard_serialize, | |
| #in_context | TODO: DOCUMENT ME. |
| #inspect_attributes, #keywordify, | |
| #native_write_to | Write this |
| #process_xincludes | Loads and substitutes all xinclude elements below the node. |
| #set | Set the |
| #set_namespace | Set the namespace to |
| #to_format, #write_format_to, #add_child_node, #add_next_sibling_node, #add_previous_sibling_node, #replace_node | |
::Nokogiri::ClassResolver - Included
| #related_class | Find a class constant within the. |
Searchable - Included
| #% | Alias for Searchable#at. |
| #/ | Alias for Searchable#search. |
| #> | Search this node’s immediate children using |
| #at | Search this object for |
| #at_css | Search this object for |
| #at_xpath | Search this node for |
| #css | Search this object for |
| #search | Search this object for |
| #xpath | Search this node for |
| #css_internal, #css_rules_to_xpath, #xpath_impl, #xpath_internal, #xpath_query_from_css_rule, #extract_params | |
PP::Node - Included
Constructor Details
.new(document, name)
Create a new Attr element on the document with name
# File 'ext/nokogiri/xml_attr.c', line 55
static VALUE
new (int argc, VALUE *argv, VALUE klass)
{
xmlDocPtr xml_doc;
VALUE document;
VALUE name;
VALUE rest;
xmlAttrPtr node;
VALUE rb_node;
rb_scan_args(argc, argv, "2*", &document, &name, &rest);
if (! rb_obj_is_kind_of(document, cNokogiriXmlDocument)) {
rb_raise(rb_eArgError, "parameter must be a Nokogiri::XML::Document");
}
xml_doc = noko_xml_document_unwrap(document);
node = xmlNewDocProp(
xml_doc,
(const xmlChar *)StringValueCStr(name),
NULL
);
noko_xml_document_pin_node((xmlNodePtr)node);
rb_node = noko_xml_node_wrap(klass, (xmlNodePtr)node);
rb_obj_call_init(rb_node, argc, argv);
if (rb_block_given_p()) {
rb_yield(rb_node);
}
return rb_node;
}
Instance Attribute Details
#value=(content) (writeonly) Also known as: #content=
Set the value for this Attr to content. Use nil to remove the value (e.g., a ::Nokogiri::HTML boolean attribute).
# File 'ext/nokogiri/xml_attr.c', line 12
static VALUE
set_value(VALUE self, VALUE content)
{
xmlAttrPtr attr;
xmlChar *value;
xmlNode *cur;
Noko_Node_Get_Struct(self, xmlAttr, attr);
if (attr->children) {
xmlFreeNodeList(attr->children);
}
attr->children = attr->last = NULL;
if (content == Qnil) {
return content;
}
value = xmlEncodeEntitiesReentrant(attr->doc, (unsigned char *)StringValueCStr(content));
if (xmlStrlen(value) == 0) {
attr->children = xmlNewDocText(attr->doc, value);
} else {
attr->children = xmlStringGetNodeList(attr->doc, value);
}
xmlFree(value);
for (cur = attr->children; cur; cur = cur->next) {
cur->parent = (xmlNode *)attr;
cur->doc = attr->doc;
if (cur->next == NULL) {
attr->last = cur;
}
}
return content;
}
Instance Method Details
#deconstruct_keys(array_of_names) → Hash)
Returns a hash describing the Attr, to use in pattern matching.
Valid keys and their values:
-
name→ (String) The name of the attribute. -
#value → (String) The value of the attribute.
-
namespace→ (Namespace, nil) The Namespace of the attribute, ornilif there is no 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"/>
</root>
XML
attributes = doc.root.elements.first.attribute_nodes
# => [#(Attr:0x35c { name = "foo", value = "abc" }),
# #(Attr:0x370 {
# name = "bar",
# namespace = #(Namespace:0x384 {
# prefix = "noko",
# href = "http://nokogiri.org/ns/noko"
# }),
# value = "def"
# })]
attributes.first.deconstruct_keys([:name, :value, :namespace])
# => {:name=>"foo", :value=>"abc", :namespace=>nil}
attributes.last.deconstruct_keys([:name, :value, :namespace])
# => {:name=>"bar",
# :value=>"def",
# :namespace=>
# #(Namespace:0x384 {
# prefix = "noko",
# href = "http://nokogiri.org/ns/noko"
# })}
Since v1.14.0
# File 'lib/nokogiri/xml/attr.rb', line 55
def deconstruct_keys(keys) { name: name, value: value, namespace: namespace } end
#inspect_attributes (private)
[ GitHub ]# File 'lib/nokogiri/xml/attr.rb', line 61
def inspect_attributes [:name, :namespace, :value] end
#to_s
Alias for Node#content.
# File 'lib/nokogiri/xml/attr.rb', line 8
alias_method :to_s, :content
#value (writeonly)
Alias for Node#content.
# File 'lib/nokogiri/xml/attr.rb', line 7
alias_method :value, :content