我不認爲默認的瀏覽器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.0和Saxon-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 XSLT。XSLTProcessor
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的一種方法。
您是否考慮過使用SaxonCE - 它支持在瀏覽器中執行XSLT 2.0(並且可以在所有五種主流瀏覽器(包括Chrome)上成功運行)? –
如果他們暴露了一個相當於他們的XSLT20Processor的JavaScript XPath20Evaluator,那真是太棒了。不幸的是,他們還沒有提供這種選擇。謝謝你的提示,但。很高興知道那是在那裏。 – underrun
你不需要等待「他們」。只需拿到你的SaxonCE許可證並使用它。 –