2012-05-08 50 views
3

我想在Web應用程序中使用JavaScript XPaths使用exslt擴展名,但我無法弄清楚如何做到這一點。在JavaScript中使用exslt擴展xpaths

假裝我有一個html文檔,裏面有一些div。我想運行這個:

namespaces={'regexp':'http://exslt.org/regular-expressions'}; 
result = document.evaluate( 
      "//div[regexp:test(.,'$')]", 
      document, 
      function(ns){ 
       return namespaces.hasOwnProperty(ns) ? namespaces[ns] : null; 
      }, 
      XPathResult.ANY_TYPE, 
      null); 

只會導致計算中出現無效的XPath表達式異常。我正在使用chrome。

有什麼我需要做的,使這個東西的工作?我在exslt.org上看到有JavaScript的實現,但我如何確保這些實現可用?我是否需要將我的javascript插入到dom中的命名空間腳本元素或瘋狂的東西?

UPDATE

如果這是不可能直接使用瀏覽器的DOM +的JavaScript和XPath,纔有可能使用EXSLT擴展在瀏覽器中,以模擬document.evaluate(返回元素的列表寫XSLT匹配xpath)?

+0

您是否考慮過使用SaxonCE - 它支持在瀏覽器中執行XSLT 2.0(並且可以在所有五種主流瀏覽器(包括Chrome)上成功運行)? –

+0

如果他們暴露了一個相當於他們的XSLT20Processor的JavaScript XPath20Evaluator,那真是太棒了。不幸的是,他們還沒有提供這種選擇。謝謝你的提示,但。很高興知道那是在那裏。 – underrun

+0

你不需要等待「他們」。只需拿到你的SaxonCE許可證並使用它。 –

回答

1

我不認爲默認的瀏覽器XPath實現支持EXSLT。在EXSLT頁面上提到的JavaScript支持很可能是關於如何使用in-browser.javascript提供您自己的exslt函數實現。這裏是one example I was able to find very quickly。例如,您可以在Firefox中使用Saxon-B as an extension to run XSLT2.0Saxon-B has built-in support for exslt (unlike Saxon-HE), though you will likely be better off just using XSLT/XPath 2.0 features。例如,這裏是regular expression syntax。不過,依靠Mozilla Saxon-B的擴展並不能幫助你使用Chrome或其他瀏覽器。

這樣說我不認爲你可以找到一個跨瀏覽器的解決方案來在你的XPath中使用EXSLT擴展。 DOM Level 3 XPath的conformance section要求支持XPath 1.0,並且沒有提及EXSLT。該INVALID_EXPRESSION_ERR被說成是拋出:

if the expression has a syntax error or otherwise is not a legal expression according to the rules of the specific XPathEvaluator or contains specialized extension functions or variables not supported by this implementation.

最後,這裏是一個open bugzilla ticket爲Firefox開拓EXSLT支持他們的DOM Level 3的XPath實現。它似乎從2007年開始就坐在那裏。票證說:

Currently Mozilla gives an exception "The expression is not a legal expression." even if a namespace resolver correctly resolving the EXSLT prefixes to the corresponding URLs is passed in。這裏是test case

-

如果你不介意我問,究竟你想使用正則表達式?也許我們可以幫助您擺脫standard XPath string functions的組合?

-

UPDATE您可以通過XSLT建立一個XPath亞軍(就像你在更新問你的問題),但它不會從源文檔返回的節點,它會返回看起來完全相同的新節點。 XSLT生成一個新的結果樹文檔,我不認爲有辦法讓它返回對原始節點的引用。

據我所知,Mozilla(和Chrome)不僅對於從外部來源加載的XML文檔,還對來自正在顯示的文檔的DOM元素的support XSLTXSLTProcessor documentation提到如何tranformToFragment(),例如will only produce HTML DOM objects if the owner document is itself an HTMLDocument, or if the output method of the stylesheet is HTML

這裏有一個簡單的XPath亞軍我建立測試出你的IDE:

1)首先你需要一個XSLT模板的工作。

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:regexp="http://exslt.org/regular-expressions" 
    extension-element-prefixes="regexp"> 

    <xsl:template match="/"> 
     <xsl:copy-of select="."/> 
    </xsl:template> 
</xsl:stylesheet> 

我開始使用document.implementation.createDocument API來建立它在JavaScript,但想通這將是容易,只需加載它。 FF仍支持document.load,而Chrome只允許您使用XHR加載內容。如果您想從本地磁盤加載XHR文件,則需要使用--allow-file-access-from-files啓動Chrome。

2)一旦我們的模板裝,我們就需要修改xsl:copy-of指令select屬性的值來運行我們需要的XPath:

function runXPath(xpath) { 
    var processor = new XSLTProcessor(); 
    var xsltns = 'http://www.w3.org/1999/XSL/Transform'; 

    var xmlhttp = new window.XMLHttpRequest(); 
    xmlhttp.open("GET", "xpathrunner.xslt", false); 
    xmlhttp.send(null); 

    var transform = xmlhttp.responseXML.documentElement; 

    var copyof = transform.getElementsByTagNameNS(xsltns, 'copy-of')[0]; 
    copyof.setAttribute('select', xpath); 

    processor.importStylesheet(transform);   

    var body = document.getElementById('body'); // I gave my <body> an id attribute 
    return processor.transformToFragment(body, document); 
} 

現在,您可以像運行:

var nodes = runXPath('//div[@id]'); 
console.log(nodes.hasChildNodes()); 
if (nodes.firstChild) { 
    console.log(nodes.firstChild.localName); 
} 

它像//div[@id]「正規」的XPath的偉大工程(並且沒有找到//div[@not-there]但我不能讓它運行regexp:test擴展功能。與//div[regexp:test(string(@id), "a")]它不會出錯,只是返回空集。

Mozilla文檔建議their XSLT processor support EXSLT。無論如何,我想他們都是在幕後使用libxml/libxslt。也就是說,我也無法在Mozilla中使用它。

希望它有幫助。

任何機會,你可以脫身jQuery regexp?不太可能對您的XPath構建器實用程序有所幫​​助,但仍然是在HTML節點上運行regexp的一種方法。

+0

那麼前景是嚴峻的。我也只看着支持鉻。目的不是創建特定的正則表達式過濾xpath,而是在html/css/javascript中創建一個xpath創建器,就像chrome擴展XPath Helper一樣(請參閱https://chrome.google.com/webstore/detail/hgimnogjllphhhkhlmebbmlgjoejdpjl) 。 – underrun

+0

可能會在他們的wiki /郵件列表上詢問。我搜索了wiki和問題的EXSLT,我發現這一個:http://code.google.com/p/chromium/issues/detail?id=111655&q=EXSLT&colspec=ID%20Pri%20Mstone%20ReleaseBlock%20Area%20Feature% 20Status%20Owner%20Summary。他們必須對EXSLT有一些支持,但我不確定它是否暴露給DOM Level 3 XPath。瞭解Mozilla在DOM Xpath中沒有它,儘管它們似乎在XSLT中也有它,但我認爲Chrome也不會。 –

+0

@SynapticUnderrun我發佈了一個更新,探索基於XSLT的XPath跑步者的想法。它的工作原理,但沒有運行'regexp:'擴展功能。 –