Skip to content

Instantly share code, notes, and snippets.

@robbypelssers
Created April 15, 2014 18:38
Show Gist options
  • Select an option

  • Save robbypelssers/10757404 to your computer and use it in GitHub Desktop.

Select an option

Save robbypelssers/10757404 to your computer and use it in GitHub Desktop.
Javascript Xpath Demo: xpath.js
/**
https://developer.mozilla.org/en-US/docs/Web/XPath
**/
requirejs([], function() {
//count the number of <li> tags
var listItemCount = document.evaluate( 'count(/html/body//li)', document, null, XPathResult.ANY_TYPE, null );
alert("There are " + listItemCount.numberValue + " <li> tags inside the <body> tag");
//output: There are 5 <li> tags inside the <body> tag
//stringjoin the values of the <li> nodes
var iterator = document.evaluate('/html/body//li', document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null );
var listItem;
var listItemValues = [];
while (listItem = iterator.iterateNext()) {
listItemValues.push(listItem.textContent);
}
alert("The languages listed are '" + listItemValues.join() + "'.");
//output: The languages listed are 'Javascript,Java,Scala,Ceylon,Python'.
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment