简体   繁体   English

XML命名空间,无法选择节点

[英]XML Namespace, can not select nodes

I have this xml: 我有这个xml:

<?xml version="1.0" encoding="utf-8" ?>  
    <ArrayOfFileInformation xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://tempuri.org/">  <Data> ......

When I leave "xmlns="http://tempuri.org/" in the Xml, the following doesn't return any nodes: 当我在Xml中保留"xmlns="http://tempuri.org/"时,以下内容不会返回任何节点:

CString tag = L"//Data";
MSXML2::IXMLDOMNodeListPtr pDataTag = pXMLDocument->selectNodes((_bstr_t)tag);

When I remove the attribute, everything works fine though. 当我删除该属性时,一切正常。

Can you please help me? 你能帮我么?

In order to select namespaced nodes in XPath you need to use the facility provided by your XPath library to bind a prefix to the relevant namespace URI, and then use that prefix when selecting nodes by name. 为了在XPath中选择命名空间节点,您需要使用XPath库提供的功能将前缀绑定到相关的命名空间URI,然后在通过名称选择节点时使用该前缀。 The MSDN documentation for the selectNodes method has a C++ example of just this , the key is to set the SelectionNamespaces property on the document MSDN文档中的selectNodes方法只有一个C ++示例 ,关键是在文档上设置SelectionNamespaces属性。

pXMLDocument->setProperty("SelectionNamespaces", "xmlns:tmp='http://tempuri.org/'");

which will then allow you to use an XPath of //tmp:Data 然后,您将可以使用//tmp:Data的XPath

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM