【发布时间】:2021-03-30 15:29:15
【问题描述】:
无论出于何种原因,如果我输入源 XML,请评估它是否正确。但是,如果我使用样式表转换 XML 并且它是 DocumentFragment,它会转到警报(“否”)。有人知道会发生什么吗?也许需要在运行评估之前转换 DocumentFragment?但是为了什么?非常感谢。
doc_trans=xsltProcessor.transformToFragment(doc_xml_source, document);
//var resultDoc=doc_xnl_source;
var resultDoc=doc_trans;
//var path = "/catalog/cd/title"
path="/html/body/table//row";
if (resultDoc.evaluate) {
alert("yes");
var nodes = rDoc.evaluate(path, resultDoc, null, XPathResult.ANY_TYPE, null);
var result = nodes.iterateNext();
while (result) {
alert(result.childNodes[0].nodeValue);
txt += result.childNodes[0].nodeValue + "<br>";
result = nodes.iterateNext();
}
} else {
alert("no");
var evaluator = new XPathEvaluator();
//DocumentFragment not a valid node type exception thrown here...
var resulty = evaluator.evaluate("//tbody/tr", resultDoc, null,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE);
alert( resulty.snapshotLength);
}
【问题讨论】:
-
“成功了”。什么是“它”你用的是什么软件?这段代码在哪里运行?它是用什么语言编写的?
-
你好。 Javascript,Chrome 浏览器。实际上是通过 w3 学校 api 工作的。使用他们的“cdcatalog.xml”;和“cdcatalog_client.xsl”。我能够完成转换,但想解析转换后的 DocumentFragment。 document.evaluate 似乎只接受 HTML 和 XML(旧的 MSXML 允许在转换后的 XML 上使用 selectNodes)。
标签: xml document.evaluate