使用 javascript 在 HTML 中运行 XPath 表达式 SVG
我正在尝试在嵌入 html 的 svg 上运行 xpath 表达式。我只是不知道如何设置参数。我想从给定的命名空间中查找具有任意属性的元素。我使用以下 xpath 表达式:
var xpathexp = "//*[@*[namespace-uri()='"+this.typo7namespace+"']]";
我测试了这个表达式,它按预期工作。 这是查找结果集的代码:
var result = this.svgdocument.contentDocument.evaluate( xpathexp, this.svgdocument.documentElement, null, XPathResult.ANY_TYPE, null );
有人可以告诉我,或者发布教程的链接,如何处理命名空间、命名空间解析器?
问候...
I'm trying to run an xpath-expression over an svg which is embedded in html. I just cannot figure out how to set up the parameters. I want find elements that have an arbitary attribute from a given namespace. I use the following xpath expression:
var xpathexp = "//*[@*[namespace-uri()='"+this.typo7namespace+"']]";
I tested this expression and it worked as expected.
this is the code to find the result set:
var result = this.svgdocument.contentDocument.evaluate( xpathexp, this.svgdocument.documentElement, null, XPathResult.ANY_TYPE, null );
Could anybody tell me, or post a link to a tutorial, how to deal with the namspaces, the namespace resolvers??
Greetings...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是有关使用 XPath 的 Mozilla 教程:
https://developer.mozilla.org/en/Introduction_to_using_XPath_in_JavaScript
这是编写自定义命名空间解析器的一篇:
这些
我找到了 不过,接口相当笨重,所以我编写了一个抽象层,它将接受 xpath 字符串和上下文节点,并返回一个常规的 js 数组。它在浏览器中运行,并嵌入在 Mozilla Rhino 下的 Java 中:
https://svn.apache.org/repos/asf/commons/sandbox/gsoc/2010/scxml-js/trunk/src/javascript/scxml/cgf/util/xpath.js
全部上述内容应该适用于除 IE6-9 之外的所有浏览器。
IE6-8 本身不支持 SVG,所以这对你的问题来说应该不那么重要。不过,为了完整起见,这里有一篇很好的文章,描述了早期 IE8 中的 XPath 支持,包括对解析命名空间的支持:
http://www.nczonline.net /blog/2009/04/04/xpath-in-javascript-part-3/
显然,IE9 浏览器也不支持 XPath,这会带来更大的问题,因为它本身就支持 SVG。也许最好的方法是使用 ActiveX 来处理 MSXML API:
IE9 selectSingleNode 在测试版中缺失,如何解决这个问题在 JavaScript 中?
Here's the Mozilla tutorial on using XPath:
https://developer.mozilla.org/en/Introduction_to_using_XPath_in_JavaScript
Here's one on writing custom namespace resolvers:
https://developer.mozilla.org/en/Introduction_to_using_XPath_in_JavaScript#Implementing_a_User_Defined_Namespace_Resolver
I found these interfaces to be rather clunky, though, so I wrote an abstraction layer that would take the xpath string and a context node, and would return a regular js array. It works inside the browser and embedded in Java under Mozilla Rhino:
https://svn.apache.org/repos/asf/commons/sandbox/gsoc/2010/scxml-js/trunk/src/javascript/scxml/cgf/util/xpath.js
All of the above should work in all browsers except for IE6-9.
IE6-8 does not support SVG natively, so this should be less important to your question. For completeness, though, here's a good article describing XPath support in earlier IE8, including support for resolving namespaces:
http://www.nczonline.net/blog/2009/04/04/xpath-in-javascript-part-3/
Apparently, IE9 also does not include support for XPath in the browser, which is more problematic, as it does support SVG natively. Probably the best approach here is to use ActiveX to work with MSXML APIs:
IE9 selectSingleNode missing from beta, how to overcome this in JavaScript?