How to use variables in document.evaluate()

S

shellon

Hi all:
I met a problem when using document.evaluate() to get text content
using XPath,
my code is as follows:

nodes = document.evaluate("/html/body/div/ul[1]/li[5]", document,
null,XPathResult. UNORDERED_NODE_SNAPSHOT_TYPE , null);

in the XPath expression "/html/body/div/ul[1]/li[5]", I need to
traverse from li[1] to li[5],
so I should use a variable here , like this:

for (index =1; index<6; i++)
{
nodes = document.evaluate("/html/body/div/ul[1]/li[index]", ....);
// the variable index here is invalid!
}
my question is: How to use the variable in XPath expression?
appreciate your help!
 
J

Julian Turner

shellon wrote:

[snip]
nodes = document.evaluate("/html/body/div/ul[1]/li[index]", ....);
// the variable index here is invalid!
[/snip]

Hi

You may be confusing this with something like PHP which does allow you
to reference a variable within a string literal.

In JavaScript it should be

nodes = document.evaluate("/html/body/div/ul[1]/li[" + index+ "]",
.....);

Regards

Julian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,148
Latest member
ElizbethDa
Top