manjunathub
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....
<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....
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...
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:
Or, if you are using double quotes to surround your pattern string:
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\\"]';
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\\\"]";
ASKER
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);
Try this:
http://jsfiddle.net/2UruT/1/
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 + "]");
http://jsfiddle.net/2UruT/1/
ASKER
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
when it is used in the below statement
it looks like this
this xpath however in C#, vbscript it is working but not in javascript.. is there any way to handle this apart from replacing "\" by "\\"
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\"]
when it is used in the below statement
XmlDoc.selectSingleNode("//I[" + xPathExpression + "]")
it looks like this
XmlDoc.selectSingleNode("//I[" + "[@G="B: 0 mg/L"][@Day="6"][@OBS="test1's\"]" + "]")
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:
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)
In JavaScript (and I think in C# also) you can't avoid the escaping of \ because the strings are always processed for escape sequences.
XmlDoc.selectSingleNode("//I" + xPathExpression)
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);
http://www.w3schools.com/dom/dom_parser.aspIn JavaScript (and I think in C# also) you can't avoid the escaping of \ because the strings are always processed for escape sequences.
ASKER
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
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
ASKER
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:
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 + "]");
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Sudaraka u solved my problem... But i was not able to get y my code was failing when i did selectSingleNode().......
Open in new window
Reference:
http://www.w3schools.com/xpath/xpath_syntax.asp