Xml – Extract value of attribute node via XPath


How can I extract the value of an attribute node via XPath?

A sample XML file is:

<parents name='Parents'>
  <Parent id='1' name='Parent_1'>
    <Children name='Children'>
      <child name='Child_2' id='2'>child2_Parent_1</child>
      <child name='Child_4' id='4'>child4_Parent_1</child>
      <child name='Child_1' id='3'>child1_Parent_1</child>
      <child name='Child_3' id='1'>child3_Parent_1</child>
  <Parent id='2' name='Parent_2'>
    <Children name='Children'>
      <child name='Child_1' id='8'>child1_parent2</child>
      <child name='Child_2' id='7'>child2_parent2</child>
      <child name='Child_4' id='6'>child4_parent2</child>
      <child name='Child_3' id='5'>child3_parent2</child>

So far I have this XPath string:


It returns only child elements, but I would like to have the value of the name attribute.

For my sample XML file, here's what I'd like the output to be:


Best Solution


Your original child[@name] means an element child which has an attribute name. You want child/@name.