FF、IE、Opera、および Safari で XML ドキュメントを照会するための XPath ライブラリを探していますが、見つかりませんでした。見たことがありますか?
12 に答える
Google は、サイボウズ ラボの有名な JavaScript-XPath を書き直した Wicked Good XPath をリリースしました。
リンク: https://github.com/google/wicked-good-xpath
書き直されたバージョンは、元の実装よりも 40% 小さく、約 30% 高速です。
Google の AJAXSLTオープン ソース プロジェクトは、前述の要件を十分に満たしています。
彼ら自身の説明が言うように:
「AJAXSLT はJavaScriptでの XSLT の実装です。XSLT はXPath を使用するため、XSLTとは独立して使用できる XPath の実装でもあります。この実装には、XSLT をネイティブに提供するよりも多くのブラウザーで均一に利用できるようにするという利点があります。必要に応じて、さらに多くのブラウザーに拡張できることを示しています 。AJAXSLT は、高度な Web アプリケーションのクロスブラウザー互換性を積極的に追求している開発者にとって興味深いものです。」
更新: 2010 年末、Michael Kay は、GWT を使用して、Saxon XSLT 2.0 プロセッサを Javascript にコンパイルしました (したがって、5 つの主要なブラウザすべてで利用できるようになりました)。軽量のブラウザー内の Saxon が間もなく登場する可能性があります。
これは私が使用するものです
// 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));
jQuery用の基本的なXPath プラグインを使用して、 XPath クエリ機能を取得できます。
また、 XPath XML 処理(ここでも jQuery を使用)に関するこの記事を読むことを検討してください。
http://dev.abiss.gr/sarissa/プロジェクトをご覧ください。XML 関連の API のほとんどを IE に移行し、ドキュメント オブジェクトのメソッドを評価します。実際、jQuery には XPath プロセッサがなく、/a/b/c のみのような非常に単純なパス セレクタがあります。
Javascript での XPath の最新のクロスブラウザー実装は次のとおりです: https://github.com/andrejpavlovic/xpathjs
完全に機能し、単体テスト済みで、優れたサポートを提供します。最もクールな部分は、名前空間もサポートしていることです!
クロスブラウザーで機能するXPath2.0構文をサポートするjQueryXPathプラグインを試してみることをお勧めします。
アドホック クエリは許可されていないと思いますが、XPath クエリを実装する方法については、Johann Burkard のXSLT jQuery プラグインを参照してください。jQuery リファレンスダッシュボード ウィジェットで使用していますが、非常に安定しています。
FormFaces (JS での XForms 実装) には、簡単に抽出して個別に使用できる信頼性の高い XPath エンジンがあります。
他のオプションはLlamaLab XPath.jsかもしれません(少し古いようですが)
ここで Cameron McCormack のxpath ライブラリを使用できると思います。それは私にとって完全に機能しています。