Looping through Data Source nodes - InfoPath Dev
in

InfoPath Dev

Looping through Data Source nodes

Last post 07-17-2007 09:03 AM by skrile. 7 replies.
Page 1 of 1 (8 items)
Sort Posts: Previous Next
  • 07-17-2007 06:07 AM

    • skrile
    • Top 500 Contributor
    • Joined on 08-17-2006
    • USA
    • Posts 29

    Looping through Data Source nodes

    I have a project where I want to loop through a group of data in my Main Datasource.  The structure of my data is:

     Main
        myFields
            lables
                label1
                label2
                label3
                label4

     What I want to do is write a looping function that looks at each of the children of labels and then do some stuff.  I've tried something like this:

     var oNode = null
    var oNodes = XDocument.DOM.selectNodes("//my:myFields/my:labels")
    while (oNode = oNodes.nextNode())
        { XDocument.UI.Alert(oNode.text); }

    This shows me the TEXT of each oNode, but I want the NAME of the oNode. 

    I've tried  XDocument.UI.Alert(oNode.nodeName); and it just returns my:labels. 

    I've tried XDocument.UI.Alert(oNode.firstChild.nodeName); and it just returns #text

    Any thoughts?

  • 07-17-2007 06:23 AM In reply to

    Re: Looping through Data Source nodes

    Try the following xpath instead:

    /my:myFields/my:labels/*

    Then you should be able to use nodeName

     

    David Airapetyan / Software Services Architect
    Qdabra® Software / Streamline data gathering to turn process into knowledge
  • 07-17-2007 06:26 AM In reply to

    • skrile
    • Top 500 Contributor
    • Joined on 08-17-2006
    • USA
    • Posts 29

    Re: Looping through Data Source nodes

    Boo ya.  Thank you.  That did it!
  • 07-17-2007 06:54 AM In reply to

    • skrile
    • Top 500 Contributor
    • Joined on 08-17-2006
    • USA
    • Posts 29

    Re: Looping through Data Source nodes

    So, now I'm trying to use that information to refresh all the data in those nodes by values stored in another data set.  The complete code is:

    function XDocument::OnLoad(eventObj)
    {
       var objTranslation = XDocument.DataObjects("ThirdPartyEvent_Translations").DOM; objTranslation.setProperty("SelectionNamespaces",'xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" ' + ' xmlns:d="http://schemas.microsoft.com/office/infopath/2003/ado/dataFields"');
      var selectedLanguage = XDocument.DOM.selectSingleNode("/my:myFields/my:languageview").text; 
      var oNode = null
      var transtring = ""
      var oNodes = XDocument.DOM.selectNodes("//my:myFields/my:labels/*")
      while (oNode = oNodes.nextNode())
      {    transtring = objTranslation.selectSingleNode("//dfs:myFields/dfs:dataFields/dfs:ThirdPartyEvent_Translations/@Translation[../@stringID = 'user_sectionhead' and ../@languageid = '" + selectedLanguage + "']").text
           if (transtring != null)
              {XDocument.DOM.selectSingleNode("/my:myFields/my:labels/" + oNode.nodeName).text = transtring}
       }
    }

     

    The above code WORKS!  But, all the labels are painted with the corresponding text to the user_sectionhead stringid.

    The problem I am running in to is that when I try to replace 'user_sectionhead' (one of the names of my nodes under labels) I get a Object required error.  Here is the syntax that IS NOT WORKING:

    transtring = objTranslation.selectSingleNode("//dfs:myFields/dfs:dataFields/dfs:ThirdPartyEvent_Translations/@Translation[../@stringID = '" + oNode.nodeName + "'and ../@languageid = '" + selectedLanguage + "']").text

     

    Could it be that oNode.nodeName is not "text" and needs to be converted to a string? 

  • 07-17-2007 07:24 AM In reply to

    Re: Looping through Data Source nodes

    Technically this code seems to make sense but maybe there is something else under labels which causes this to fail. I would recommend to step through your code in the debugger, looking at oNode at each iteration.

    David Airapetyan / Software Services Architect
    Qdabra® Software / Streamline data gathering to turn process into knowledge
  • 07-17-2007 08:43 AM In reply to

    • skrile
    • Top 500 Contributor
    • Joined on 08-17-2006
    • USA
    • Posts 29

    Re: Looping through Data Source nodes

    OK, I found the issue.  I named my stringIDs on my Sharepoint list things like 'user_username' and 'user_emailaddress'.  oNode.nodeName was returning 'my:user_username' etc.  I used the string.replace("my:","") function and all is well.  However, I was wondering.  Is there a way to "trap" the fact that the following doesn't exist?

     function transtring(sid,thislid)
       { var objTranslation = XDocument.DataObjects("ThirdPartyEvent_Translations").DOM;
         objTranslation.setProperty("SelectionNamespaces",'xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" ' + ' xmlns:d="http://schemas.microsoft.com/office/infopath/2003/ado/dataFields"');
         sid = sid.replace("my:","")
         var tstring = objTranslation.selectSingleNode("//dfs:myFields/dfs:dataFields/dfs:ThirdPartyEvent_Translations/@Translation[../@stringID = '"+sid+"' and ../@languageid = '"+thislid+"']").text; return tstring;
     
     }

    I tried:

    if (objTranslation.selectSingleNode("//dfs:myFields/dfs:dataFields/dfs:ThirdPartyEvent_Translations/@Translation[../@stringID = '"+sid+"' and ../@languageid = '"+thislid+"']").text == null) {}

     But that didn't work.  Got the same object error.

  • 07-17-2007 08:51 AM In reply to

    Re: Looping through Data Source nodes

    Well, selectSingleNode returns a node object if xpath matches or null otherwise. So if you do .text of something which is null, you will get an exception.

    Instead, do something like that:

    var someNode = something.selectSingleNode(someXPath)
    if (someNode != null)
    {
      // do something with someNode.text
    }

     

    David Airapetyan / Software Services Architect
    Qdabra® Software / Streamline data gathering to turn process into knowledge
  • 07-17-2007 09:03 AM In reply to

    • skrile
    • Top 500 Contributor
    • Joined on 08-17-2006
    • USA
    • Posts 29

    Re: Looping through Data Source nodes

    Genius.  Here is all the code, and it works great.  I'll always have an English translation, but may be missing some other.  This way, at least the English will always be dispalyed.

    //When loading the document, set all the labels to the default language id choice (english)
    function XDocument::OnLoad(eventObj)
      { var langID = XDocument.DOM.selectSingleNode("/my:myFields/my:languageview").text;
        setTranslations(langID);
      }

    //After a user changes the language dropdown, reset all the labels if possible to the language of choice
    function msoxd_my_languageview::OnAfterChange(eventObj)
    { var langID = XDocument.DOM.selectSingleNode("/my:myFields/my:languageview").text;
      setTranslations(langID);
    }

    //Set all the language labels to the proper translation if possible.
    function setTranslations(lid)
    {
      var oNode = null;
      var oNodes = XDocument.DOM.selectNodes("//my:myFields/my:labels/*");
      while (oNode = oNodes.nextNode())
      { var theString = transtring(oNode.nodeName,lid);
        XDocument.DOM.selectSingleNode("/my:myFields/my:labels/"+oNode.nodeName).text = transtring(oNode.nodeName,lid);
      }
    }

    //Actually do the work of retrieving the translation.
    //First, set the translation to N/A
    //Then see if the currently selected language (languageview) has a translation
    //If not, then see if there is an English equivelant
    //If not, then just return the N/A
    function transtring(sid,thislid)
    { var resultstring = "N/A"
      var objTranslation = XDocument.DataObjects("ThirdPartyEvent_Translations").DOM;
      objTranslation.setProperty("SelectionNamespaces",'xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" ' + ' xmlns:d="http://schemas.microsoft.com/office/infopath/2003/ado/dataFields"');
     
      //sid is passed in from a looping function that goes through all the labels node.  InfoPath sticks a "my:" on the front of each
      //of the nodes.  I didn't use "my:" in my naming convention in the Sharepoint list, so here we strip it out.
      sid = sid.replace("my:","") 

      var tstring = objTranslation.selectSingleNode("//dfs:myFields/dfs:dataFields/dfs:ThirdPartyEvent_Translations/@Translation[../@stringID = '"+sid+"' and ../@languageid = '"+thislid+"']");
      if (tstring == null)
      { tstring = objTranslation.selectSingleNode("//dfs:myFields/dfs:dataFields/dfs:ThirdPartyEvent_Translations/@Translation[../@stringID = '"+sid+"' and ../@languageid = 'en']") }
      if (tstring != null) { var resultstring = tstring.text }
      return resultstring;
    }

     

    Thanks for you help.

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