Need help with Xpath methods in javascript (selectSingleNode, selectNodes)
I want to optimize my javascript but I ran into a bit of trouble. I'm using XSLT transformation and the basic idea is to get a part of the XML and subquery it so the calls are faster and less expensive. This is a part of the XML:
<suite>
<table id="spis" runat="client">
<rows>
<row id="spis_1">
<dispatch>'2008', '288627'</dispatch>
<data col="urGod">
<title>2008</title>
<description>Ur. god.</description>
</data>
<data col="rbr">
<title>288627</title>
<description>Rbr.</description>
</data>
...
</rows>
</table>
</suite>
In the page, this is the javascript that works with this:
// this is my global variable for getting the elements so I just get the most
// I can in one call
elemCollection = iDom3.Table.all["spis"].XML.DOM.selectNodes("/suite/table/rows/row").context;
//then I have the method that uses this by getting the subresults from elemCollection
//rest of the method isn't interesting, only the selectNodes call
_buildResults = function (){
var _RowList = elemCollection.selectNodes("/data[@col = 'urGod']/title");
var tmpResult = [''];
var substringResult="";
for (i=0; i<_RowList.length; i++) {
tmpResult.push(_RowList[i].text,iDom3.Global.Delimiter);
}
...
//this variant works
elemCollection = iDom3.Table.all["spis"].XML.DOM
_buildResults = function (){
var _RowList = elemCollection.selectNodes("/suite/table/rows/row/data[@col = 'urGod']/title");
var tmpResult = [''];
var substringResult="";
for (i=0; i<_RowList.length; i++) {
tmpResult.push(_RowList[i].text,iDom3.Global.Delimiter);
}
...
开发者_JAVA技巧
The problem is, I can't find a way to use the subresults to get what I need.
You are trying to select nodes from a collection (NodeList).
elemCollection.selectNodes("/data[@col = 'urGod']/title");
Try to select a node from a single item (Node) from the collection such as:
elemCollection[i].selectSingleNode("/data[@col = 'urGod']/title")
Added to your code (without data validation and assuming we want the first match for the col attribute):
_buildResults = function (){
var tmpResult = [''];
var substringResult="";
for (i=0; i<elemCollection.length; i++) {
tmpResult.push(elemCollection[i].selectSingleNode("/data[@col = 'urGod']/title").text,iDom3.Global.Delimiter);
}
You will need to iterate the elemCollection list, and then do this query:
./data[@col = 'urGod']/title
精彩评论