XPath:xml path Language,是用于确定xml某个节点位置的语言。
根据xpath查找节点的JavaScript方法
function selectSingleNode(xmldoc,sXpath){
if (window.ActiveXObject) {
//IE浏览器
return xmldoc.selectSingleNode(sXpath);
} else if (window.XPathEvaluator) {
//FireFox类浏览器
var xpathObj = new XPathEvaluator();
if (xpathObj) {
var result = xpathObj.uate(sXpath,xmldoc,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null);
return result.singleNodeValue;
} else {
return null;
}
} else {
return null;
}
}
function selectNodes(xmldoc,sXpath){
if (window.ActiveXObject) {
//IE浏览器
return xmldoc.selectNodes(sXpath);
} else if (window.XPathEvaluator) {
//FireFox类浏览器
var xpathObj = new XPathEvaluator();
if (xpathObj) {
var result = xpathObj.uate(sXpath,xmldoc,null,XPathResult.ORDERED_NODE_ITERATOR_TYPE,null);
//得到的result是个迭代对象,需要把它里面的内容转存到数组中
var nodes = new Array();
var node;
while((node = result.iterateNext()) != null) {
//把迭代器中的内容存到数组中
nodes.push(node);
}
return nodes;
} else {
return null;
}
} else {
return null;
}
}
下面是利用上面的方法查找xml文档中指定条件节点的代码
function testxpath(){
var rootElement = loadXML(true,"XPATH.xml");
removeBlank(rootElement);
//查找所有的author元素节点
//关键就是如何表式元素节点在DOM树中的路径
// 绝对路径 /books/book/author
// 相对路径 book/author
// 全文档搜索法 //author //book/author
var author1 = selectNodes(rootElement,"/books/book/author");
var author2 = selectNodes(rootElement,"book/author");
var author3 = selectNodes(rootElement,"//author");
var author4 = selectNodes(rootElement,"//book/author");
//查找属性节点,查找book节点下isdn属性
//关键是先找到包含属性节点的元素节点
var isdn1 = selectNodes(rootElement,"/books/book/@isdn");
var isdn2 = selectNodes(rootElement,"[email=book/@isdn]book/@isdn[/email]");
var isdn3 = selectNodes(rootElement,"//book/@isdn");
var isdn4 = selectNodes(rootElement,"//books/book/@isdn");
//查找文本节点,name元素下面的文本
//关键是找到包含文本节点的那个元素节点
var text1 = selectNodes(rootElement,"/books/book/name/text()");
var text2 = selectNodes(rootElement,"book/name/text()");
var text3 = selectNodes(rootElement,"//book/name/text()");
var text4 = selectNodes(rootElement,"//name/text()");
//有条件的查找元素节点,包含属性isdn
//需要首先找到元素节点,然后再增加条件表达式
var book1 = selectNodes(rootElement,"/books/book[@isdn]");
var book2 = selectNodes(rootElement,"book[@isdn]");
var book3 = selectNodes(rootElement,"//book[@isdn]");
//多个条件与关系的查找元素节点
//需要首先找到元素节点,然后每一个查询条件写在一个中括号中
var bookA1 = selectNodes(rootElement,"/books/book[@isdn='0002'][price>35]");
var bookA2 = selectNodes(rootElement,"book[@isdn='0002'][price>35]");
var bookA3 = selectNodes(rootElement,"//book[@isdn='0002'][price>35]");
//多个条件或关系的查找元素节点
//需要首先找到元素节点,然后每一个查询条件写在一个XPATH的表达式,这些表达式用|组合,表式或关系
var bookO1 = selectNodes(rootElement,"/books/book[@isdn='0002'] | /books/book[price>35]");
var bookO2 = selectNodes(rootElement,"book[@isdn='0002'] | /books/book[price>35]");
var bookO3 = selectNodes(rootElement,"//book[@isdn='0002'] | /books/book[price>35]");
}
//IE浏览器
return xmldoc.selectSingleNode(sXpath);
} else if (window.XPathEvaluator) {
//FireFox类浏览器
var xpathObj = new XPathEvaluator();
if (xpathObj) {
var result = xpathObj.uate(sXpath,xmldoc,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null);
return result.singleNodeValue;
} else {
return null;
}
} else {
return null;
}
}
function selectNodes(xmldoc,sXpath){
if (window.ActiveXObject) {
//IE浏览器
return xmldoc.selectNodes(sXpath);
} else if (window.XPathEvaluator) {
//FireFox类浏览器
var xpathObj = new XPathEvaluator();
if (xpathObj) {
var result = xpathObj.uate(sXpath,xmldoc,null,XPathResult.ORDERED_NODE_ITERATOR_TYPE,null);
//得到的result是个迭代对象,需要把它里面的内容转存到数组中
var nodes = new Array();
var node;
while((node = result.iterateNext()) != null) {
//把迭代器中的内容存到数组中
nodes.push(node);
}
return nodes;
} else {
return null;
}
} else {
return null;
}
}
下面是利用上面的方法查找xml文档中指定条件节点的代码
function testxpath(){
var rootElement = loadXML(true,"XPATH.xml");
removeBlank(rootElement);
//查找所有的author元素节点
//关键就是如何表式元素节点在DOM树中的路径
// 绝对路径 /books/book/author
// 相对路径 book/author
// 全文档搜索法 //author //book/author
var author1 = selectNodes(rootElement,"/books/book/author");
var author2 = selectNodes(rootElement,"book/author");
var author3 = selectNodes(rootElement,"//author");
var author4 = selectNodes(rootElement,"//book/author");
//查找属性节点,查找book节点下isdn属性
//关键是先找到包含属性节点的元素节点
var isdn1 = selectNodes(rootElement,"/books/book/@isdn");
var isdn2 = selectNodes(rootElement,"[email=book/@isdn]book/@isdn[/email]");
var isdn3 = selectNodes(rootElement,"//book/@isdn");
var isdn4 = selectNodes(rootElement,"//books/book/@isdn");
//查找文本节点,name元素下面的文本
//关键是找到包含文本节点的那个元素节点
var text1 = selectNodes(rootElement,"/books/book/name/text()");
var text2 = selectNodes(rootElement,"book/name/text()");
var text3 = selectNodes(rootElement,"//book/name/text()");
var text4 = selectNodes(rootElement,"//name/text()");
//有条件的查找元素节点,包含属性isdn
//需要首先找到元素节点,然后再增加条件表达式
var book1 = selectNodes(rootElement,"/books/book[@isdn]");
var book2 = selectNodes(rootElement,"book[@isdn]");
var book3 = selectNodes(rootElement,"//book[@isdn]");
//多个条件与关系的查找元素节点
//需要首先找到元素节点,然后每一个查询条件写在一个中括号中
var bookA1 = selectNodes(rootElement,"/books/book[@isdn='0002'][price>35]");
var bookA2 = selectNodes(rootElement,"book[@isdn='0002'][price>35]");
var bookA3 = selectNodes(rootElement,"//book[@isdn='0002'][price>35]");
//多个条件或关系的查找元素节点
//需要首先找到元素节点,然后每一个查询条件写在一个XPATH的表达式,这些表达式用|组合,表式或关系
var bookO1 = selectNodes(rootElement,"/books/book[@isdn='0002'] | /books/book[price>35]");
var bookO2 = selectNodes(rootElement,"book[@isdn='0002'] | /books/book[price>35]");
var bookO3 = selectNodes(rootElement,"//book[@isdn='0002'] | /books/book[price>35]");
}