General XMetaL Discussion
XMetaL Community Forum › General XMetaL Discussion › Getting value of an attribute using getNodesByXPath
-
dcramer September 2, 2009 at 9:03 pm
Getting value of an attribute using getNodesByXPath
September 2, 2009 at 9:03 pmParticipants 1Replies 2Last Activity 13 years, 4 months agoHi 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');[/code]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,
DavidDerek Read September 2, 2009 at 9:26 pm
Reply to: Getting value of an attribute using getNodesByXPath
September 2, 2009 at 9:26 pmDavid, 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);
}[/code]dcramer September 3, 2009 at 12:45 pm
Reply to: Getting value of an attribute using getNodesByXPath
September 3, 2009 at 12:45 pmOk, 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”;
[/code]Thanks,
David -
AuthorPosts
- You must be logged in to reply to this topic.