c#
  • .net
  • xml
  • xpath
  • 2010-11-03 58 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

    相關問題