This is my XML given:
<WorkItem>
<Id>717</Id>
<WorkItemType>Product Backlog Item</WorkItemType>
<TreeLevel>1</TreeLevel>
<Children>
<WorkItem>
<Id>719</Id>
<WorkItemType>Product Backlog Item</WorkItemType>
<TreeLevel>2</TreeLevel>
<Children>
<WorkItem>
<Id>721</Id>
<WorkItemType>Task</WorkItemType>
<TreeLevel>3</TreeLevel>
<Children>
<WorkItem>
<Id>722</Id>
<WorkItemType>Task</WorkItemType>
<TreeLevel>4</TreeLevel>
<Children />
</WorkItem>
</Children>
</WorkItem>
</Children>
</WorkItem>
<WorkItem>
<Id>720</Id>
<WorkItemType>Product Backlog Item</WorkItemType>
<TreeLevel>2</TreeLevel>
<Children>
<WorkItem>
<Id>724</Id>
<WorkItemType>Task</WorkItemType>
<TreeLevel>3</TreeLevel>
<Children>
<WorkItem>
<Id>726</Id>
<WorkItemType>Task</WorkItemType>
<TreeLevel>4</TreeLevel>
<Children />
</WorkItem>
</Children>
</WorkItem>
<WorkItem>
<Id>725</Id>
<WorkItemType>Task</WorkItemType>
<TreeLevel>3</TreeLevel>
<Children>
<WorkItem>
<Id>727</Id>
<WorkItemType>Task</WorkItemType>
<TreeLevel>4</TreeLevel>
<Children />
</WorkItem>
<WorkItem>
<Id>728</Id>
<WorkItemType>Task</WorkItemType>
<TreeLevel>4</TreeLevel>
<Children />
</WorkItem>
<WorkItem>
<Id>729</Id>
<WorkItemType>Task</WorkItemType>
<TreeLevel>4</TreeLevel>
<Children>
<WorkItem>
<Id>745</Id>
<WorkItemType>Task</WorkItemType>
<TreeLevel>5</TreeLevel>
<Children />
</WorkItem>
<WorkItem>
<Id>746</Id>
<WorkItemType>Task</WorkItemType>
<TreeLevel>5</TreeLevel>
<Children />
</WorkItem>
</Children>
</WorkItem>
</Children>
</WorkItem>
</Children>
</WorkItem>
<WorkItem>
<Id>723</Id>
<WorkItemType>Task</WorkItemType>
<TreeLevel>2</TreeLevel>
<Children>
<WorkItem>
<Id>744</Id>
<WorkItemType>Task</WorkItemType>
<TreeLevel>3</TreeLevel>
<Children />
</WorkItem>
</Children>
</WorkItem>
</Children>
</WorkItem>
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.
I tried to receive it with this xpath command (I use System.Xml.XmlDocument in C#):
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']]
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).
- 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'.
- In case of a WorkItem-element of type 'Task' I want to have all child WorkItem-elements below.
How can I express this in XPath?
In the given XML I expect the WorkItem-elements with ID's 719,720,723,744
Children
) based on the condition you defined in it, but it will not filter some elements out in the selected elements - you should do it manually. Why do you want to select the Workitem 744, but not the WorkItem 746? Your filter options are not clear for me. – Sergii ZhevzhykChildren/WorkItem[WorkItemType[text()='Product Backlog Item']]|Children/WorkItem[WorkItemType[text()='Task']]|Children/WorkItem[WorkItemType[text()='Task']]/Children/WorkItem
– Keith HallChildren/WorkItem[WorkItemType[text()='Product Backlog Item']]|Children/WorkItem[WorkItemType[text()='Task']]/descendant-or-self::WorkItem[WorkItemType[text()='Task']]
– Bruno Bieri