title: XPath select child node by type condition and for certain type all its children
body: <p>This is my XML given:</p>
  
  <pre><code>&lt;WorkItem&gt;
      &lt;Id&gt;717&lt;/Id&gt;
      &lt;WorkItemType&gt;Product Backlog Item&lt;/WorkItemType&gt;
      &lt;TreeLevel&gt;1&lt;/TreeLevel&gt;
      &lt;Children&gt;
          &lt;WorkItem&gt;
              &lt;Id&gt;719&lt;/Id&gt;
              &lt;WorkItemType&gt;Product Backlog Item&lt;/WorkItemType&gt;
              &lt;TreeLevel&gt;2&lt;/TreeLevel&gt;
              &lt;Children&gt;
                  &lt;WorkItem&gt;
                      &lt;Id&gt;721&lt;/Id&gt;
                      &lt;WorkItemType&gt;Task&lt;/WorkItemType&gt;
                      &lt;TreeLevel&gt;3&lt;/TreeLevel&gt;
                      &lt;Children&gt;
                          &lt;WorkItem&gt;
                              &lt;Id&gt;722&lt;/Id&gt;
                              &lt;WorkItemType&gt;Task&lt;/WorkItemType&gt;
                              &lt;TreeLevel&gt;4&lt;/TreeLevel&gt;
                              &lt;Children /&gt;
                          &lt;/WorkItem&gt;
                      &lt;/Children&gt;
                  &lt;/WorkItem&gt;
              &lt;/Children&gt;
          &lt;/WorkItem&gt;
          &lt;WorkItem&gt;
              &lt;Id&gt;720&lt;/Id&gt;
              &lt;WorkItemType&gt;Product Backlog Item&lt;/WorkItemType&gt;
              &lt;TreeLevel&gt;2&lt;/TreeLevel&gt;
              &lt;Children&gt;
                  &lt;WorkItem&gt;
                      &lt;Id&gt;724&lt;/Id&gt;
                      &lt;WorkItemType&gt;Task&lt;/WorkItemType&gt;
                      &lt;TreeLevel&gt;3&lt;/TreeLevel&gt;
                      &lt;Children&gt;
                          &lt;WorkItem&gt;
                              &lt;Id&gt;726&lt;/Id&gt;
                              &lt;WorkItemType&gt;Task&lt;/WorkItemType&gt;
                              &lt;TreeLevel&gt;4&lt;/TreeLevel&gt;
                              &lt;Children /&gt;
                          &lt;/WorkItem&gt;
                      &lt;/Children&gt;
                  &lt;/WorkItem&gt;
                  &lt;WorkItem&gt;
                      &lt;Id&gt;725&lt;/Id&gt;
                      &lt;WorkItemType&gt;Task&lt;/WorkItemType&gt;
                      &lt;TreeLevel&gt;3&lt;/TreeLevel&gt;
                      &lt;Children&gt;
                          &lt;WorkItem&gt;
                              &lt;Id&gt;727&lt;/Id&gt;
                              &lt;WorkItemType&gt;Task&lt;/WorkItemType&gt;
                              &lt;TreeLevel&gt;4&lt;/TreeLevel&gt;
                              &lt;Children /&gt;
                          &lt;/WorkItem&gt;
                          &lt;WorkItem&gt;
                              &lt;Id&gt;728&lt;/Id&gt;
                              &lt;WorkItemType&gt;Task&lt;/WorkItemType&gt;
                              &lt;TreeLevel&gt;4&lt;/TreeLevel&gt;
                              &lt;Children /&gt;
                          &lt;/WorkItem&gt;
                          &lt;WorkItem&gt;
                              &lt;Id&gt;729&lt;/Id&gt;
                              &lt;WorkItemType&gt;Task&lt;/WorkItemType&gt;
                              &lt;TreeLevel&gt;4&lt;/TreeLevel&gt;
                              &lt;Children&gt;
                                  &lt;WorkItem&gt;
                                      &lt;Id&gt;745&lt;/Id&gt;
                                      &lt;WorkItemType&gt;Task&lt;/WorkItemType&gt;
                                      &lt;TreeLevel&gt;5&lt;/TreeLevel&gt;
                                      &lt;Children /&gt;
                                  &lt;/WorkItem&gt;
                                  &lt;WorkItem&gt;
                                      &lt;Id&gt;746&lt;/Id&gt;
                                      &lt;WorkItemType&gt;Task&lt;/WorkItemType&gt;
                                      &lt;TreeLevel&gt;5&lt;/TreeLevel&gt;
                                      &lt;Children /&gt;
                                  &lt;/WorkItem&gt;
                              &lt;/Children&gt;
                          &lt;/WorkItem&gt;
                      &lt;/Children&gt;
                  &lt;/WorkItem&gt;
              &lt;/Children&gt;
          &lt;/WorkItem&gt;
          &lt;WorkItem&gt;
              &lt;Id&gt;723&lt;/Id&gt;
              &lt;WorkItemType&gt;Task&lt;/WorkItemType&gt;
              &lt;TreeLevel&gt;2&lt;/TreeLevel&gt;
              &lt;Children&gt;
                  &lt;WorkItem&gt;
                      &lt;Id&gt;744&lt;/Id&gt;
                      &lt;WorkItemType&gt;Task&lt;/WorkItemType&gt;
                      &lt;TreeLevel&gt;3&lt;/TreeLevel&gt;
                      &lt;Children /&gt;
                  &lt;/WorkItem&gt;
              &lt;/Children&gt;
          &lt;/WorkItem&gt;
      &lt;/Children&gt;
  &lt;/WorkItem&gt;
  </code></pre>
  
  <p>I would like to retrieve all nodes of a node with subnodes two a certain types. If this type is of one kind I would like to get all descendant as well.</p>
  
  <p>I tried to receive it with this xpath command (I use System.Xml.XmlDocument in C#):</p>
  
  <pre><code>xmlDoc.SelectNodes("Children/WorkItem[WorkItemType[text()='Product Backlog Item']]|Children/WorkItem[WorkItemType[text()='Task']]/following::WorkItem[WorkItemType[text()='Task']]");
  
  // splitted for better readability
  Children/WorkItem[
      WorkItemType[
          text()='Product Backlog Item']
      ]|
      Children/WorkItem[
          WorkItemType[
             text()='Task']
      ]/following::WorkItem[WorkItemType[text()='Task']]
  </code></pre>
  
  <p>This provides me only the nodes with ID 719 and 720.
  But I expect the WorkItem nodes with Id's: 719 and 720 (first part of the xpath expression) as well as 723 and 744 (from the second xpath expression).</p>
  
  <ul>
  <li>My goal is to have WorkItem-elements of WorkItemType 'Product Backlog Item' which has Children WorkItem-Elements of WorkItemType 'Product Backlog Item' as well as 'Task'.
  
  <ul>
  <li>In case of a WorkItem-element of type 'Task' I want to have <strong>all</strong> child WorkItem-elements below.</li>
  </ul></li>
  </ul>
  
  <p>How can I express this in XPath?</p>
  
  <p>In the given XML I expect the WorkItem-elements with ID's 719,720,723,744</p>