Link to home
Start Free TrialLog in
Avatar of manjunathub
manjunathubFlag for India

asked on

get xml node from xpath...

want to get the node's from xml

<root>
    <DATA>
          <I  G="A: 0 mg/L" Day="5"  OBS="test's" />
          <I  G="B: 0 mg/L" Day="6"  OBS="test1's\" />
          <I  G="B: 0 mg/L" Day="4"  OBS="test's\" />
          <I  G="A: 0 mg/L" Day="1"  OBS="test's\" />
    </DATA>
</root>


I want to get node which has attributes   G="B: 0 mg/L" Day="6"  OBS="test1's\"     how to build the xpath for it so that i can get nodelists  using javascript.....

Please it is urgent and i don't have that much knowledge of handling XML's from JavaScript....
Avatar of Sudaraka Wijesinghe
Sudaraka Wijesinghe
Flag of Sri Lanka image

//I[@G='B: 0 mg/L' and @Day=6 and @OBS="test1's\"]

Open in new window


Reference:
http://www.w3schools.com/xpath/xpath_syntax.asp
Avatar of manjunathub

ASKER

i tried this but  JavaScript considers \" as an escape character and it is giving an error undetermined string.......

is there any other way to get that node...
 
You can use \\ to escape the single \

If you are using single quotes to surround your pattern string:
var pattern = '//I[@G=\'B: 0 mg/L\' and @Day=6 and @OBS="test1\'s\\"]';

Open in new window


Or, if you are using double quotes to surround your pattern string:
var pattern = "//I[@G='B: 0 mg/L' and @Day=6 and @OBS=\"test1's\\\"]";

Open in new window

this is the code wht i'm using to build the xpath
for(var i=0; i<this.data.length; i++){
    XPathKeyAttrib = "";
    for(var j = 0; j<this.keyAttribs.length; j++){
	XPathKeyAttrib += "@"+this.keyAttribs[j]+" = \""+ (this.data[i][j]).toString().trim() +"\" and "; 
}
}

// code to xml nodelist...
xmlTag = XmlDoc.selectSingleNode("//" + xPathExpression);

Open in new window

Try this:
for(var i=0; i<this.data.length; i++){
    XPathKeyAttrib = "";
    for(var j = 0; j<this.keyAttribs.length; j++){
        if(XPathKeyAttrib.length > 0) XPathKeyAttrib += ' and ';

        XPathKeyAttrib += "@"+this.keyAttribs[j]+" = \""+ (this.data[i][j]).toString().trim() +"\""; 
}
}

// code to xml nodelist...
xmlTag = XmlDoc.selectSingleNode("//*[" + xPathExpression + "]");

Open in new window


http://jsfiddle.net/2UruT/1/
srry i forgot to assign XPathKeyAttrib to "I" instead of  ""

i'm taking care of "and" but in xpath i'm getting escape character "\" from data itself, so the xpath (xPathExpression)  will be like

 I [@G="B: 0 mg/L"][@Day="6"][@OBS="test1's\"]

Open in new window


when it is used in the below statement
XmlDoc.selectSingleNode("//I[" + xPathExpression + "]")

Open in new window


it looks like this

XmlDoc.selectSingleNode("//I[" + "[@G="B: 0 mg/L"][@Day="6"][@OBS="test1's\"]" + "]")

Open in new window


this xpath however in C#, vbscript it is working but not in javascript.. is there any way to handle this apart from replacing "\" by "\\"



Since you are using [] for each attribute, you have to loose the [] in selectSingleNode:
XmlDoc.selectSingleNode("//I" + xPathExpression)

Open in new window


In order to get it to work in JavaScript you have to have the XML as a DOM object and use the method I have used in my example above (jsfiddle)
var result = xmldom.evaluate(xpath_pattern, xmldom, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

Open in new window

http://www.w3schools.com/dom/dom_parser.asp

In JavaScript (and I think in C# also) you can't avoid the escaping of \ because the strings are always processed for escape sequences.
i have attached a sample it is working fine in FF but not in IE....

input are

Input XML :<root><DATA><I  G="A: 0 mg/L" Day="5" OBS="test's" /><I  G="B: 0 mg/L" Day="6" OBS="test1's\" /><I  G="B: 0 mg/L" Day="4" OBS="test's\" /><I  G="A: 0 mg/L" Day="1" OBS="test's\" /></DATA></root>

Group :                  B: 0 mg/L
Day :                     4
Observation :        test's\
textXmlPrse.html
can any body help pls it is urgent......
Sorry for the delay in reply.

It looks like for IE you need to escape the \ in test's\
You can replace \ with \\ explicitly for IE only like below:
xpath = xpath.replace(/\\/g, '\\\\');
xmlNodeList = xmlDoc.selectSingleNode("//I[" + xpath + "]");

Open in new window

thnx fro ur reply i tried replacing "\" by "\\" but still in ie it is giving an error....  in attached html i tried but it gave me an error....
ASKER CERTIFIED SOLUTION
Avatar of Sudaraka Wijesinghe
Sudaraka Wijesinghe
Flag of Sri Lanka image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Sudaraka  u solved my problem... But i was not able to get y my code was failing when i did selectSingleNode().......