admin管理员组文章数量:1293755
I need to create an XML xPath parser. All parsing has to happen on client side (using javascript). I created an javascript that does this, and everything looks OK until default namespaces e into play. I simply can't query XML that has default namespace.
I created an example code on fiddle. In xmlString is XML string received from server. In xPathString is query done on received XML.
Here are some scenarios:
- / - no namespaces - everything works OK
- / - ns namespace added. element has ns: prefix. xPath uses this prefix - OK
- / - default namespace used - not sure how to configure 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. I wonder if it is possible to assign default namespace prefix in javascript?
Note: The example provided on fiddle will not work in IE.
I need to create an XML xPath parser. All parsing has to happen on client side (using javascript). I created an javascript that does this, and everything looks OK until default namespaces e into play. I simply can't query XML that has default namespace.
I created an example code on fiddle. In xmlString is XML string received from server. In xPathString is query done on received XML.
Here are some scenarios:
- http://jsfiddle/BF34q/1/ - no namespaces - everything works OK
- http://jsfiddle/BF34q/2/ - ns namespace added. element has ns: prefix. xPath uses this prefix - OK
- http://jsfiddle/BF34q/3/ - default namespace used - not sure how to configure 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. I wonder if it is possible to assign default namespace prefix in javascript?
Note: The example provided on fiddle will not work in IE.
Share asked Jul 20, 2011 at 11:58 dugokontovdugokontov 4,6221 gold badge27 silver badges26 bronze badges2 Answers
Reset to default 10I think there are three ways to do this:
- Use
//*[local-name()='book']
syntax for accessing nodes - Convert XML to string, remove default namespace using RegExp, convert it back to XML
- 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.
This can be achieved like this:
function nsResolver(prefix) {
switch (prefix) {
case 'xhtml':
return 'http://www.w3/1999/xhtml';
case 'mathml':
return 'http://www.w3/1998/Math/MathML';
default:
return 'http://example./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.
Usually, the XPath processor has to have namespaces registered and prefixes mapped to them in order for the expression to be successfully evaluated. 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.
本文标签: Parsing XML with default namespace using xPath in javascriptStack Overflow
版权声明:本文标题:Parsing XML with default namespace using xPath in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741587297a2386917.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论