當文檔頂部存在 時,我在使用JScript的Windows Script Host中評估XPath表達式時遇到問題。當我刪除DOCTYPE解析不中斷。這裏是我用來分析和加載xml的代碼示例。有沒有辦法解析這種類型的XML,除去DOCTYPE聲明。如何在DOCTYPE存在於XML聲明中時評估Xpath
我不想在處理時刪除DOCTYPE。解決方案可能是使用正則表達式刪除DOCTYPE <!DOCTYPE.*?>/gm;
但我不想要那樣的解決方案。
var XmlDocument;
try {
XmlDocument = new ActiveXObject("msxml2.DOMDocument.6.0");
XmlDocument.async = false;
XmlDocument.resolveExternals = false;
XmlDocument.validateOnParse = false;
} catch(e) {
if(debug == true)
WScript.echo("***ERROR while creating DOM Object: " + e.description);
}
// Load an XML file into the DOM instance
try {
XmlDocument.load(filePath);
} catch(e) {
if(debug == true)
WScript.echo("***ERROR LOADING FILE: " + e.description);
}
var Node;
try {
Node = XmlDocument.selectSingleNode("//metadata/url");
} catch(e) {
if(debug == true)
WScript.echo("***ERROR RESOLVING NODE: " + e.description);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE rightinclude SYSTEM "http://localhost/iw/aa.dtd">
<a>
<b>
<c>/someurl.html</c>
</b>
</a>
你說得對。當我關閉DOCTYPE標記時,解析器不會中斷。對於這種情況,沒有一些設置或指示。 – svlada