new(tag, attrs, child1, child2, …)
tag: String attrs: Hash, Attr or Array of Attr (or nil) child?: String or Node
# 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
# 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
get the list of nodeValues by IDs
# 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
# 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
# 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
# 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
# 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
# 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
# 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
# File lib/xml/dom/core.rb, line 2058 def getAttribute(name) attr = getAttributeNode(name) if attr.nil? '' else attr.nodeValue end end
# File lib/xml/dom2/element.rb, line 442 def getAttributeNS(nsuri, localname) attr = getAttributeNodeNS(nsuri, localname) if attr.nil? "" else attr.nodeValue end end
# File lib/xml/dom/core.rb, line 2104 def getAttributeNode(name) @attr.getNamedItem(name) end
# 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
# 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) + "\00\\00"" + [attr.length].pack("N") + attr_digests + [children_num].pack("N") + children_digests) end
# 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
# 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
# File lib/xml/dom2/element.rb, line 508 def hasAttribute(name) !getAttributeNode(name).nil? end
# File lib/xml/dom2/element.rb, line 513 def hasAttributeNS(nsuri, localname) !getAttributeNodeNS(nsuri, localname).nil? end
# File lib/xml/dom2/element.rb, line 437 def hasAttributes() attributes.length > 0 end
# File lib/xml/dom2/element.rb, line 517 def idAttribute; @idAttribute; end
# File lib/xml/dom2/element.rb, line 518 def idAttribute=(name); @idAttribute = name; end
# File lib/xml/dom2/element.rb, line 434 def localname; @localname; end
# File lib/xml/dom2/element.rb, line 419 def namespaceURI; @uri; end
# File lib/xml/dom/core.rb, line 1982 def nodeName @name end
# File lib/xml/dom/core.rb, line 1971 def nodeType ELEMENT_NODE end
# 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
# File lib/xml/dom2/element.rb, line 422 def prefix; @prefix; end
# File lib/xml/dom2/element.rb, line 425 def prefix=(prefix); ## to be checked @prefix = prefix @name = @prefix + ':' + @localname @prefix.freeze @name.freeze end
# File lib/xml/dom/core.rb, line 2092 def removeAttribute(name) ret = getAttributeNode(name) removeAttributeNode(ret) if ret end
# File lib/xml/dom2/element.rb, line 470 def removeAttributeNS(nsuri, localname) ret = getAttributeNodeNS(nsuri, localname) removeAttributeNode(ret) if ret end
# 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
# 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
# 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
# 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
# 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
# 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
if attribute 'xml:space' is 'preserve', don't trim any white spaces
# 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