title: Java XPath "if" statement
body: <p>I have some problems when I use <code>if</code> statement inside a <code>for</code> loop to delete some nodes in a XML document, because I don't understand why my counters become to 0 after put the <code>if</code> statement:</p>
  
  <pre><code>public static void main(String[] args) throws Exception {
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      Document document = dbf.newDocumentBuilder().parse(new File("input3.xml"));
  
  
      XPathFactory xpf = XPathFactory.newInstance();
      XPath xpath = xpf.newXPath();
  
      XPathExpression expres = xpath.compile("//TNMS/TPaths/TPath[Topology/LayerSet/NonTerminatedLayers/Layer='RS64']");
      NodeList PathsCount = (NodeList) expres.evaluate(document, XPathConstants.NODESET);
  
      System.out.println("Inicia el Borrado:");
  
      for (int i = 0; i &lt; PathsCount.getLength(); i++)
      {
          System.out.println(i);
  
          XPathExpression exprueba = xpath.compile("/TNMS/TPaths/TPath[1]/Topology/LayerSet/TerminatedLayers");
          NodeList Count = (NodeList) exprueba.evaluate(document, XPathConstants.NODESET);
          System.out.println("Hay " + Count.getLength());
          int countvar = Count.getLength();
  
          XPathExpression expression = xpath.compile("//TNMS/TPaths/TPath[Topology/LayerSet/NonTerminatedLayers/Layer='RS64']");
          Node b13Node = (Node) expression.evaluate(document, XPathConstants.NODE);
          b13Node.getParentNode().removeChild(b13Node);
      }
  </code></pre>
  
  <p>I see next in the output:</p>
  
  <pre><code>Inicia el Borrado:
  0
  
  Hay 0
  
  1
  
  Hay 1
  
  2
  
  Hay 1
  
  3
  
  Hay 0
  
  &lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;&lt;TNMS&gt;
      &lt;TPaths&gt;
  
      &lt;/TPaths&gt;
  &lt;/TNMS&gt;BUILD SUCCESSFUL (total time: 0 seconds)
  </code></pre>
  
  <p>But when i agree the <code>if</code> statement: </p>
  
  <pre><code>public static void main(String[] args) throws Exception {
          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
          Document document = dbf.newDocumentBuilder().parse(new File("input3.xml"));
  
  
          XPathFactory xpf = XPathFactory.newInstance();
          XPath xpath = xpf.newXPath();
  
          XPathExpression expres = xpath.compile("//TNMS/TPaths/TPath[Topology/LayerSet/NonTerminatedLayers/Layer='RS64']");
          NodeList PathsCount = (NodeList) expres.evaluate(document, XPathConstants.NODESET);
  
          System.out.println("Inicia el Borrado:");
  
          for (int i = 0; i &lt; PathsCount.getLength(); i++)
          {
              System.out.println(i);
  
              XPathExpression exprueba = xpath.compile("/TNMS/TPaths/TPath[1]/Topology/LayerSet/TerminatedLayers");
              NodeList Count = (NodeList) exprueba.evaluate(document, XPathConstants.NODESET);
              System.out.println("Hay " + Count.getLength());
              int countvar = Count.getLength();
  
              if (countvar == 1) {
                  XPathExpression expression = xpath.compile("//TNMS/TPaths/TPath[Topology/LayerSet/NonTerminatedLayers/Layer='RS64']");
                  Node b13Node = (Node) expression.evaluate(document, XPathConstants.NODE);
                  b13Node.getParentNode().removeChild(b13Node);
              }
          }
  </code></pre>
  
  <p>the output is the next:</p>
  
  <pre><code>    Inicia el Borrado:
  0
  
  Hay 0
  
  1
  
  Hay 0
  
  2
  
  Hay 0
  
  3
  
  Hay 0
  </code></pre>
  
  <p>and the erase is failed.</p>
  
  <p>Any ideas?</p>