Pages: 1
Print
Author Topic: Getting value of an attribute using getNodesByXPath  (Read 4361 times)
dcramer
Member

Posts: 120


« on: September 02, 2009, 03:03:18 PM »

Hi there,
This is probably simple, but I'm obviously missing something. I want to grab the value of the id of the nearest ancestor that has an id. I'm using getNodesByXPath and think I'm getting the id I want:

Code:
var rng = ActiveDocument.Range;
var nd = rng.ContainerNode;
var id = nd.getNodesByXPath('ancestor-or-self::*[@id][1]/@id');

id.length is 1 and id.item(0).nodeName is 'id', so that looks good, but id.item(0).data is undefined.

What should I be doing?

Thanks,
David
Logged

David Cramer
Technical Writer
Motive, an Alcatel-Lucent Company
Derek Read
Program Manager (XMetaL)
Administrator
Member

Posts: 2621



WWW
« Reply #1 on: September 02, 2009, 03:26:35 PM »

David, try this:

Code:
//XMetaL Script Language JScript:
var rng = ActiveDocument.Range;
var nd = rng.ContainerNode;
var id = nd.getNodesByXPath('ancestor-or-self::*[@id][1]/@id');
if(id.length > 0) {
Application.Alert("Node Name: " + id.item(0).nodeName);
Application.Alert("Node Value: " + id.item(0).nodeValue);
}
Logged
dcramer
Member

Posts: 120


« Reply #2 on: September 03, 2009, 06:45:28 AM »

Ok, I see, in the example for getNodesByXpath ".data" is used because it's getting the value of a text node:

Code:
tableOfContents += currTitleNumber + ". " + title.childNodes.item(0).data + "\n";

Thanks,
David
Logged

David Cramer
Technical Writer
Motive, an Alcatel-Lucent Company
Pages: 1
Print
Jump to: