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     }
  • 相关阅读:
    safenet 超级狗 java调用 小计
    解析Javascript中大括号“{}”的多义性
    openlayers研究(一) 初始化流程
    计算球面两点间距离实现Vincenty+Haversine
    搭建高可用mongodb集群(四)—— 分片
    搭建高可用mongodb集群(三)—— 深入副本集内部机制
    搭建高可用mongodb集群(二)—— 副本集
    C# 7.1 的 Async Main()
    深入理解 C# 7.1 提供的 async 非同步 Main() 方法
    使用Blazor Server 线路处理程序 (circuit handler)跟踪打开的SignalR连接
  • 原文地址:https://www.cnblogs.com/ongoing/p/3085664.html
Copyright © 2011-2022 走看看