Class: XML::DOM::Element
- Defined in:
- lib/xml/dom/core.rb,
lib/xml/dom/digest.rb,
lib/xml/dom2/xpath.rb,
lib/xml/dom2/element.rb
Overview
Class XML::DOM::Element
superclass
Node
Constant Summary
Constants inherited from Node
Node::ATTRIBUTE_NODE, Node::CDATA_SECTION_NODE, Node::COMMENT_NODE, Node::DOCUMENT_FRAGMENT_NODE, Node::DOCUMENT_NODE, Node::DOCUMENT_TYPE_NODE, Node::ELEMENT_NODE, Node::ENTITY_NODE, Node::ENTITY_REFERENCE_NODE, Node::NODE_NODE, Node::NOTATION_NODE, Node::PROCESSING_INSTRUCTION_NODE, Node::TEXT_NODE
Instance Method Summary collapse
- #_checkNode(node) ⇒ Object
-
#_getIDVals(ids = nil) ⇒ Object
get the list of nodeValues by IDs [experimental implement].
- #_getMyLocation(parent) ⇒ Object
- #_getMyLocationInXPath(parent) ⇒ Object
- #_getNamespaces(parentNamespaces = {}, all = false) ⇒ Object
-
#attributes ⇒ Object
[DOM].
-
#cloneNode(deep = true) ⇒ Object
[DOM].
-
#dump(depth = 0) ⇒ Object
— Element#dump(depth = 0).
-
#getAttribute(name) ⇒ Object
[DOM].
-
#getAttributeNode(name) ⇒ Object
[DOM].
-
#getAttributeNodeNS(nsuri, localname) ⇒ Object
[DOM2].
-
#getAttributeNS(nsuri, localname) ⇒ Object
[DOM2].
- #getDigest(algorithm = Digest::MD5, force = false) ⇒ Object
-
#getElementsByTagName(tagname) ⇒ Object
- DOM
-
(but this is not “live”).
-
#getElementsByTagNameNS(nsuri, localname) ⇒ Object
[DOM2].
-
#hasAttribute(name) ⇒ Object
[DOM2].
-
#hasAttributeNS(nsuri, localname) ⇒ Object
[DOM2].
-
#hasAttributes ⇒ Object
[DOM2].
- #idAttribute ⇒ Object
- #idAttribute=(name) ⇒ Object
-
#initialize(tag = nil, attr = nil, *children) ⇒ Element
constructor
new(tag, attrs, [child1, child2, …]) or new(tag, attrs, child1, child2, …) tag: String attrs: Hash, Attr or Array of Attr (or nil) child?: String or Node.
-
#localname ⇒ Object
[DOM2].
-
#namespaceURI ⇒ Object
[DOM2].
-
#nodeName ⇒ Object
(also: #tagName)
[DOM].
-
#nodeType ⇒ Object
[DOM].
-
#normalize ⇒ Object
[DOM].
-
#prefix ⇒ Object
[DOM2].
-
#prefix=(prefix) ⇒ Object
[DOM2].
-
#removeAttribute(name) ⇒ Object
[DOM].
-
#removeAttributeNode(oldAttr) ⇒ Object
[DOM].
-
#removeAttributeNS(nsuri, localname) ⇒ Object
[DOM2].
-
#setAttribute(name, value) ⇒ Object
[DOM].
-
#setAttributeNode(newAttr) ⇒ Object
[DOM].
-
#setAttributeNodeNS(newAttr) ⇒ Object
[DOM2].
-
#setAttributeNS(nsuri, qname, value) ⇒ Object
[DOM2].
-
#to_s ⇒ Object
— Element#to_s().
-
#trim(preserve = false) ⇒ Object
trim extra whitespaces if attribute ‘xml:space’ is ‘preserve’, don’t trim any white spaces.
Methods inherited from Node
#+, #<=>, #==, #[], #[]=, #__collectAncestorNS, #__collectDescendatNS, #__sibling, #_ancestor, #_child, #_descendant, #_following, #_fsibling, #_getChildIndex, #_getNodeByAbsoluteLocationTerm, #_insertNodes, #_matchAttribute?, #_matchNode?, #_matchNodeAttributes?, #_matchNodeType?, #_nodesByLocationTerms, #_nodesByRelativeLocationTerm, #_preceding, #_psibling, #_removeFromTree, #_removeNode, #_searchID, #accept, #accept_name, #appendChild, #childNodes, #childNodes=, #children_accept, #children_accept_name, #each, #firstChild, #getNodesByXPath, #getNodesByXPointer, #hasChildNodes, #insertBefore, #inspect, #isSupported, #lastChild, #makeXPath, #makeXPointer, #nextSibling, #nodeValue, #nodeValue=, #ownerDocument, #ownerDocument=, #parentNode, #parentNode=, #previousSibling, #removeChild, #replaceChild
Constructor Details
#initialize(tag = nil, attr = nil, *children) ⇒ Element
new(tag, attrs, [child1, child2, …]) or new(tag, attrs, child1, child2, …)
tag: String
attrs: Hash, Attr or Array of Attr (or nil)
child?: String or Node
1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 |
# File 'lib/xml/dom/core.rb', line 1941 def initialize(tag = nil, attr = nil, *children) super(*children) raise "parameter error" if !tag @name = tag.freeze if attr.nil? @attr = NamedNodeMap.new([]) elsif attr.is_a?(Hash) nodes = [] attr.each do |key, value| nodes.push(Attr.new(key, value)) end @attr = NamedNodeMap.new(nodes) elsif attr.is_a?(Array) @attr = NamedNodeMap.new(attr) elsif attr.is_a?(Attr) @attr = NamedNodeMap.new([attr]) else raise "parameter error: #{attr}" end end |
Instance Method Details
#_checkNode(node) ⇒ Object
2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 |
# File 'lib/xml/dom/core.rb', line 2265 def _checkNode(node) unless node.nodeType == ELEMENT_NODE || node.nodeType == TEXT_NODE || node.nodeType == COMMENT_NODE || node.nodeType == PROCESSING_INSTRUCTION_NODE || node.nodeType == CDATA_SECTION_NODE || node.nodeType == ENTITY_REFERENCE_NODE raise DOMException.new(DOMException::HIERARCHY_REQUEST_ERR) end end |
#_getIDVals(ids = nil) ⇒ Object
get the list of nodeValues by IDs
- experimental implement
2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 |
# File 'lib/xml/dom/core.rb', line 2217 def _getIDVals(ids = nil) if ids.nil? doc = ownerDocument return [] if doc.nil? ids = doc._getIDAttrs end idelem = [] if !ids[nodeName].nil? return attributes._getValues(ids[nodeName]) elsif !ids['*'].nil? return attributes._getValues(ids['*']) end return [] end |
#_getMyLocation(parent) ⇒ Object
2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 |
# File 'lib/xml/dom/core.rb', line 2160 def _getMyLocation(parent) index = 1 parent.childNodes do |child| if child == self return "child(#{index},#{@name})" end if child.nodeType == ELEMENT_NODE && child.nodeName == @name index += 1 end end nil end |
#_getMyLocationInXPath(parent) ⇒ Object
327 328 329 330 331 332 333 |
# File 'lib/xml/dom2/xpath.rb', line 327 def _getMyLocationInXPath(parent) name = nodeName n = parent.childNodes.to_a.select { |i| i.nodeType == ELEMENT_NODE and i.nodeName == name }.index(self) "#{name}[#{n + 1}]" end |
#_getNamespaces(parentNamespaces = {}, all = false) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/xml/dom2/element.rb', line 121 def _getNamespaces(parentNamespaces = {}, all = false) if !parentNamespaces parentNamespaces = parentNode._getNamespaces(nil, true) end namespaces = {} attributes.each do |a| namespaces[a.prefix] = a.namespaceURI if a.prefix end if @localname namespaces[@prefix] = @uri end parentNamespaces.each do |prefix, uri| if all if !namespaces.include?(prefix) namespaces[prefix] = uri end else if namespaces[prefix] == parentNamespaces[prefix] namespaces.delete(prefix) end end end namespaces end |
#attributes ⇒ Object
- DOM
1993 1994 1995 1996 1997 1998 1999 2000 2001 |
# File 'lib/xml/dom/core.rb', line 1993 def attributes if iterator? @attr.each do |key, value| yield(value) end if @attr else @attr end end |
#cloneNode(deep = true) ⇒ Object
- DOM
2207 2208 2209 2210 2211 2212 2213 |
# File 'lib/xml/dom/core.rb', line 2207 def cloneNode(deep = true) attrs = [] @attr.each do |attr| attrs.push(attr.cloneNode(true)) end super(deep, @name, attrs) end |
#dump(depth = 0) ⇒ Object
— Element#dump(depth = 0)
dumps the Element.
2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 |
# File 'lib/xml/dom/core.rb', line 2028 def dump(depth = 0) attr = '' @attr.each do |a| ## self.attributes do |a| attr += a.to_s + ", " end if @attr attr.chop! attr.chop! print ' ' * depth * 2 print "#{@name}(#{attr})\n" @children.each do |child| child.dump(depth + 1) end if @children end |
#getAttribute(name) ⇒ Object
- DOM
2058 2059 2060 2061 2062 2063 2064 2065 |
# File 'lib/xml/dom/core.rb', line 2058 def getAttribute(name) attr = getAttributeNode(name) if attr.nil? '' else attr.nodeValue end end |
#getAttributeNode(name) ⇒ Object
- DOM
2104 2105 2106 |
# File 'lib/xml/dom/core.rb', line 2104 def getAttributeNode(name) @attr.getNamedItem(name) end |
#getAttributeNodeNS(nsuri, localname) ⇒ Object
- DOM2
476 477 478 479 480 481 482 |
# File 'lib/xml/dom2/element.rb', line 476 def getAttributeNodeNS(nsuri, localname) attributes.each do |attr| return attr if attr.namespaceURI == nsuri && attr.localname == localname end nil end |
#getAttributeNS(nsuri, localname) ⇒ Object
- DOM2
442 443 444 445 446 447 448 449 |
# File 'lib/xml/dom2/element.rb', line 442 def getAttributeNS(nsuri, localname) attr = getAttributeNodeNS(nsuri, localname) if attr.nil? "" else attr.nodeValue end end |
#getDigest(algorithm = Digest::MD5, force = false) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/xml/dom/digest.rb', line 76 def getDigest(algorithm = Digest::MD5, force = false) return @digest if (!force && @digest) attr = attributes children = childNodes attr_digests = "" children_digests = "" if attr attr_array = attr.sort {|a, b| DOM.tou16(a.nodeName) <=> DOM.tou16(b.nodeName)} attr_array.each {|a| attr_digests << a.getDigest(algorithm, force) } end children_num = 0 children.each {|c| next if c.nodeType == COMMENT_NODE children_num += 1 children_digests << c.getDigest(algorithm, force) } @digest = algorithm.digest([ELEMENT_NODE].pack("N") + DOM.tou16(nodeName) + "\0\0" + [attr.length].pack("N") + attr_digests + [children_num].pack("N") + children_digests) end |
#getElementsByTagName(tagname) ⇒ Object
- DOM
-
(but this is not “live”)
2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 |
# File 'lib/xml/dom/core.rb', line 2147 def getElementsByTagName(tagname) ret = NodeList.new @children.each do |node| if node.nodeType == ELEMENT_NODE if tagname == '*' || node.nodeName == tagname ret << node end ret << node.getElementsByTagName(tagname) end end if @children ret end |
#getElementsByTagNameNS(nsuri, localname) ⇒ Object
- DOM2
493 494 495 496 497 498 499 500 501 502 503 504 505 |
# File 'lib/xml/dom2/element.rb', line 493 def getElementsByTagNameNS(nsuri, localname) ret = NodeList.new @children.each do |node| if node.nodeType == ELEMENT_NODE if (localname == '*' || node.localname == localname) and (nsuri == '*' || node.namespaceURI == nsuri) ret << node end ret << node.getElementsByTagNameNS(nsuri, localname) end end if @children ret end |
#hasAttribute(name) ⇒ Object
- DOM2
508 509 510 |
# File 'lib/xml/dom2/element.rb', line 508 def hasAttribute(name) !getAttributeNode(name).nil? end |
#hasAttributeNS(nsuri, localname) ⇒ Object
- DOM2
513 514 515 |
# File 'lib/xml/dom2/element.rb', line 513 def hasAttributeNS(nsuri, localname) !getAttributeNodeNS(nsuri, localname).nil? end |
#hasAttributes ⇒ Object
- DOM2
437 438 439 |
# File 'lib/xml/dom2/element.rb', line 437 def hasAttributes() attributes.length > 0 end |
#idAttribute ⇒ Object
517 |
# File 'lib/xml/dom2/element.rb', line 517 def idAttribute; @idAttribute; end |
#idAttribute=(name) ⇒ Object
518 |
# File 'lib/xml/dom2/element.rb', line 518 def idAttribute=(name); @idAttribute = name; end |
#localname ⇒ Object
- DOM2
434 |
# File 'lib/xml/dom2/element.rb', line 434 def localname; @localname; end |
#namespaceURI ⇒ Object
- DOM2
419 |
# File 'lib/xml/dom2/element.rb', line 419 def namespaceURI; @uri; end |
#nodeName ⇒ Object Also known as: tagName
- DOM
1982 1983 1984 |
# File 'lib/xml/dom/core.rb', line 1982 def nodeName @name end |
#nodeType ⇒ Object
- DOM
1971 1972 1973 |
# File 'lib/xml/dom/core.rb', line 1971 def nodeType ELEMENT_NODE end |
#normalize ⇒ Object
- DOM
2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 |
# File 'lib/xml/dom/core.rb', line 2182 def normalize return if @children.nil? old = nil children = @children.to_a.dup children.each do |child| if !old.nil? && old.nodeType == TEXT_NODE && child.nodeType == TEXT_NODE old.appendData(child.nodeValue) self.removeChild(child) else if child.nodeType == ELEMENT_NODE child.normalize end old = child end end end |
#prefix ⇒ Object
- DOM2
422 |
# File 'lib/xml/dom2/element.rb', line 422 def prefix; @prefix; end |
#prefix=(prefix) ⇒ Object
- DOM2
425 426 427 428 429 430 431 |
# File 'lib/xml/dom2/element.rb', line 425 def prefix=(prefix); ## to be checked @prefix = prefix @name = @prefix + ':' + @localname @prefix.freeze @name.freeze end |
#removeAttribute(name) ⇒ Object
- DOM
2092 2093 2094 2095 |
# File 'lib/xml/dom/core.rb', line 2092 def removeAttribute(name) ret = getAttributeNode(name) removeAttributeNode(ret) if ret end |
#removeAttributeNode(oldAttr) ⇒ Object
- DOM
2131 2132 2133 2134 2135 2136 2137 2138 |
# File 'lib/xml/dom/core.rb', line 2131 def removeAttributeNode(oldAttr) ret = getAttributeNode(oldAttr.nodeName) if ret.nil? || ret != oldAttr raise DOMException.new(DOMException::NOT_FOUND_ERR) end @attr.removeNamedItem(oldAttr.nodeName) ret end |
#removeAttributeNS(nsuri, localname) ⇒ Object
- DOM2
470 471 472 473 |
# File 'lib/xml/dom2/element.rb', line 470 def removeAttributeNS(nsuri, localname) ret = getAttributeNodeNS(nsuri, localname) removeAttributeNode(ret) if ret end |
#setAttribute(name, value) ⇒ Object
- DOM
2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 |
# File 'lib/xml/dom/core.rb', line 2074 def setAttribute(name, value) if @ownerDocument attr = @ownerDocument.createAttribute(name) attr.appendChild(@ownerDocument.createTextNode(value)) else attr = Attribute.new(name) attr.appendChild(Text.new(value)) end setAttributeNode(attr) end |
#setAttributeNode(newAttr) ⇒ Object
- DOM
2115 2116 2117 2118 2119 2120 2121 2122 |
# File 'lib/xml/dom/core.rb', line 2115 def setAttributeNode(newAttr) ret = getAttributeNode(newAttr.nodeName) if ret == newAttr raise DOMException.new(DOMException::INUSE_ATTRIBUTE_ERR) end @attr.setNamedItem(newAttr) ret end |
#setAttributeNodeNS(newAttr) ⇒ Object
- DOM2
485 486 487 488 489 490 |
# File 'lib/xml/dom2/element.rb', line 485 def setAttributeNodeNS(newAttr) ret = getAttributeNodeNS(newAttr.namespaceURI, newAttr.localname) removeAttributeNode(ret) if ret setAttributeNode(newAttr) ret end |
#setAttributeNS(nsuri, qname, value) ⇒ Object
- DOM2
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 |
# File 'lib/xml/dom2/element.rb', line 452 def setAttributeNS(nsuri, qname, value) if qname.index(':') prefix, localname = qname.split(':') raise DOMException.new(DOMException::NAMESPACE_ERR) if nsuri.nil? or (prefix == 'xml' and nsuri != 'http://www.w3.org/XML/1998/namespace') else raise DOMException.new(DOMException::NAMESPACE_ERR) if qname == 'xmlns' and nsuri != 'http://www.w3.org/2000/xmlns/' end attr = @ownerDocument.createAttributeNS(nsuri, qname) attr.appendChild(@ownerDocument.createTextNode(value)) setAttributeNodeNS(attr) end |
#to_s ⇒ Object
— Element#to_s()
return the string representation of the Element.
2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 |
# File 'lib/xml/dom/core.rb', line 2008 def to_s attr = '' @attr.each do |a| attr << ' ' + a.to_s end if @attr content = super if content != '' ret = "<#{@name}#{attr}>#{content}</#{@name}>" else ret = "<#{@name}#{attr}/>" end ret << "\n" if parentNode.nodeType == DOCUMENT_NODE ret end |
#trim(preserve = false) ⇒ Object
trim extra whitespaces if attribute ‘xml:space’ is ‘preserve’, don’t trim any white spaces
2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 |
# File 'lib/xml/dom/core.rb', line 2241 def trim(preserve = false) if !attributes['xml:space'].nil? value = attributes['xml:space'].nodeValue if value == 'preserve' preserve = true elsif value == 'default' preserve = false end end return nil if @children.nil? children = @children.to_a.dup children.each do |child| if !preserve && (child.nodeType == TEXT_NODE || child.nodeType == CDATA_SECTION_NODE) if child.trim == "" self.removeChild(child) end else child.trim(preserve) end end nil end |