2008-10-08 7 views

Antwort

10

Google hat gerade Wicked Good XPath veröffentlicht - eine Neufassung des berühmten JavaScript-XPath von Cybozu Lab.

-Link: https://github.com/google/wicked-good-xpath

Die neu geschriebene Version ist 40% kleiner und 30% schneller als die ursprüngliche Implementierung.

+0

Dies ist die richtige Wahl jetzt im Jahr 2012. zu schade, dass Sie nicht Antworten ablehnen können –

+1

Vielleicht ist es eine gute Lösung für HTML-Dokumente. Aber es gibt ein offenes Problem für XML-Dokumente https://code.google.com/p/wicked-good-xpath/issues/detail?id=7 – dizel3d

+0

Beachten Sie, dass zum Zeitpunkt des Schreibens der Download unter dem angegebenen Link isn arbeite nicht. Die schnellste Lösung, die ich gefunden habe, war, sie aus einem NPM-Paket zu holen ([hier] (https://github.com/jacobmarble/node-wgxpath)). – Kat

4

Sie könnten die grundlegende XPath plug-in for jQuery verwenden, um XPath-Abfrage-Funktionalität zu erhalten.

Auch könnten Sie lesen betrachten diese article on XPath XML processing (wieder mit jQuery)

+1

Danke! Ich habe beide gesehen. Das XPath-Plugin für jQuery konvertiert den XPath-Ausdruck einfach in den jQuery-Auswahlausdruck. Es funktioniert nicht einmal für einfache Fälle. –

+0

Funktioniert das überhaupt mit jquery> 1.2? –

+0

Ab heute sind diese Bibliotheken nicht verfügbar. –

3

Werfen Sie einen Blick auf http://dev.abiss.gr/sarissa/ Projekt. Sie haben die meisten XML-bezogenen APIs in den IE migriert und die Methode für das Dokumentobjekt ausgewertet. Und in der Tat hat jQuery keinen XPath-Prozessor, es hat einen sehr einfachen Pfadselektor wie:/a/b/c nur

0

Ich denke nicht, dass es Ad-hoc-Abfragen ermöglicht, aber Sie können sich Johann Burkard XSLT jQuery plug-in für Inspiration, wie Sie XPath-Abfragen implementieren. Ich benutze es in meinem jQuery Reference Dashboard Widget und es ist wirklich solide.

5

Google's AJAXSLT Open-Source-Projekt passt gut die angegebenen Anforderungen.

Wie ihre eigene Beschreibung geht zu sagen:

„AJAXSLT ist eine Implementierung von XSLT in JavaScript Da XSLT verwendet XPath, es ist auch eine Implementierung von XPath, die unabhängig von XSLT verwendet werden kann.. Diese Implementierung hat den Vorteil, dass XSLT auf mehreren Browsern einheitlich verfügbar ist, als dies von Natur aus möglich ist und dass es bei Bedarf auf noch mehr Browser erweitert werden kann .. AJAXSLT ist für Entwickler interessant, die sich aggressiv für die Browser-Kompatibilität ihres fortgeschrittenen Webs einsetzen Anwendungen. "

UPDATE: Ende 2010 hat Michael Kay seinen Saxon XSLT 2.0-Prozessor mit Hilfe von GWT zu Javascript kompiliert (und damit für alle 5 gängigen Browser verfügbar gemacht). Es ist wahrscheinlich, dass es in Kürze einen leichtgewichtigen In-Browser-Saxon geben wird.

+0

Ab November 2011 ist die Client Edition (CE) von Kay's Saxon noch in Alpha. –

+0

@ james.garriss: Das bedeutet nicht, dass es nicht verwendet wird. Es gibt interessante Threads in der xsl-Liste zu diesem Thema. Ich vermute, dass das Hauptproblem von Saxonica nicht technologischer Natur ist, sondern vielmehr, das richtige Geschäftsmodell für den sächsischen CE zu finden, damit es profitabel werden kann. –

+0

Die verlinkte Seite hat nun Folgendes zu sagen: * "Die Welt der Browser und Webanwendungen ist seit der Implementierung dieser Bibliothek 2005 weitergezogen. Viele weitere Browser implementieren nun XSLT und XPath nativ und, noch wichtiger, weniger Webanwendungen XML als ihr Übertragungsdatenformat und weiter zu dem geeigneteren und pragmatischeren JSON. Eine einfachere und flexiblere Möglichkeit, Daten an HTML auf der Browserseite zur Anzeige in Webanwendungen zu binden, ist google-jstemplate. "* –

0

Sie können die vorhandene native DOM-Unterstützung jedes Browsers nutzen. Dazu müssten Sie Ihren eigenen Wrapper erstellen, der Grund dafür sind die Unterschiede zwischen den Browsern. Sie können an http://dotnetcaffe.blogspot.com

Mit freundlichen Grüßen

+0

Danke für den Link! Ich musste es aufgeben, eine Bibliothek zu finden und implementierte einen einfachen browserspezifischen Wrapper für natives DOM. Wird versuchen, deine zu benutzen. –

0

FormFaces (XForms Implementierung in JS) eine zuverlässige XPath Engine einen Blick darauf werfen, die easly extrahiert und verwendet werden können unabhängig.

0

Ich denke, dass Sie Cameron McCormack's xpath library hier verwenden können. Es funktioniert perfekt für mich.

4

Dies ist, was ich

// xpath.js 
// ------------------------------------------------------------------ 
// 
// a cross-browser xpath class. 
// Derived form code at http://jmvidal.cse.sc.edu/talks/javascriptxml/xpathexample.html. 
// 
// Tested in Chrome, IE9, and FF6.0.2 
// 
// Author  : Dino 
// Created : Sun Sep 18 18:39:58 2011 
// Last-saved : <2011-September-19 15:07:20> 
// 
// ------------------------------------------------------------------ 

/*jshint browser:true */ 

(function(globalScope) { 
    'use strict'; 

    /** 
    * The first argument to this constructor is the text of the XPath expression. 
    * 
    * If the expression uses any XML namespaces, the second argument must 
    * be a JavaScript object that maps namespace prefixes to the URLs that define 
    * those namespaces. The properties of this object are taken as prefixes, and 
    * the values associated to those properties are the URLs. 
    * 
    * There's no way to specify a non-null default XML namespace. You need to use 
    * prefixes in order to reference a non-null namespace in a query. 
    * 
    */ 

    var expr = function(xpathText, namespaces) { 
     var prefix; 
     this.xpathText = xpathText; // Save the text of the expression 
     this.namespaces = namespaces || null; // And the namespace mapping 

     if (document.createExpression) { 
      this.xpathExpr = true; 
      // I tried using a compiled xpath expression, it worked on Chrome, 
      // but it did not work on FF6.0.2. Threw various exceptions. 
      // So I punt on "compiling" the xpath and just evaluate it. 
      // 
      // This flag serves only to store the result of the check. 
      // 

       // document.createExpression(xpathText, 
       // // This function is passed a 
       // // namespace prefix and returns the URL. 
       // function(prefix) { 
       //  return namespaces[prefix]; 
       // }); 
     } 
     else { 
      // assume IE and convert the namespaces object into the 
      // textual form that IE requires. 
      this.namespaceString = ""; 
      if (namespaces !== null) { 
       for(prefix in namespaces) { 
        // Add a space if there is already something there 
        if (this.namespaceString.length>1) this.namespaceString += ' '; 
        // And add the namespace 
        this.namespaceString += 'xmlns:' + prefix + '="' + 
         namespaces[prefix] + '"'; 
       } 
      } 
     } 
    }; 

    /** 
    * This is the getNodes() method of XPath.Expression. It evaluates the 
    * XPath expression in the specified context. The context argument should 
    * be a Document or Element object. The return value is an array 
    * or array-like object containing the nodes that match the expression. 
    */ 
    expr.prototype.getNodes = function(xmlDomCtx) { 
     var self = this, a, i, 
      doc = xmlDomCtx.ownerDocument; 

     // If the context doesn't have ownerDocument, it is the Document 
     if (doc === null) doc = xmlDomCtx; 

     if (this.xpathExpr) { 
      // could not get a compiled XPathExpression to work in FF6 
      // var result = this.xpathExpr.evaluate(xmlDomCtx, 
      //  // This is the result type we want 
      //  XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, 
      //  null); 

      var result = doc.evaluate(this.xpathText, 
       xmlDomCtx, 
       function(prefix) { 
        return self.namespaces[prefix]; 
       }, 
       XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, 
       null); 

      // Copy the results into an array. 
      a = []; 
      for(i = 0; i < result.snapshotLength; i++) { 
       a.push(result.snapshotItem(i)); 
      } 
      return a; 
     } 
     else { 
      // evaluate the expression using the IE API. 
      try { 
       // This is IE-specific magic to specify prefix-to-URL mapping 
       doc.setProperty("SelectionLanguage", "XPath"); 
       doc.setProperty("SelectionNamespaces", this.namespaceString); 

       // In IE, the context must be an Element not a Document, 
       // so if context is a document, use documentElement instead 
       if (xmlDomCtx === doc) xmlDomCtx = doc.documentElement; 
       // Now use the IE method selectNodes() to evaluate the expression 
       return xmlDomCtx.selectNodes(this.xpathText); 
      } 
      catch(e2) { 
       throw "XPath is not supported by this browser."; 
      } 
     } 
    }; 


    /** 
    * This is the getNode() method of XPath.Expression. It evaluates the 
    * XPath expression in the specified context and returns a single matching 
    * node (or null if no node matches). If more than one node matches, 
    * this method returns the first one in the document. 
    * The implementation differs from getNodes() only in the return type. 
    */ 
    expr.prototype.getNode = function(xmlDomCtx) { 
     var self = this, 
       doc = xmlDomCtx.ownerDocument; 
     if (doc === null) doc = xmlDomCtx; 
     if (this.xpathExpr) { 

      // could not get compiled "XPathExpression" to work in FF4 
      // var result = 
      //  this.xpathExpr.evaluate(xmlDomCtx, 
      //  // We just want the first match 
      //  XPathResult.FIRST_ORDERED_NODE_TYPE, 
      //  null); 

      var result = doc.evaluate(this.xpathText, 
       xmlDomCtx, 
       function(prefix) { 
        return self.namespaces[prefix]; 
       }, 
       XPathResult.FIRST_ORDERED_NODE_TYPE, 
       null); 
      return result.singleNodeValue; 
     } 
     else { 
      try { 
       doc.setProperty("SelectionLanguage", "XPath"); 
       doc.setProperty("SelectionNamespaces", this.namespaceString); 
       if (xmlDomCtx == doc) xmlDomCtx = doc.documentElement; 
       return xmlDomCtx.selectSingleNode(this.xpathText); 
      } 
      catch(e) { 
       throw "XPath is not supported by this browser."; 
      } 
     } 
    }; 


    var getNodes = function(context, xpathExpr, namespaces) { 
     return (new globalScope.XPath.Expression(xpathExpr, namespaces)).getNodes(context); 
    }; 

    var getNode = function(context, xpathExpr, namespaces) { 
     return (new globalScope.XPath.Expression(xpathExpr, namespaces)).getNode(context); 
    }; 


    /** 
    * XPath is a global object, containing three members. The 
    * Expression member is a class modelling an Xpath expression. Use 
    * it like this: 
    * 
    * var xpath1 = new XPath.Expression("/kml/Document/Folder"); 
    * var nodeList = xpath1.getNodes(xmldoc); 
    * 
    * var xpath2 = new XPath.Expression("/a:kml/a:Document", 
    *         { a : 'http://www.opengis.net/kml/2.2' }); 
    * var node = xpath2.getNode(xmldoc); 
    * 
    * The getNodes() and getNode() methods are just utility methods for 
    * one-time use. Example: 
    * 
    * var oneNode = XPath.getNode(xmldoc, '/root/favorites'); 
    * 
    * var nodeList = XPath.getNodes(xmldoc, '/x:derp/x:twap', { x: 'urn:0190djksj-xx'}); 
    * 
    */ 

    // place XPath into the global scope. 
    globalScope.XPath = { 
     Expression : expr, 
     getNodes : getNodes, 
     getNode : getNode 
    }; 

}(this)); 
+0

Sie sollten dies auf Sourceforge oder ähnliches setzen. –

+0

Heutzutage schlägt dies für IE10 fehl. –

0

Die andere Option verwenden könnte (wenn auch ein wenig alt zu sein scheint)

2

Hier ist die jüngste Cross-Browser-Implementierung von XPath in Javascript: https://github.com/andrejpavlovic/xpathjs

Es ist voll funktionsfähig und Unit-getestet und hat großen Support. Der coolste Teil ist, dass es auch Namespaces unterstützt!

Verwandte Themen