Skip to content

Instantly share code, notes, and snippets.

@andrewbigger
Created July 25, 2014 02:05
Show Gist options
  • Save andrewbigger/33e628f1bb143e271f9c to your computer and use it in GitHub Desktop.
Save andrewbigger/33e628f1bb143e271f9c to your computer and use it in GitHub Desktop.
Detect Nested CXML P or C Nodes
#!/usr/bin/env ruby
require 'rubygems'
require 'optparse'
require 'nokogiri'
options = { :log => $stdout,
:verbose => false
}
opt = OptionParser.new do |opt|
opt.banner = "Usage: nested_tag_detect [options] <xml>"
opt.on('--log-file=log file') { |log| options[:log] = log }
opt.on('--v','--verbose') { options[:verbose] = true }
opt.on_tail('-h', '--help') do
puts opt
exit
end
end
opt.parse!(ARGV)
unless ARGV.size == 1
puts opt
exit
end
target_file = ARGV[0]
puts "Checking: #{target_file}"
fpr = File.open(target_file.to_s, "r")
doc = Nokogiri::XML(fpr)
doc.css('p, c').each do |node|
if node.parent.name == node.name
puts "Nested node detected at line #{node.line}"
end
end
fpr.close
puts "Done"
puts " "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment