zoukankan      html  css  js  c++  java
  • common Xpath solution across browser

    //select single node

     1 function selectsingleNode(context, expression, namespaces) {
     2         var doc = (context.nodeType != 9 ? context.ownerDocument : context);
     3 
     4         if (typeof doc.evaluate != "undefined") {
     5             var nsresolver = null;
     6             if (namespaces instanceof Object) {
     7                 nsresolver = function (prefix) {
     8                     return namespaces[prefix];
     9                 };
    10             }
    11 
    12             var result = doc.evaluate(expression, context, nsresolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
    13             return (result !== null ? result.singleNodeValue : null);
    14         } else if (typeof context.selectSingleNode != "undefined") {
    15 
    16             //create namespace string
    17             if (namespaces instanceof Object) {
    18                 var ns = "";
    19                 for (var prefix in namespaces) {
    20                     if (namespaces.hasOwnProperty(prefix)) {
    21                         ns += 'xmlns:' + prefix + "='" + namespaces[prefix] + "'";
    22                     }
    23                 }
    24                 doc.setProperty("SelectionNamespaces", ns);
    25             }
    26             return context.selectSingleNode(expression);
    27         } else {
    28         throw new Error("no xpath engine found");
    29         }
    30     }

    //select nodes

     1 function selectNodes(context, expression, namespaces) {
     2         var doc = (context.nodeType != 9 ? context.ownerDocument : context);
     3 
     4         if (typeof doc.evaluate != "undefined") {
     5             var nsresolver = null;
     6             if (namespaces instanceof Object) {
     7                 nsresolver = function (prefix) {
     8                     return namespaces[prefix];
     9                 };
    10             }
    11 
    12             var result = doc.evaluate(expression, context, nsresolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
    13             var nodes = new Array();
    14 
    15             if (result != null) {
    16                 for (var i = 0, len = result.snapshotLength; i < len; i++) {
    17                     nodes.push(result.snapshotItem(i));
    18                 }
    19             }
    20 
    21             return nodes;
    22         } else if (typeof context.selectNodes != "undefined") {
    23 
    24             //create namespace string
    25             if (namespaces instanceof Object) {
    26                 var ns = "";
    27                 for (var prefix in namespaces) {
    28                     if (namespaces.hasOwnProperty(prefix)) {
    29                         ns += "xmlns:" + prefix + "='" + namespaces[prefix] + "'";
    30                     }
    31                 }
    32                 doc.setProperty("SelectionNamespaces", ns);
    33             }
    34             var result = context.selectNodes(expression);
    35             var nodes = new Array();
    36 
    37             for (var i = 0, len = result.length; i < len; i++) {
    38                 nodes.push(result[i]);
    39             }
    40 
    41             return nodes;
    42         } else {
    43         throw new Error("no xpath engine found");
    44         }
    45     }
  • 相关阅读:
    junit单元测试:@Before、@After
    package打包报错:There are test failures
    报错:程序包com.sun.image.codec.jpeg不存在
    报错:Caused by: java.sql.SQLException: Field 'id' doesn't have a default value
    elementui表格table组件的树形表格
    Linux常用命令
    黑马程序员hibernate2017版4天的讲义免费下载
    腾讯云服务器如何开放端口?
    文本编辑器中使用正则表达式进行替换的示例
    Lambda表达式
  • 原文地址:https://www.cnblogs.com/ongoing/p/3085664.html
Copyright © 2011-2022 走看看