Skip to content

Instantly share code, notes, and snippets.

@joshski
Created January 1, 2014 13:06
Show Gist options
  • Save joshski/8207912 to your computer and use it in GitHub Desktop.
Save joshski/8207912 to your computer and use it in GitHub Desktop.
bug in npm 'xpath'?
require 'nokogiri'
Nokogiri::HTML('<z><a /><b /><a /><b /><b /><b /><a /><c><a /><b /></c></z>').css("a + b")
=> [#<Nokogiri::XML::Element:0x3fdf7d0c0a34 name="b">, #<Nokogiri::XML::Element:0x3fdf7d0c0688 name="b">, #<Nokogiri::XML::Element:0x3fdf7d0c046c name="b">]
Nokogiri::HTML('<z><a /><b /><a /><b /><b /><b /><a /><c><a /><b /></c></z>').xpath("//a/following-sibling::*[1]/self::b")
=> [#<Nokogiri::XML::Element:0x3fdf7d0cc294 name="b">, #<Nokogiri::XML::Element:0x3fdf7d0cc08c name="b">, #<Nokogiri::XML::Element:0x3fdf7d0cbe70 name="b">]
$('<z><a /><b /><a /><b /><b /><b /><a /><c><a /><b /></c></z>').find('a + b');
Object[b, b, b]
var xpath = require('xpath');
var dom = require('xmldom').DOMParser;
var doc = new dom().parseFromString('<z><a /><b /><a /><b /><b /><b /><a /><c><a /><b /></c></z>');
var elements = xpath.select("//a/following-sibling::*[1]/self::b", doc);
console.log(elements.length);
// => prints '1'
var libxmljs = require("libxmljs");
var doc = libxmljs.parseXml('<z><a /><b /><a /><b /><b /><b /><a /><c><a /><b /></c></z>');
var elements = doc.find("//a/following-sibling::*[1]/self::b");
console.log(elements.length);
// => prints '3'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment