From: Aaron Patterson Date: 2009-08-27T07:48:49+09:00 Subject: Re: Question about Nokogiri xpath Hi Venkat, On Thu, Aug 27, 2009 at 06:49:41AM +0900, Venkat Akkineni wrote: > I am trying to parse an xml file using nokogiri. If any body has > experience with nokogiri. I request you to help. > > Sample xml file. > > > > type="VARCHAR" size="32" autoIncrement="false"> > > > > > > It can be seen that Column element has two child elements. But when I > use > > input = Nokogiri::XML(File.new(file)) > input.xpath('//table/column').each do |column| > column.children.length > end > > > The result is 5. > > While the element is at index 1, element > is at index 5. Does any one know why this is. Your XML contains whitespace nodes. The whitespace nodes are maintained in the tree that Nokogiri produces. If you iterate over each node, you can see this in action: input.xpath('//table/column').each do |column| # Check each child for blankness column.children.each { |child| puts child.blank? } end If you do not care to maintain whitespace nodes, you can tell the parser to disable blank nodes like this: input = Nokogiri::XML(File.new(file)) { |cfg| cfg.noblanks } I hope that helps! If you have more questions, I encourage you to join the nokogiri mailing list which can be found here: http://groups.google.com/group/nokogiri-talk -- Aaron Patterson http://tenderlovemaking.com/