Delphi / MSXML:XPath查询失败 [英] Delphi/MSXML: XPath queries fail

查看:142
本文介绍了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开发人员指南< / title>
< genre> Computer< / genre>
< price> 44.95< / price>
< publish_date> 2000-10-01< / publish_date>
< description>深入了解使用
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'时,它返回如预期)一个元素的一个元素。但是,如果我从
< catalog xmlns:xsi ='http://www.w3.org/2001中删除:xsi / XMLSchema-instance'> ,并重新运行查询,返回的模板器为空。如果我删除了整个xmlns属性,则生成的nodelist再次有1个元素。所以我的问题是这样的:我该如何解决这个问题,即如何让MSXML返回正确数量的实例(运行XPath查询时),不管的命名空间(或其他属性)?



谢谢!

解决方案

请参阅此链接



当您使用< catalog xmlns ='http://www.w3.org/2001/XMLSchema-instance'> 时,整个节点将被移动到一个不同的(默认)命名空间。你的XPath没有看到这个其他命名空间,所以它找不到任何数据。使用< catalog xmlns:xsi ='http://www.w3.org/2001/XMLSchema-instance'> 你只是将xsi声明为不同的命名空间。这将是与默认命名空间不同的命名空间。



我现在无法测试,但添加一些像这样可能有帮助:

  XML.setProperty('SelectionNamespaces','xmlns =''http://www.w3.org/2001/XMLSchema-instance'''); 

或者也许没有。正如我所说,我现在不能测试。


I've loaded a XML document, and now I wish to run a XPath query to select a certain subset of the XML. The XML is

<?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>

and the procedure goes something like

procedure RunXPathQuery(XML: IXMLDOMDocument2; Query: string);
begin

  XML.setProperty('SelectionLanguage', 'XPath');

  NodeListResult := XML.documentElement.selectNodes(Query));

  ShowMessage('Found (' + IntToStr(NodeListResult.length) + ') nodes.');

end;

Problem is: when I run the XPath query '/catalog' for the above XML, it returns (as expected) a nodelist of 1 element. However, if I remove :xsi from <catalog xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'> and re-run the query, the nodelist returned is empty. If I remove the entire 'xmlns'-attribute, the resulting nodelist has, once again, 1 element.

So my question is this: what can I do to remedy this, i.e. how do I make MSXML return the correct number of instances (when running a XPath query), regardless of the namespace (or other attributes)?

Thanks!

解决方案

See this link!

When you use <catalog xmlns='http://www.w3.org/2001/XMLSchema-instance'> then the whole node will be moved to a different (default) namespace. Your XPath isn't looking inside this other namespace so it can't find any data. With <catalog xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'> you're just declaring xsi as a different namespace. This would be a different namespace than the default namespace.

I can't test it right now but adding something like this might help:

XML.setProperty('SelectionNamespaces', 'xmlns=''http://www.w3.org/2001/XMLSchema-instance''');

Or maybe it doesn't. As I said, I can't test it right now.

这篇关于Delphi / MSXML:XPath查询失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆