Sha256: 49ad59694fc6e41486e6ee218dfeb6ef92454b3fa0d97e933d9ef80b42b6a838

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

module ParseHelper
  def x(xpath,options = {})
    doc = options[:doc] || @doc
    preamble = options[:meta] == true ? metaxmlpathpreamble : xmlpathpreamble
    if doc.is_a?(Nokogiri::XML::Node)
      nodes = doc.xpath("#{preamble}#{xpath}")
    else
      nodes=REXML::XPath.match(doc,"#{preamble}#{xpath}")
    end
    if nodes.is_a? String
      return nodes
    end
    if nodes.length==1
      return node_value(nodes.first)
    elsif nodes.length>1
      return nodes.map{|x| node_value(x)}
    else
      return nil
    end
  end
  
  def node_value(node)
    if node.is_a?(Nokogiri::XML::Attr) || node.is_a?(REXML::Attribute)
      # Attributes are allowed to be an empty string
      return node.to_s
    elsif node.is_a?(Nokogiri::XML::Element) || node.is_a?(REXML::Element)
      return node.text == '' ? nil : node.text
    else
      return node.to_s
    end
  end

  def xmlpathpreamble
    ''
  end
  def load_xml_doc(xml)
    doc = Nokogiri.XML(xml) { |config| config.strict }
    doc.remove_namespaces!
    doc
  end
  private :x
  
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
amee-2.7.0 lib/amee/parse_helper.rb
amee-2.6.0 lib/amee/parse_helper.rb