title: Xpath only selecting first instance of element
body: <p>I am using <code>javax.xml.xpath.XPath;</code> To attempt to parse some <code>XML</code></p>
  
  <p>Sometimes a particular element may appear multiple times, when this happens I want to select all of them.</p>
  
  <p>I have the following <code>XML</code> (tag names and values have been changed)</p>
  
  <pre><code>&lt;Pearent&gt;
   &lt;a&gt;hello&lt;/a&gt;
   &lt;a&gt;world&lt;/a&gt;
   &lt;b&gt;bye&lt;/b&gt;
  &lt;/Pearent&gt;
  </code></pre>
  
  <p>I want to get the text value of all a nodes</p>
  
  <p>The results are being inserted directly into a <code>Java</code> <code>HashMap</code> and then accessed using <code>Thymeleaf</code></p>
  
  <p>if I use the following <code>xpath</code> <code>/Pearent/a</code>, I get <code>"hello"</code></p>
  
  <p>If I use <code>/Pearent/a[1]</code> I get <code>"hello"</code></p>
  
  <p>If I use <code>/Pearent/a[2]</code> I get "world"</p>
  
  <p>I have also tried targeting just the <code>Pearent</code> node so If I use <code>/Pearent</code>
  I get <code>"\n   hello\n    world\n    bye\n"</code>
  (note the the formatting <code>"\n   "</code> has been automatically inserted)</p>
  
  <p>I cannot target the pearent node as a solution to my problem because sometimes a <code>Pearent</code> node has other child nodes which I don't want.</p>
  
  <p>Interestingly I have tried the same <code>XML</code> and <code>XPath</code> in an online parser
  <a href="http://www.freeformatter.com/xpath-tester.html" rel="nofollow noreferrer">http://www.freeformatter.com/xpath-tester.html</a></p>
  
  <p>And I get different results</p>
  
  <p>If I use the <code>xpath</code> <code>/Pearent/a</code>, I get <code>&lt;a&gt;hello&lt;/a&gt;&lt;a&gt;world&lt;/a&gt;</code></p>
  
  <p>If I use the <code>xpath</code> <code>/Pearent/a[1]</code>, I get <code>&lt;a&gt;hello&lt;/a&gt;</code></p>
  
  <p>If I use the <code>xpath</code> <code>/Pearent/a[2]</code>, I get <code>&lt;a&gt;world&lt;/a&gt;</code></p>
  
  <p>If I use the <code>xpath</code> <code>/Pearent</code>, I get <code>&lt;Pearent&gt;&lt;a&gt;hello&lt;/a&gt;&lt;a&gt;world&lt;/a&gt;&lt;b&gt;bye&lt;/b&gt;&lt;/Pearent&gt;</code></p>
  
  <p>I see 2 differences here:</p>
  
  <ol>
  <li>The online parser returns whole nodes, my parser only returns the text content of nodes</li>
  <li>The online parser selects all elements which match, my parser only selects the first element which matches.</li>
  </ol>
  
  <p>My parser function is bellow</p>
  
  <p>My questions are:</p>
  
  <ul>
  <li><p>How can I control the formatting which appears to be being inserted when my <code>xpath</code> targets a node which has multiple child nodes?</p></li>
  <li><p>How can I get the text value of all child nodes as a string (ie for <code>Xpath</code> <code>/Pearent/a</code> I would want to get <code>"helloworld"</code> </p></li>
  <li><p>How can I return the results as separate nodes so that i can use a <code>foreach</code> loop on them in <code>Java</code> (remember I said the results were being passed into a <code>hashmap</code> value) or <code>ThymeLeaf</code></p></li>
  </ul>
  
  <h2>MY XML PARSER CODE</h2>
  
  <pre><code>public String readValue(String XML , String Xpath) throws Exception{
  
          final DocumentBuilderFactory builderFactory =     DocumentBuilderFactory.newInstance();
          builderFactory.setNamespaceAware(Boolean.parseBoolean(propertyConfiguration.getNameSpaceAware()));
      final DocumentBuilder builder = builderFactory.newDocumentBuilder();
      Document xml = builder.parse(new InputSource(new StringReader(XML)));
  
      final XPath xPath = XPathFactory.newInstance().newXPath();
  
      String retrievedValue = xPath.compile(Xpath).evaluate(xml);
  
      return retrievedValue;
  }
  </code></pre>
  
  <hr>
  
  <h2>THE PARSER FUNCTION IS BEING CALLED BY</h2>
  
  <pre><code>Map requiredData = new HashMap();
  requiredData.put("Title","/Pearent/a");
  </code></pre>