Method: Scrubyt::XPathUtils.generate_XPath
- Defined in:
- lib/scrubyt/utils/xpathutils.rb
.generate_XPath(node, stopnode = nil, write_indices = false) ⇒ Object
Generate XPath for the given node
parameters
node - The node we are looking up the XPath for
stopnode - The Xpath generation is stopped and the XPath that was generated so far is returned if this node is reached.
write_indices - whether the index inside the parent shuold be added, as in html/body/table/tr/td
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/scrubyt/utils/xpathutils.rb', line 35 def self.generate_XPath(node, stopnode=nil, write_indices=false) path = [] indices = [] found = false while !node.nil? && node.class != Hpricot::Doc do if node == stopnode found = true break end path.push node.name indices.push find_index(node) if write_indices node = node.parent end #This condition ensures that if there is a stopnode, and we did not found it along the way, #we return nil (since the stopnode is not contained in the path at all) return nil if stopnode != nil && !found result = "" if write_indices path.reverse.zip(indices.reverse).each { |node,index| result += "#{node}[#{index}]/" } else path.reverse.each{ |node| result += "#{node}/" } end "/" + result.chop end |