xpath query for attribute can't work?

hui zhou
2011-02-28
2013-05-15
  • hui zhou
    hui zhou
    2011-02-28

    I am just trying to do some test on VTD-XML library, but I find if I search some element with attribute defined in xpath expression, it just can't work.

    For my test, I have an xml file with below content:

    <users>
      <user name="j1" level="junior" />
      <user name="j2" level="junior" />
      <user name="s1" level="senior" />
      <user name="s2" level="senior" />
    </users>
    

    And I have an java file with below content:

    import com.ximpleware.*;
    public class Test{
        public static void main(String[] args) throws Exception{
            String xpathExpr = "/users/user[@level='junior']";
            VTDGen vg = new VTDGen();
            int i = -1;
            if (vg.parseFile("test.xml",true)){
                VTDNav vn = vg.getNav();
                AutoPilot ap = new AutoPilot();
                ap.selectXPath(xpathExpr);
                ap.bind(vn);
    
                while((i=ap.evalXPath()) != -1) {
                    System.out.println("Index No. is " + i);
                }
            }
        }
    }
    

    After running the java code, it desn't output any content. But If I change the xpath expression from

    String xpathExpr = "/users/user]";
    

    to

    String xpathExpr = "/users/user";
    

    Then it will ouput some contents as expected.

    Any idea why my previous xpath quey can't work?

     
  • hui zhou
    hui zhou
    2011-02-28

    For my previous posts , it should be:

    After running the java code, it desn't output any content. But If I change the xpath expression from

    String xpathExpr = "/users/user[@level='junior']";
    

    to

    String xpathExpr = "/users/user";
    

    Then it will ouput some contents as expected.

    By the way, If I want to search all the attribute "level" with xpath expression "//@level", it doesn't output any content either.

     
  • jimmy zhang
    jimmy zhang
    2011-02-28

    ok, will investigate

     
  • jimmy zhang
    jimmy zhang
    2011-02-28

    can you dowload the new release and i think it should work