0

Is it possible to get XML snippets using XPATH in javascript (evaluate function, Chrome)?

How to do it?

Keep getting errors in every approach I've used.

2
  • Can you tell us in which enviroment you want to use Javascript and XPath? As you mention evaluate, for Mozilla browsers see developer.mozilla.org/en-US/docs/… and developer.mozilla.org/en-US/docs/Web/API/document.evaluate and post details of code used and errors you get in case the documentation does not help. Commented Nov 5, 2014 at 13:33
  • @MartinHonnen Hey Martin, thanks for a reply, i am targeting chrome. it seems to do something here, but it returns error when I am trying to run examples from Mozilla docu. Commented Nov 5, 2014 at 13:36

1 Answer 1

1

Here is an example:

var xmlDoc = new DOMParser().parseFromString('<root><foo bar="baz">whatever</foo></root>', 'application/xml');
var xpathResult = xmlDoc.evaluate('//foo[@bar = "baz"]', xmlDoc, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
while ((el = xpathResult.iterateNext()) != null) {
  alert(el.textContent);
}

Works fine for me with Firefox and Chrome.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.