c#
  • .net
  • xml
  • xpath
  • 2010-11-03 87 views 1 likes 
    1
    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 /”,我想返回该路径下的项目:XmlNodeList xPath过滤节点

    • “XXX/YYY/ZZZ/ABC/DEF/GHI “< - 将 有效
    • ”XXX/YYY/ZZZ/ABC/DEF/GHI/JKL“ < - 将 有效
    • ” XXX/YYY/ZZZ/AAA/aaa/zzz“< - 不会是 有效

    我可以通过执行访问节点路径荷兰国际集团这样的:

    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来做到这一点。这可能吗?

    +0

    @Alejandro:请问,请解释为什么xxx/yyy/zzz/aaa/aaa/zzz“'”无效?它也以'xxx/yyy/zzz /'作为另外两个开始。 – 2010-11-04 01:46:44

    回答

    1

    我想你想检查

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

    and starts-with(scPath, 'xxx/yyy/zzz/') 
    

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

    +1

    +1正确的答案。 – 2010-11-03 16:27:36

    +0

    我使用的搜索引擎检索器基于System.Xml.XmlDocument提供结果。不幸的是,我无法改变我回到这里的结果的类型。是否可以继续使用您在回答中提到的xPath 2.0实现,并且是否还有可用的此Xpath 2.0的免费实现? (我正在开发的商业用途,它的成本200美元) – Younes 2010-11-03 17:21:15

    +0

    @Alejandro:感谢编辑我的问题,使其更具可读性。 – Younes 2010-11-03 17:25:04

    相关问题