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     }
  • 相关阅读:
    Ubuntu下手动安装vscode
    VMware Tools安装后设置自动挂载解决共享文件夹无法显示的问题
    VMware Tools安装方法及共享文件夹设置方法
    JavaScript原始类型转换和进制转换
    Javascript的数据类型(原始类型和引用类型)
    设计模式(六)观察者模式
    设计模式(五)之适配器模式
    设计模式(四)注册模式 解决:解决全局共享和交换对象
    设计模式(三)单例模式
    设计模式(二)之策略模式
  • 原文地址:https://www.cnblogs.com/ongoing/p/3085664.html
Copyright © 2011-2022 走看看