javascript 带有 dom 文档的 xpath

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3596578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:39:09  来源:igfitidea点击:

xpath with dom document

javascriptxmldomxpathdocument.evaluate

提问by ertan

I'm trying the find a xml node with xpath query. but i cannot make it working. In firefox result is always "undefined" and chrome throws a error code.

我正在尝试使用 xpath 查询查找 xml 节点。但我不能让它工作。在 Firefox 中,结果总是“未定义”,chrome 会抛出错误代码。

<script type="text/javascript">

var xmlString = '<form><name>test</name></form>';
var doc = new DOMParser().parseFromString(xmlString,'text/xml');

var result = doc.evaluate('/form/name', doc, 
                          null, XPathResult.ANY_TYPE, null);

alert(result.stringValue);

</script>

what's wrong with this code ?

这段代码有什么问题?

采纳答案by Topera

I don't know why did you get this error, but you can change XPathResult.ANY_TYPEto XPathResult.STRING_TYPEand will works (tested in firefox 3.6).

我不知道为什么会出现此错误,但是您可以更改XPathResult.ANY_TYPEXPathResult.STRING_TYPE并且可以正常工作(在 Firefox 3.6 中测试)。

See:

看:

var xmlString = '<form><name>test</name></form>';
var doc = new DOMParser().parseFromString(xmlString,'text/xml');
var result = doc.evaluate('/form/name', doc, null, XPathResult.STRING_TYPE, null);
alert(result.stringValue); // returns 'test'

See in jsfiddle.

请参阅jsfiddle



DETAILS:

细节:

The 4th parameter of method evaluateis a integer where you specify what kind of result do you need (reference). There are many types, as integer, string and any type. This method returns a XPathResult, that has many properties.

方法的第四个参数evaluate是一个整数,您可以在其中指定您需要什么样的结果(参考)。有很多类型,如整数、字符串和任何类型。此方法返回一个XPathResult,它具有许多属性。

You must match the property (numberValue, stringValue) with the property used in evaluate.

您必须将属性 (numberValue, stringValue) 与评估中使用的属性匹配。

I just don't understand why any typedidn't work with string value.

我只是不明白为什么any type不使用string value.

回答by Aurimas

XPathResult.ANY_TYPEwould return a node set for xpath expression /form/name, so result.stringValuewould have trouble converting node set to string. In this case you could use result.iterateNext().textContent

XPathResult.ANY_TYPE将为 xpath expression 返回一个节点集/form/name,因此result.stringValue将节点集转换为字符串时会遇到问题。在这种情况下,您可以使用result.iterateNext().textContent

However, an expression like count(/form/name)would return a number value when used with XPathResult.ANY_TYPEand you could use result.numberValueto retrieve the number in that case.

但是,count(/form/name)当使用 with 时,like 表达式将返回一个数字值,在这种情况下XPathResult.ANY_TYPE,您可以使用它result.numberValue来检索数字。

Some more detailed explanation at https://developer.mozilla.org/en/DOM/document.evaluate#Result_types

https://developer.mozilla.org/en/DOM/document.evaluate#Result_types 上的一些更详细的解释