Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / General MS InfoPath Questions / April 2007

Tip: Looking for answers? Try searching our database.

Find an attribute's parent element

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Deken - 28 Mar 2007 21:52 GMT
In Infopath 2003, in JScript, is it possible to determine an attribute's
parent element?  I need to be able to update the value of a sibling attribute
when the first attribute is changed, and I can find no way to climb the tree
from the attribute to its containing element.

I would think that this would work:
/**********************************************/
function OnAfterChange(eventObj)
{
        var curNode = eventObj.Source;
        var found = false;
        while (! found) {
            if (curNode.nodeTypeString == "element"){
                var modflag = curNode.getAttribute("modflag");
                if (modflag != null) { // null is missing
                    if( modflag != "true" ) {
                        curNode.setAttribute("modflag","true");
                    }
                    found = true;
                }
            }
            curNode = curNode.parentNode;
            if (curNode == null) {
                found = true; // pseudo found - reached top
            }
        }
        return;
}
/**********************************************/
except that when curNode.nodeTypeString == "attribute", curNode.parentNode
is null.

TIA for any assistance available!

Deken
Greg Collins [InfoPath MVP] - 29 Mar 2007 03:35 GMT
In your situation you would do the following:

var parent = curNode.selectSingleNode("..");

This works despite parentNode being null.

Signature

Greg Collins [Microsoft MVP]
Visit Braintrove ( http://www.braintrove.com )
Visit InfoPathDev ( http://www.InfoPathDev.com )

Deken - 29 Mar 2007 14:52 GMT
THANK YOU!

First I tried
/****************************************************/
function OnAfterChange(eventObj)
{
        var curNode = eventObj.Source;
        var found = false;
        while (! found) {
            if (curNode.nodeTypeString == "element"){
                var modflag = curNode.getAttribute("modflag");
                if (modflag != null) { // null is missing
                    if( modflag != "true" ) {
                        curNode.setAttribute("modflag","true");
                    }
                    found = true;
                }
            }
            curNode = curNode.selectSingleNode("..");
            if (curNode == null) {
                found = true; // pseudo found - reached top
            }
        }
        return;
}
/****************************************************/

However, that results in an error: "MSXML5.DLL: A text node child of an
attribute may not be passed as the context node for an XSLT transform or an
XPath query."
(eventObj starts out as the text node child of the 'status' attribute, once
for a "Delete" operation to get rid of the existing value, a second time for
an "Insert" operation to add the new value)

SOLUTION:
The code that works is
/****************************************************/
function OnAfterChange(eventObj)
{
        var curNode = eventObj.Source;
        var found = false;
        while (! found) {
            if (curNode.nodeTypeString == "element"){
                var modflag = curNode.getAttribute("modflag");
                if (modflag != null) {
                    if( modflag != "true" ) {
                        curNode.setAttribute("modflag","true");
                    }
                    found = true;
                }
            }
            else if (curNode.nodeTypeString == "text"){
                curNode = curNode.parentNode;
            }
            else if (curNode.nodeTypeString == "attribute"){
                curNode = curNode.selectSingleNode("..");
            }
            if (curNode == null) {
                found = true; // pseudo found - reached top or can't climb higher due to
object model idiosyncracies
            }
        }
        return;
}
/****************************************************/

> In your situation you would do the following:
>
> var parent = curNode.selectSingleNode("..");
>
> This works despite parentNode being null.
Greg Collins [InfoPath MVP] - 04 Apr 2007 18:11 GMT
Glad you got it working. Thanks for the update!

Signature

Greg Collins [Microsoft MVP]
Visit Braintrove ( http://www.braintrove.com )
Visit InfoPathDev ( http://www.InfoPathDev.com )

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.