title: Using an integer variable into an XPath expression Java to extract info from repeating nodes with namespaces
body: <p>Solved! Thank you guys!</p>
  
  <p>Your suggestions led me to the answer. So all of you deserve thanks for the suggestion.</p>
  
  <p>The solution to putting a int variable is the following:</p>
  
  <pre><code>//Put my xpath into a string variable where it can evaluate my integer "counter"
  
  String myString = "/*[local-name()='root' and namespace-uri()='DK']/*[local-name()='book' and namespace-uri()='DK']" + counter + "]/*[local-name()='item1' and namespace-uri()='DK']/*[local-name()='item2' and namespace-uri()='DK']/*[local-name()='date' and namespace-uri()='DK'][" + counter + "]"
  
  
  //Then simply put that string variable into the xpath expression.
  System.out.print(myString); 
  </code></pre>
  
  <p>Original Question below:</p>
  
  <p>Firstly, I appreciate any help. Thanks in advance for any help.</p>
  
  <p>I have an XML file that I need to parse.</p>
  
  <p>Lets say for example...</p>
  
  <pre><code>&lt;root xmlns="DK"&gt;
    &lt;book&gt;
      &lt;item1&gt;
        &lt;item2&gt;
          &lt;date&gt;xxx
          &lt;/date&gt;
          &lt;date&gt;x1x1x1
          &lt;/date&gt;
        &lt;/item2&gt;
      &lt;/item1&gt;
    &lt;/book&gt;
    &lt;book&gt;
      &lt;item1&gt;
        &lt;item2&gt;
          &lt;date&gt;yyy
          &lt;/date&gt;
          &lt;date&gt;y1y1y1
          &lt;/date&gt;
        &lt;/item2&gt;
      &lt;/item1&gt;
    &lt;/book&gt;    
  &lt;/root&gt;
  </code></pre>
  
  <p>So basically, I'm looking for the node &lt; date> with xxx and then move on to the same node &lt; date> in the next book which contains yyy.</p>
  
  <p>The input xml file will have an unknown amount of repeating books with different information for each book. </p>
  
  <p>I've chosen to try something like this.</p>
  
  <pre><code>// I've imported from dom4j SAXreader. I probably could use something different.
  import org.dom4j.io.SAXReader;
  
  
  
  // Variables
  Document document = null;       
  
  // Take input from file    
  SAXReader reader = new SAXReader();
  document = reader.read("E:/templates/test.xml");
  
  int counter = 0;
  int numofbooks = 0;
  
  Element root = document.getRootElement();
  
  for ( Iterator i = root.elementIterator("book"); i.hasNext();) {
      Element element = (Element) i.next();
  
  counter = counter + 1;
  numofbooks = counter;
  }
  
  System.out.println("There are " + numofbooks + " books required for processing");
  while(counter != numofbooks); {
  counter = counter + 1;
  
  System.out.print("The values are: ");
  
  // ----- This here is my problem code ----- //
  System.out.print(/*[local-name()='root' and namespace-uri()='DK']/*[local-name()='book' and namespace-uri()='DK'][counter]/*[local-name()='item1' and namespace-uri()='DK']/*[local-name()='item2' and namespace-uri()='DK']/*[local-name()='date' and namespace-uri()='DK'][counter]).getStringValue());
  
  
  System.out.print(" respectively");
  }
  </code></pre>
  
  <p>My problem lies here. Because there are a few repeated nodes with the exact same name that are nested, I'm grabbing them out by number using an integer counter variable. </p>
  
  <p>My XPath expression doesn't understand that I'm putting in a variable "counter" that is an integer.</p>
  
  <p>Is there any syntax I can use to make this work?
  Or is it just not possible?</p>
  
  <p>Thanks in advance.</p>
  
  <p>DK</p>