title: Xpath to get all attributes of a DOM sub-tree?
body: <p>Could someone please explain why this is happening; this following is the code I’ve got:</p>
  
  <pre><code>String xml = "&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n" +
  "&lt;title text=\"title1\"&gt;\n" +
  "    &lt;comment id=\"comment1\"&gt;\n" +
  "        &lt;data&gt; abcd &lt;/data&gt;\n" +
  "        &lt;data&gt; efgh &lt;/data&gt;\n" +
  "    &lt;/comment&gt;\n" +
  "    &lt;comment id=\"comment2\"&gt;\n" +
  "        &lt;data&gt; ijkl &lt;/data&gt;\n" +
  "        &lt;data&gt; mnop &lt;/data&gt;\n" +
  "        &lt;data&gt; qrst &lt;/data&gt;\n" +
  "    &lt;/comment&gt;\n" +
  "&lt;/title&gt;\n";
  
  DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  Document doc = builder.parse(new InputSource(new StringReader(xml)));
  NodeList nlist = doc.getElementsByTagName("comment");
  
  XPathFactory xpf = XPathFactory.newInstance();
  XPath xp = xpf.newXPath();
  NodeList nodes = (NodeList)xp.evaluate("//@*", nlist.item(0), XPathConstants.NODESET);
  for(int i = 0; i &lt; nodes.getLength(); i++)
      System.out.println(nodes.item(i));
  </code></pre>
  
  <p>The output produced is:</p>
  
  <pre><code>text="title1"
  id="comment1"
  id="comment2"
  </code></pre>
  
  <p>The problem is, I am trying to do an XPath query on a sub-tree (i.e. the first <code>comment</code> block), so I just want to get <em>all attributes for this sub-tree</em> but for some reason, whichever sub-tree I pass in, it always returns <strong>all</strong> attribute nodes, as if the object being passed into the xpath evaluator is the root node.</p>
  
  <p>How can I just pass in and evaluate <em>all attributes</em> for a subtree?</p>