General XMetaL Discussion

XMetaL Community Forum General XMetaL Discussion Getting value of an attribute using getNodesByXPath

  • dcramer

    Getting value of an attribute using getNodesByXPath

    Participants 1
    Replies 2
    Last Activity 13 years, 1 month ago

    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');[/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,
    David

    Reply

    Derek Read

    Reply to: Getting value of an attribute using getNodesByXPath

    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);
    }[/code]

    Reply

    dcramer

    Reply to: Getting value of an attribute using getNodesByXPath

    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”;
    [/code]

    Thanks,
    David

    Reply

  • You must be logged in to reply to this topic.

Lost Your Password?

Products
Downloads
Support