简体   繁体   English

在带有 javascript 的 HTML 中运行 XPath 表达式 SVG

[英]running an XPath expression SVG inside an HTML with javascript

I'm trying to run an xpath-expression over an svg which is embedded in html.我正在尝试在嵌入在 html 中的 svg 上运行 xpath 表达式。 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:我使用以下 xpath 表达式:

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...问候...

Here's the Mozilla tutorial on using XPath:https://developer.mozilla.org/en/Introduction_to_using_XPath_in_JavaScript这是关于使用 XPath 的 Mozilla 教程: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这是关于编写自定义命名空间解析器的一个: 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.不过,我发现这些接口相当笨拙,所以我编写了一个抽象层,它将采用 xpath 字符串和一个上下文节点,并返回一个常规的 js 数组。 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 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-9 外,以上所有内容都应该适用于所有浏览器。

IE6-8 does not support SVG natively, so this should be less important to your question. IE6-8 本身不支持 SVG,所以这对你的问题应该不太重要。 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/不过,为了完整起见,这里有一篇很好的文章描述了早期 IE8 中的 XPath 支持,包括对解析命名空间的支持: 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.显然,IE9 也不包括在浏览器中对 XPath 的支持,这更有问题,因为它确实支持 SVG。 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?这里最好的方法可能是使用 ActiveX 来处理 MSXML API: IE9 selectSingleNode 从 beta 中丢失,如何在 JavaScript 中克服这个问题?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM