XmlNodeList xPath过滤掉节点

时间:2010-11-03 14:54:51

标签: c# .net xml xpath

XmlNodeList list = null;
list = xmlResult.SelectNodes("/sitecore
                               /result
                                /item
                                 [scWebsitePath='"+sitecoreContextItemPath+"'
                                  and scTemplateId='"+templateId+"' 
                                  and scDateCreated > '"+publishedFrom+"' 
                                  and scDateCreated < '"+publishedTo+"']");

上面的代码返回所有“已验证”节点的列表。 是否也可以让xPath检查路径即。 “xxx / yyy / zzz /”是节点的一部分,例如,如果给定的路径是“xxx / yyy / zzz /”,并且我希望返回该路径下面的项目:

  • “xxx / yyy / zzz / abc / def / ghi”&lt; - 将会 有效
  • “xxx / yyy / zzz / abc / def / ghi / jkl”&lt; - 将 有效
  • “xxx / yyy / zzz / aaa / aaa / zzz”&lt; - 不会 有效

我可以通过以下方式访问节点Path:

XmlNode thisScPath = node.SelectSingleNode("scPath");
if (thisScPath == null)
continue;

所以我想知道我是否也可以这样做:

list = xmlResult.SelectNodes("/sitecore
                               /result
                                /item[scWebsitePath='"+sitecoreContextItemPath+"'
                                      and scTemplateId='"+templateId+"'
                                      and scDateCreated > '"+publishedFrom+"'
                                      and scDateCreated < '"+publishedTo+"'
                                      and scPath = '"+scPath+"/*']");

从列表中删除无效节点时,由于性能不佳,这将为我节省很多压力。我最终可以使用c#string.IndexOf!= -1语句删除无效项,但如果可能的话我想用xPath执行此操作。这可能吗?

1 个答案:

答案 0 :(得分:1)

我想你想检查一下

  and contains(scPath, 'xxx/yyy/zzz/')

  and starts-with(scPath, 'xxx/yyy/zzz/')
在您的XPath表达式中

。 如果您使用http://www.xqsharp.com/中的XPath 2.0实现,那么您甚至可以使用正则表达式和匹配函数。