Getting the full Path of a node javascript - InfoPath Dev
in

InfoPath Dev

Getting the full Path of a node javascript

Last post 01-18-2013 10:35 AM by svallab. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 01-17-2013 05:44 AM

    Getting the full Path of a node javascript

    I have been trying to get the full path to a node. the selectnodes is always giving undefined. Am I doing anything wrong? or is thee another way to get full xpath for a node using GetContextNodes(). Please help! Urgent.


    function AddComment_onclick() {

        //here get the conext
        // More than one item may be selected in the form, such as multiple table rows...
        // Just get the first item (zero-based).
        var oFocusNode = goXDocument.View.GetContextNodes().Item(0);

        // Build the XPath to the focus field.
        // This will be used to identify a matching XPath in the Help DOM.
        var sXPath = "";
        var oPathNode = oFocusNode
       
        sXPath = GetXPath_SequentialIteration(oPathNode);
        alert(sXPath);
    }

    function GetXPath_SequentialIteration(node)
    {
        try
        {
            var path;
            path = "/" + node.baseName;
            alert(path);
            pNode = node.parentNode;
            if (pNode != null) {
                // Gets the position within the parent element.
                // However, this position is irrelevant if the element is unique under its parent:
                var siblings = pNode.SelectNodes(node.baseName);
                alert("Siblings " + siblings.length);
                if (siblings != null && siblings.length > 1) // There's more than 1 element with the same name
                {
                    var position = 1;
                    for (i = 0; i < siblings.length; i++)
                    {
                        if (i == node)
                        {
                            break;
                        }

                        position++;
                    }

                    path = path + "[" + position + "]";
                }

                // Climbing up to the parent elements:
                path = pNode.GetXPath_SequentialIteration() + path;
            }
        }
        catch (e) {
            // Currently we don't handle any exceptions.
            // But we also don't want an error to appear if there is one.
            alert(e.Description);
        }

  • 01-18-2013 10:35 AM In reply to

    Re: Getting the full Path of a node javascript

    fixed the code. the corrent one is selectNodes not SelectNodes.

    the right code is

     

    function AddComment_onclick() {

    //here get the conext

    // More than one item may be selected in the form, such as multiple table rows...

    // Just get the first item (zero-based).

    var oFocusNode = goXDocument.View.GetContextNodes().Item(0);

    // Build the XPath to the focus field.

    // This will be used to identify a matching XPath in the Help DOM.

    var sXPath = "";var oPathNode = oFocusNode

    sXPath = GetXPath_SequentialIteration(oPathNode);

    var comment = document.getElementById('NewComment');

    window.external.Window.XDocument.Extension.AddComment(sXPath, comment.value);

    document.getElementById('NewComment').value = "";

    goPrevFocusNode = null;

    }

    function GetXPath_SequentialIteration(node)

    {

    try

    {

    var path;path = "/" + node.tagName;

    pNode = node.parentNode;

    if (pNode != null) {

    // Gets the position within the parent element.

    // However, this position is irrelevant if the element is unique under its parent:

    goXDocument.DOM.setProperty("SelectionNamespaces", 'xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2012-11-23T23:50:49"');

    var siblings = pNode.selectNodes(node.tagName);

    if (siblings != null && siblings.length > 1) // There's more than 1 element with the same name

    {

    var position = 1;

    for (i = 0; i < siblings.length; i++) {

    if (siblings[i] == node) {break;

    }

    position++;

    }

    path = path +
    "[" + position + "]";

    }

    // Climbing up to the parent elements:

    if (pNode.parentNode != null) {

    path = GetXPath_SequentialIteration(pNode) + path;

    }

    }

    }

    catch (e) {

    // Currently we don't handle any exceptions.

    // But we also don't want an error to appear if there is one.

    alert(e);

    }

    return path;

    }

     

     

Page 1 of 1 (2 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.