Skip to content

Instantly share code, notes, and snippets.

@nick1123
Created September 11, 2013 19:36
Show Gist options
  • Save nick1123/6528690 to your computer and use it in GitHub Desktop.
Save nick1123/6528690 to your computer and use it in GitHub Desktop.
Strange behavior using Nokogiri xpath
xml = '<?xml version="1.0" encoding="utf-8"?><mdx version="4.0" xmlns="responseSimple" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></mdx>'
parsed = Nokogiri.parse(xml)
parsed.xpath('/mdx').present?
# => false
xml = '<?xml version="1.0" encoding="utf-8"?><mdx></mdx>'
parsed = Nokogiri.parse(xml)
parsed.xpath('/mdx').present?
# => true
# Running jruby-1.7.2 on Ubuntu 12.04
@quixoten
Copy link

require 'nokogiri'

xml = '<?xml version="1.0" encoding="utf-8"?><mdx version="4.0" xmlns="responseSimple" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"></mdx>'
parsed = Nokogiri.parse(xml)
puts "present: #{parsed.xpath('/xmlns:mdx').to_s}"
# => false

xml = '<?xml version="1.0" encoding="utf-8"?><mdx></mdx>'
parsed = Nokogiri.parse(xml)
puts "present: #{parsed.xpath('/mdx').to_s}"
# => true

# Running jruby-1.7.2 on Ubuntu 12.04

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment