XDocument.DOM.selectSingleNode using variable

S

stingbot

Hi,

I'm trying to select a node in my form using a changing variable.

Due to the way i need to display text and due to this field being in a
section inside of a repeating section already, i havent been able to
rework the form to be entirely repeating.

I have this code at the moment:

function ParseDate(date,NodeName)
{

var oldDate;
//this is used to hold the ISO formatted date

var newDate;
//this is used to hold the date after it has been changed

oldDate = date;
//set this variable with the date passed from the calling function.

newDate = oldDate.substring(8,10) + "/" + oldDate.substring(5,7) + "/"
+ oldDate.substring(0,4);
//convert date to dd/mm/yyy

var str1 = '//my:';
//start path to xml node

var str2 = NodeName;
//finish path to xml node taken from parameter in function name

var FieldToChange = str1.concat(str2);
//combine the 2 strings to make the whole path.

var NewDateNode = XDocument.DOM.selectSingleNode(FieldToChange);
//select the node defined in FieldToChange

newDateNode.text = newDate;
//Set chosen field on the form with the new date format

}

I get NewDateNode is undefined, and when i debug i find that
NewDateNode is not being set from FieldToChange

FieldToChange is the proper xml path. "//my:Australian_Date", I can see
this with a XDocument.UI.Alert(FieldToChange);

The whole point is that I was trying to write a function that I could
reuse on various fields to insert the correct date (07/08/2006,
dd/mm/yyyy) for Australia, as the ISO date format doesnt look nice in
any reports generated by the form. A Date picker control is used to get
the date parameter in the function name.

I am trying not to write this block of code for each and every field I
want to do this to.

Any Help or comments appreciated.
 
S

stingbot

Hmmm, It seems I have solved my own problem. This is how I did it:

function ParseDate(date,NodeName)
{
var oldDate;
var newDate;
oldDate = date;
newDate = oldDate.substring(8,10) + "/" + oldDate.substring(5,7) + "/"
+ oldDate.substring(0,4);
var nodeToChange = XDocument.DOM.selectSingleNode("/my:HRASection//my:"
+ NodeName);
nodeToChange.text = newDate;
}

I'm still learning, and I dont quite understand why I was getting
errors previously.

Using less lines and variables. It works from anywhere I call it so
far.
 
A

AlphaAJ

In your first code, when you were calling selectSingleNode(), it looks like
your argument was my://whatever_your_node_is_called. In your second example,
you have that prefixed with "/my:HRASection//" Maybe the path really wasn't
correct originally. But since everything works now, this issue might be
moot.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top