Search code examples
sitecoresitecore7

Sitecore query syntax: Select all female descendents whose parent isn't called Jack


Given a Sitecore content tree of Males and Females (each sex with own template) representing a family tree, how would I select all Female descendents of an item where the parent wasn't called Jack using Sitecore query?

Context: My context item is one of Bob's children. My query shouldn't return Bob himself. Bob also has hundreds of brothers with thousands of descendants that I really don't want appearing in my results.

  • Bob
    • Sarah
      • Jim
      • Julie
    • John
      • Sue
      • Jack
        • Anne
    • Jack
      • Claire
        • Mary

The query should return: Sarah, Julie, Sue and Mary but not Anne or Claire.

I can select all female descendents of Bob with:

..//*[@@templateid='{insert female template id here}']

But how do I add the parent name != Jack clause?


Solution

  • If you had a "family root" node that did not represent a person by itself, you could do this:

    /path/to/family root//*[@@name != 'Jack']/*[@@templateid = '{template id}']
    

    In your case, you want only a certain person's descendants to be returned. The person themselves should not be included in the result set. In that case, the approach from your comment is the way to go:

    ..//*[../@@name != 'Jack' AND @@templateid = '{template id}']
    

    The results of both queries will include Mary since her direct parent is not called Jack.