title: XPath - abbreviation of position() function
body: <p>Can anyone explain what's the difference between</p>
  
  <pre><code>/root/a[position()=1 or position()=2
  </code></pre>
  
  <p>and</p>
  
  <pre><code>/root/a[1 or 2]
  </code></pre>
  
  <p>?
  I'd assume the 2nd to be abbreviated form of the 1st, but Java XPath (Sun JDK 1.6.0) processor thinks otherwise. Following is my test application.</p>
  
  <p>libxml2 library and also db2 XPath processor consider these paths different too. So it doesn't look like JDK bug.</p>
  
  <pre><code>import java.io.*;
  import javax.xml.xpath.*;
  import org.w3c.dom.*;
  
  import org.xml.sax.InputSource;
  
  public class XPathTest {
      public static void main(String[] args) throws Exception {
          //String xpathStr = "/root/a[position()=1 or position()=2]";
          String xpathStr = "/root/a[1 or 2]";
  
          XPathFactory xpf = XPathFactory.newInstance();
          XPath xp = xpf.newXPath();
          Reader irdr = new StringReader(
                  "&lt;root&gt;&lt;a name=\"first\"/&gt;&lt;a name=\"second\"/&gt;&lt;a name=\"third\"/&gt;&lt;/root&gt;");
          InputSource isrc = new InputSource(irdr);
          XPathExpression expr = xp.compile(xpathStr);
          Object result = expr.evaluate(isrc, XPathConstants.NODESET);
          NodeList nodes = (NodeList) result;
          for (int i = 0; i &lt; nodes.getLength(); i++) {
              Node node = nodes.item(i);
              Element element = (Element) node;
              System.out.print(element.getNodeName() + " " + element.getAttributeNode("name"));
             System.out.println();
          }
      }
  }
  </code></pre>