2008-10-08 13 views

Respuesta

10

Google acaba de lanzar Wicked Good XPath - Una reescritura del famoso JavaScript-XPath de Cybozu Lab.

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

La versión reescrita es 40% más pequeño y un 30% más rápido que la implementación original.

+0

esta es la opción correcta ahora en 2012. Lástima que no se puede desaprobar las respuestas –

+1

Tal vez, es una buena solución para documentos HTML. Pero existe un problema abierto para los documentos XML https://code.google.com/p/wicked-good-xpath/issues/detail?id=7 – dizel3d

+0

Tenga en cuenta que en el momento de la redacción, la descarga en el enlace dado no está disponible. no funciona La solución más rápida que encontré fue obtenerla de un paquete de NPM ([aquí] (https://github.com/jacobmarble/node-wgxpath)). – Kat

4

Puede usar el XPath plug-in for jQuery básico para obtener la funcionalidad de consulta de XPath.

Además, usted podría considerar la lectura de este article on XPath XML processing (de nuevo con jQuery)

+1

Gracias! He echado un vistazo a ambos. El complemento XPath para jQuery simplemente convierte la expresión XPath en la expresión de selección jQuery. No funciona incluso para casos simples. –

+0

¿Funciona esto en absoluto con jquery> 1.2? –

+0

A partir de hoy, estas libraties no están disponibles. –

3

Tome un vistazo a http://dev.abiss.gr/sarissa/ proyecto. Han migrado la mayoría de las API relacionadas con XML a IE, así como también han evaluado el método en el objeto del documento. Y, de hecho, jQuery no tiene un procesador XPath, tiene un selector de ruta muy simple como:/a/b/c solo

0

No creo que permita consultas ad-hoc, pero puede echar un vistazo a XSLT jQuery plug-in de Johann Burkard para obtener inspiración sobre cómo implementar consultas XPath. Lo uso en mi widget jQuery Reference Dashboard y es realmente sólido.

5

Google's AJAXSLT proyecto de código abierto se ajusta bien a los requisitos establecidos.

Como su propia descripción va a decir:

"AJAXSLT es una implementación de XSLT en JavaScript Debido XSLT utiliza XPath, también es una implementación de XPath que se puede utilizar de forma independiente de XSLT.. Esta implementación tiene la ventaja de que hace que XSLT esté uniformemente disponible en más navegadores que lo proporciona de forma nativa, y que se puede extender a más navegadores si es necesario. AJAXSLT es interesante para los desarrolladores que luchan agresivamente por la compatibilidad entre navegadores de su web avanzada aplicaciones. "

ACTUALIZACIÓN: A finales de 2010 Michael Kay ha estado compilando su procesador Saxon XSLT 2.0 a Javascript (por lo que está disponible para los 5 principales navegadores) utilizando GWT. Es probable que haya un sajón ligero dentro del navegador pronto.

+0

A partir de noviembre de 2011, la edición para clientes (CE) de Kay's Saxon todavía está en alfa. –

+0

@ james.garriss: esto no significa que no se use. Hay hilos interesantes en la lista xsl sobre este tema. Sospecho que el principal problema de Saxonica no es tecnológico, sino más bien para encontrar el modelo de negocio correcto para Saxon CE, para que pueda ser rentable. –

+0

La página enlazada ahora tiene lo siguiente que decir: * "El mundo del navegador y las aplicaciones web ha cambiado desde que se implementó esta biblioteca en 2005. Muchos navegadores ahora implementan XSLT y XPath de forma nativa y, lo que es más importante, usan menos aplicaciones web. XML como su formato de datos de transferencia, y pasó al JSON más apropiado y pragmático. Una forma más simple y flexible de vincular datos a HTML en el lado del navegador para su visualización en aplicaciones web es google-jstemplate. "* –

0

Puede hacer uso del soporte de DOM nativo existente de cada navegador. Para esto, tendrías que crear tu propio contenedor, la razón es las diferencias entre el navegador. Puede echar un vistazo a http://dotnetcaffe.blogspot.com

Saludos

+0

¡Gracias por el enlace! Tuve que dejar de buscar una biblioteca e implementé un contenedor simple específico del navegador para DOM nativo. Trataremos de usar el tuyo –

0

FormFaces (implementación de XForms en JS) tiene un motor de XPath fiable que puede ser easly extraído y utilizado de forma independiente.

0

Creo que puede utilizar xpath library de Cameron McCormack aquí. Funciona perfectamente para mí.

4

Esto es lo que yo uso

// 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

Debe poner esto en Sourceforge o tal. –

+0

Hoy en día esto falla para IE10. –

0

La otra opción podría ser (aunque parece ser un poco viejo)

2

Esta es la más reciente aplicación multi-navegador de XPath en Javascript: https://github.com/andrejpavlovic/xpathjs

Es totalmente funcional y probado en unidades, y tiene un gran soporte. La mejor parte es que también admite espacios de nombres.

Cuestiones relacionadas