Module | FeedTools::XmlHelper |
In: |
lib/feed_tools/helpers/xml_helper.rb
|
Generic xml methods needed in numerous places throughout FeedTools
Runs through a list of XPath queries on an element or document and returns all non-empty results, appending the results from each query onto the end of the results from the previous queries.
# File lib/feed_tools/helpers/xml_helper.rb, line 181 181: def self.combine_xpaths_all(element, xpath_list, options={}) 182: FeedTools::GenericHelper.validate_options([ :select_result_value ], 183: options.keys) 184: options = { :select_result_value => false }.merge(options) 185: 186: all_results = [] 187: result = [] 188: if element.nil? 189: return [] 190: end 191: for xpath in xpath_list 192: # Namespace aware 193: results = REXML::XPath.liberal_match(element, xpath, 194: FEED_TOOLS_NAMESPACES) 195: if options[:select_result_value] && !results.nil? && !results.empty? 196: results = 197: results.map { |x| x.respond_to?(:value) ? x.value : x.to_s } 198: end 199: if results.blank? 200: results = REXML::XPath.liberal_match(element, xpath) 201: else 202: all_results.concat(results) 203: next 204: end 205: 206: # Namespace unaware 207: if options[:select_result_value] && !results.nil? && !results.empty? 208: results = 209: results.map { |x| x.respond_to?(:value) ? x.value : x.to_s } 210: end 211: if !results.blank? 212: all_results.concat(results) 213: next 214: end 215: end 216: for xpath in xpath_list 217: if xpath =~ /^\w+$/ 218: results = [] 219: for child in element.children 220: if child.class == REXML::Element 221: if child.name.downcase == xpath.downcase 222: results << child 223: end 224: end 225: end 226: if options[:select_result_value] && !results.nil? && !results.empty? 227: results = 228: results.map { |x| x.inner_xml } 229: end 230: if !results.blank? 231: all_results.concat(results) 232: next 233: end 234: end 235: end 236: return all_results.uniq 237: end
Selects the first non-blank result.
# File lib/feed_tools/helpers/xml_helper.rb, line 32 32: def self.select_not_blank(results, &block) 33: if results.kind_of? Array 34: for result in results 35: blank_result = false 36: if !block.nil? 37: blank_result = block.call(result) 38: else 39: blank_result = result.to_s.blank? 40: end 41: unless result.nil? || blank_result 42: return result 43: end 44: end 45: else 46: blank_result = false 47: if !block.nil? 48: blank_result = block.call(results) 49: else 50: blank_result = results.to_s.blank? 51: end 52: unless results.nil? || blank_result 53: return results 54: end 55: end 56: return nil 57: end
Runs through a list of XPath queries on an element or document and returns the first non-blank result. Subsequent XPath queries will not be evaluated.
# File lib/feed_tools/helpers/xml_helper.rb, line 62 62: def self.try_xpaths(element, xpath_list, 63: options={}, &block) 64: FeedTools::GenericHelper.validate_options([ :select_result_value ], 65: options.keys) 66: options = { :select_result_value => false }.merge(options) 67: 68: result = nil 69: if element.nil? 70: return nil 71: end 72: for xpath in xpath_list 73: # Namespace aware 74: result = REXML::XPath.liberal_first(element, xpath, 75: FEED_TOOLS_NAMESPACES) 76: if options[:select_result_value] && !result.nil? 77: if result.respond_to?(:value) 78: result = result.value 79: else 80: result = result.to_s 81: end 82: end 83: blank_result = false 84: if block_given? 85: blank_result = yield(result) 86: else 87: blank_result = result.to_s.blank? 88: end 89: if !blank_result 90: if result.respond_to? :strip 91: result.strip! 92: end 93: return result 94: end 95: 96: # Namespace unaware 97: result = REXML::XPath.liberal_first(element, xpath) 98: if options[:select_result_value] && !result.nil? 99: if result.respond_to?(:value) 100: result = result.value 101: else 102: result = result.to_s 103: end 104: end 105: blank_result = false 106: if block_given? 107: blank_result = yield(result) 108: else 109: blank_result = result.to_s.blank? 110: end 111: if !blank_result 112: if result.respond_to? :strip 113: result.strip! 114: end 115: return result 116: end 117: end 118: return nil 119: end
Runs through a list of XPath queries on an element or document and returns all non-empty results. Subsequent XPath queries will not be evaluated.
# File lib/feed_tools/helpers/xml_helper.rb, line 124 124: def self.try_xpaths_all(element, xpath_list, options={}) 125: FeedTools::GenericHelper.validate_options([ :select_result_value ], 126: options.keys) 127: options = { :select_result_value => false }.merge(options) 128: 129: results = [] 130: if element.nil? 131: return [] 132: end 133: for xpath in xpath_list 134: # Namespace aware 135: results = REXML::XPath.liberal_match(element, xpath, 136: FEED_TOOLS_NAMESPACES) 137: if options[:select_result_value] && !results.nil? && !results.empty? 138: results = 139: results.map { |x| x.respond_to?(:value) ? x.value : x.to_s } 140: end 141: if results.blank? 142: results = REXML::XPath.liberal_match(element, xpath) 143: else 144: return results 145: end 146: 147: # Namespace unaware 148: if options[:select_result_value] && !results.nil? && !results.empty? 149: results = 150: results.map { |x| x.respond_to?(:value) ? x.value : x.to_s } 151: end 152: if !results.blank? 153: return results 154: end 155: end 156: for xpath in xpath_list 157: if xpath =~ /^\w+$/ 158: results = [] 159: for child in element.children 160: if child.class == REXML::Element 161: if child.name.downcase == xpath.downcase 162: results << child 163: end 164: end 165: end 166: if options[:select_result_value] && !results.nil? && !results.empty? 167: results = 168: results.map { |x| x.inner_xml } 169: end 170: if !results.blank? 171: return results 172: end 173: end 174: end 175: return [] 176: end