I have below xml file wherein I want to select hostname,instance name, instance type using XPATH
<root>
<hosts>
<host id="11">
<name>ABC</name>
<instances>
<instance id="11-1">
<name>raj</name>
<type>linux</type>
<date>2017</date>
</instance>
<instance id="11-2">
<name>raj1</name>
<type>linux</type>
<date>2017</date>
</instance>
</instances>
</host>
<host id="12">
<name>XYZ</name>
<instances>
<instance id="12-1">
<name>rahul</name>
<type>solaris</type>
<date>2017</date>
</instance>
</instances>
</host>
</hosts>
</root>
I have tried below XPATH which is selecting instance name and type but not sure how to print host name along with instance name and type.
//hosts/host/instances/instance/*[self::name or self:: type]/text()
It selects the below result.
raj
linux
raj1
linux
rahul
solaris
However, I want output like below which has host name included
ABC
raj
linux
raj1
linux
XYZ
rahul
solaris
//is quite expensive here -- it tells your XPath engine it needs to search forhostselements anywhere in the tree, instead of only directly underroot. In some engines there's an index for that kind of lookup (BaseX comes to mind), but that's by no means a universal feature./root/hostsinstead of//hosts).