Delphi / MSXML:XPath查询失败
我已经加载了一个XML文档,现在我希望运行一个XPath查询来选择XML的某个子集. XML是
<?xml version="1.0"?> <catalog xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'> <book id="bk101"> <author>Gambardella,Matthew</author> <title>XML Developer's Guide</title> <genre>Computer</genre> <price>44.95</price> <publish_date>2000-10-01</publish_date> <description>An in-depth look at creating applications with XML.</description> </book> </catalog> 而程序就是这样的 procedure RunXPathQuery(XML: IXMLDOMDocument2; Query: string); begin XML.setProperty('SelectionLanguage','XPath'); NodeListResult := XML.documentElement.selectNodes(Query)); ShowMessage('Found (' + IntToStr(NodeListResult.length) + ') nodes.'); end; 问题是:当我为上述XML运行XPath查询’/ catalog’时,它返回(如预期的)1个元素的节点列表.但是,如果我删除:xsi 解决方法
见
this link!
当您使用< catalog xmlns ='http://www.w3.org/2001/XMLSchema-instance'u0026gt;然后整个节点将被移动到另一个(默认)命名空间.您的XPath不会查看此其他命名空间,因此无法找到任何数据.使用< catalog xmlns:xsi ='http://www.w3.org/2001/XMLSchema-instance'u0026gt;你只是将xsi声明为一个不同的命名空间.这将是与默认命名空间不同的命名空间. 我现在无法测试它,但添加一些东西like this可能会有所帮助: XML.setProperty('SelectionNamespaces','xmlns=''http://www.w3.org/2001/XMLSchema-instance'''); 或者它可能没有.正如我所说,我现在无法测试它. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |