我想在一个使用exslt扩展的web应用程序中使用javascript XPaths,但我不知道如何做到这一点。
假装我有个带有div的html文档。我想做这个:
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表达式异常。我在用铬。
我还需要做些什么才能让这些东西发挥作用吗?我在exslt.org上看到了javascript的实现,但是如何确保它们是可用的呢?我需要将我的javascript插入到dom中的名称空间脚本元素中吗?
更新
如果不能直接使用browser dom + javascript和xpath,那么是否可以在浏览器中使用exslt扩展编写XSLT来模拟document.evaluate (返回与xpath匹配的元素列表)?
发布于 2012-05-14 18:00:21
我不认为默认的浏览器XPath实现支持EXSLT。EXSLT页面中提到的javascript支持很可能是关于如何使用in-browser.javascript提供您自己的exslt函数实现。这是有一个例子我很快就找到了。
例如,在火狐中,您可以使用Saxon作为运行XSLT2.0的扩展和Saxon已经内置了对exslt的支持(与Saxon不同),尽管您可能会更好地使用XSLT/XPath 2.0特性。。举个例子,这是正则表达式语法。不过,尽管如此,依靠Mozilla扩展并不能帮助你使用Chrome或其他浏览器。
尽管如此,我认为您无法在XPath中找到跨浏览器的解决方案来使用EXSLT扩展。DOM级别3的符合性部分调用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.
最后,这里有一个用于火狐的开放bugzilla票,用于打开对其DOM级别3 XPath实现的EXSLT支持。自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。这是测试用例。
--
如果你不介意我问,你到底想用正则表达式做什么?也许我们可以帮你把标准XPath字符串函数组合起来
--
update 您可以通过XSLT构建XPath运行程序(就像您在更新时问您的问题一样),但是它不会从源文档返回节点,它将返回与完全相同的新节点。XSLT生成一个新的结果树文档,我认为没有办法让它返回对原始节点的引用。
据我所知,Mozilla (和Chrome)不仅适用于从外部源加载的支持XSLT文档,也适用于显示的文档中的DOM元素。文档提到了如何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。
下面是我在ides中构建的一个简单的XPath运行程序:
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>我开始在JavaScript中使用document.implementation.createDocument APi构建它,但我认为只加载它会更容易。FF仍然支持document.load,而Chrome只允许您使用XHR加载内容。如果要从本地磁盘加载XHR文件,则需要使用--allow-file-access-from-files启动Chrome。
2)一旦加载了模板,就需要修改xsl:copy-of指令的xsl:copy-of属性的值,以运行所需的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);
}它对“常规”XPath非常有用,比如//div[@id] (并且找不到//div[@not-there]) ,但是我无法让它运行扩展函数。对于//div[regexp:test(string(@id), "a")],它不会出错,只返回空集。
Mozilla文档建议使用他们的XSLT处理器支持EXSLT。。我可以想象他们都在幕后使用libxml/libxslt。话虽如此,我也无法让它在Mozilla中工作。
希望能帮上忙。
你有机会和jQuery正则表达式一起逃脱吗?不太可能帮助您的XPath构建器实用程序,但仍然是在HTML节点上运行regexp的一种方法。
https://stackoverflow.com/questions/10508048
复制相似问题