Created
April 15, 2014 18:38
-
-
Save robbypelssers/10757404 to your computer and use it in GitHub Desktop.
Javascript Xpath Demo: xpath.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| 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