当 XML 声明中存在 DOCTYPE 时如何评估 Xpath
我在使用 JScript 时在 Windows Script Host 中评估 XPath 表达式时遇到问题 存在于文档顶部。当我删除 DOCTYPE 时,解析不会中断。这是我用来解析和加载 xml 的代码示例。有没有办法在不删除 DOCTYPE 声明的情况下解析这种 XML。
我不想在处理时删除 DOCTYPE。解决方案可能是使用 Regex 删除 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>
I have problem with evaluation XPath expression in Windows Script Host using JScript when
is present on top of document. When I remove DOCTYPE parsing doesnt break. Here is sample of code that I am using to parse and load xml. Is there any way to parse this kind of XML witout removing DOCTYPE declaration.
I dont want to delete DOCTYPE while processing. Solution could be to remove DOCTYPE using Regex<!DOCTYPE.*?>/gm;
but I dont want solution like that.
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当 DOCTYPE 元素关闭时,您是否尝试过 - 在这种情况下,某些解析器将获取文档。
例如:
Have you tried it when the DOCTYPE element is closed - some parsers will take the document in this case.
e.g:
无需修改 XML 文件中的任何内容,而是使用这种方式来解析您的文件,一切都会顺利进行。
这是一个 链接,其中包含有关以下内容的描述和详细信息:
干杯;-)
No need to modify any thing in the XML File, instead use this manner to parse your file and everything would be good to go.
Here's a link with description and details regarding:
Cheers ;-)