简体   繁体   English

使用 javascript 中的 xPath 使用默认命名空间解析 XML

[英]Parsing XML with default namespace using xPath in javascript

I need to create an XML xPath parser.我需要创建一个 XML xPath 解析器。 All parsing has to happen on client side (using javascript).所有解析都必须发生在客户端(使用 javascript)。 I created an javascript that does this, and everything looks OK until default namespaces come into play.我创建了一个 javascript 来执行此操作,在默认命名空间发挥作用之前,一切看起来都很好。 I simply can't query XML that has default namespace.我根本无法查询具有默认命名空间的 XML。

I created an example code on fiddle.我在小提琴上创建了一个示例代码。 In xmlString is XML string received from server.在 xmlString 中是从服务器接收到的 XML 字符串。 In xPathString is query done on received XML.在 xPathString 中是对收到的 XML 进行的查询。

Here are some scenarios:以下是一些场景:

  1. http://jsfiddle.net/BF34q/1/ - no namespaces - everything works OK http://jsfiddle.net/BF34q/1/ - 没有命名空间 - 一切正常
  2. http://jsfiddle.net/BF34q/2/ - ns namespace added. http://jsfiddle.net/BF34q/2/ - 添加了 ns 命名空间。 element has ns: prefix.元素有 ns: 前缀。 xPath uses this prefix - OK xPath 使用这个前缀 - OK
  3. http://jsfiddle.net/BF34q/3/ - default namespace used - not sure how to configure xPathString. http://jsfiddle.net/BF34q/3/ - 使用默认命名空间 - 不确定如何配置 xPathString。

Note that others will use this parser, so I would really like to avoid solutions like请注意,其他人会使用这个解析器,所以我真的很想避免像这样的解决方案

var xPathString = "//*[local-name()='book']";

and enable them to parse it using simple xPath expressions.并使他们能够使用简单的 xPath 表达式对其进行解析。 I wonder if it is possible to assign default namespace prefix in javascript?我想知道是否可以在 javascript 中分配默认命名空间前缀?

Note: The example provided on fiddle will not work in IE.注意:小提琴上提供的示例在 IE 中不起作用。

I think there are three ways to do this:我认为有三种方法可以做到这一点:

  1. Use //*[local-name()='book'] syntax for accessing nodes使用//*[local-name()='book']语法访问节点
  2. Convert XML to string , remove default namespace using RegExp, convert it back to XML将 XML 转换为 string ,使用 RegExp 删除默认命名空间,将其转换回 XML
  3. For XML files where you know namespaces in advance, you can create your own namespace resolver , which will allow you to use your own prefix for default namespace.对于预先知道命名空间的 XML 文件,您可以创建自己的命名空间解析器,这将允许您使用自己的前缀作为默认命名空间。

This can be achieved like this:这可以这样实现:

function nsResolver(prefix) {
    switch (prefix) {
        case 'xhtml':
            return 'http://www.w3.org/1999/xhtml';
        case 'mathml':
            return 'http://www.w3.org/1998/Math/MathML';
        default:
            return 'http://example.com/domain';
    }
}
xml.evaluate('//myPrefix:book', xml, nsResolver, XPathResult.ANY_TYPE, null);

I've got the impression that your understanding of the XPath processing doesn't match the implementation - unless, that is, the implementation you're dealing with is very different from the ones I'm familiar with.我的印象是,您对 XPath 处理的理解与实现不匹配——除非,也就是说,您正在处理的实现与我熟悉的实现非常不同。

Usually, the XPath processor has to have namespaces registered and prefixes mapped to them in order for the expression to be successfully evaluated.通常,XPath 处理器必须注册命名空间并将前缀映射到它们,才能成功评估表达式。 So the prefixes could be anything - the only thing that matters is what they're mapped to.所以前缀可以是任何东西——唯一重要的是它们映射到什么。 See this answer by a known expert to get more information.请参阅知名专家的此答案以获取更多信息。

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

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