为什么我的 xpath 不起作用 [英] Why my xpath doesn't work
问题描述
我正在使用 SharePoint 网络服务从网站检索内容类型.输出如下所示:
I am using SharePoint webservice to retrieve content type from the site. The output looks like this:
<ContentTypes ContentTypeOrder="0x010300971A94A609AC5F4390A1FF87A26CD05D" xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<ContentType Name="Issue" ID="0x010300971A94A609AC5F4390A1FF87A26CD05D"
Description="Track an issue or problem."
Scope="https://practiv1.sharepoint.com/devtest/Lists/issueTracking"
Version="0"
BestMatch="TRUE">
<XmlDocuments>
<XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
<FormTemplates xmlns="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
<Display>ListForm</Display>
<Edit>ListForm</Edit>
<New>ListForm</New>
</FormTemplates>
</XmlDocument>
</XmlDocuments>
</ContentType>
<ContentType Name="Folder"
ID="0x01200049A00CD1A9F3944A9AE7BCCAC15B02D4"
Description="Create a new folder."
Scope="https://practiv1.sharepoint.com/devtest/Lists/issueTracking"
Version="0">
<XmlDocuments>
<XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
<FormTemplates xmlns="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
<Display>ListForm</Display>
<Edit>ListForm</Edit>
<New>ListForm</New>
</FormTemplates>
</XmlDocument>
</XmlDocuments>
</ContentType>
我想使用 xpath 来检索项目.但是我使用了像//ContentType"或/ContentTypes/ContentType"这样的路径,我找不到任何东西:
I want to use xpath to retrieve items. But I used the path like "//ContentType" or "/ContentTypes/ContentType", I couldn't find anything :
var listService = new ListWebService.Lists();
listService.Url = "xxx.sharepoint.com/xxx/_vti_bin/Lists.asmx";
var contents = listService.GetListContentTypes("issueTracking", "0x01");
有人可以帮我看看我的 xpath 有什么问题吗?
Can someone help me what wrong with my xpath?
推荐答案
这是处理具有默认命名空间 (xmlns="..."
) 的 XML 时的常见问题.声明前缀的节点及其所有后代,如果没有明确指定,则在默认命名空间中被考虑.
This is a common problem when dealing with XML having default namespace (xmlns="..."
). The node where the prefix declared and all it's descendants, if not explicitly specified otherwise, are considered in the default namespace.
您需要注册一个指向命名空间 URI 的前缀并在您的 XPath 中使用该前缀,例如:
You need to register a prefix that points to the namespace URI and use that prefix in your XPath, for example :
var nsManager = new XmlNamespaceManager(new NameTable());
nsManager.AddNamespace("d", "http://schemas.microsoft.com/sharepoint/soap/");
var result = contents.SelectNodes("//d:ContentType", nsManager);
//or using the other XPath : "/d:ContentTypes/d:ContentType"
这篇关于为什么我的 xpath 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!