Hello List!

if someone needs this: Gecko/Mozilla has no implementation of the simple to handle xpath node selection methods selectNodes and selectSingleNode (which are InternetExplorerer proprietary) for Element and Document object. Here is the Implementation built as patch for QxXmlExtras.


A little tutorial for the usage is:


selectNodes: http://www.webreference.com/js/tips/020309.html
selectSingleNode: http://www.webreference.com/js/tips/020310.html

--
Mit freundlichen Grüßen
Dietrich Streifert
Visionet GmbH

--- source/script/binding/QxXmlExtras.js.orig   Tue Nov 29 09:12:47 2005
+++ source/script/binding/QxXmlExtras.js        Tue Nov 29 09:14:08 2005
@@ -105,3 +105,64 @@
 
   throw new Error("This browser does not support xml dom creation.");
 };
+
+// Implementation of selectNodes() and selectSingleNode()
+// for Gecko/Mozilla browsers
+//
+if (window.XPathEvaluator) {
+
+  var xpath = new XPathEvaluator();
+
+  if (!Element.prototype.selectSingleNode) {
+    Element.prototype.selectSingleNode = function (path) {
+      var node = xpath.evaluate(path, this, this.ownerDocument._ns, 
XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
+  
+      if (node != null) {
+        node.text = node.firstChild ? node.firstChild.nodeValue : "";
+      }
+  
+      return node;
+    };
+  }
+  
+  if (!Element.prototype.selectNodes) {
+    Element.prototype.selectNodes = function (path) {
+      var result = xpath.evaluate(path, this, this.ownerDocument._ns, 
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
+      var i, nodes = [];
+  
+      for (i=0; i<result.snapshotLength; i++) {
+        nodes[i] = result.snapshotItem(i);
+        nodes[i].text = nodes[i].firstChild ? nodes[i].firstChild.nodeValue : 
"";
+      };
+  
+      return nodes;
+    };
+  }
+  
+  if (!Document.prototype.selectSingleNode) {
+    Document.prototype.selectSingleNode = function (path) {
+      var node = xpath.evaluate(path, this, this._ns, 
XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
+      
+      if (node != null) {
+        node.text = node.firstChild ? node.firstChild.nodeValue : "";
+      }
+      
+      return node;
+    };
+  }
+  
+  if (!Document.prototype.selectNodes) {
+    Document.prototype.selectNodes = function (path) {
+      var result = xpath.evaluate(path, this, this._ns, 
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
+      var i, nodes = [];
+      
+      for (i=0; i<result.snapshotLength; i++) {
+        nodes[i] = result.snapshotItem(i);
+        nodes[i].text = nodes[i].firstChild ? nodes[i].firstChild.nodeValue : 
"";
+      };
+      
+      return nodes;
+    };
+  }
+}
+

Reply via email to