jQuery XPath pluginhttps://github.com/ilinsky/jquery-xpath

解决方案 »

  1.   

    测试了一下,发现jquery xpath插件不支持中文路径
      

  2.   

    测试了一下,感觉不如直接 jq 遍历<script src=scripts/jquery-1.8.3.min.js></script>
    <script src=scripts/jquery.xpath.min.js></script>
    <script>
    $(function() {
      res = $('xml').xpath('/bookstore/book[price>35.00]');
      $('#view').html(res.text());
      res = $('xml').xpath('/bookstore/book/price[.>35.00]');
      $('#view').append('<br>'+res.text());
      try {
        res = $('xml').xpath('//文');
        $('#view').append('<br>'+res.text());
      }catch(e) {
        $('#view').append('<br>'+e.message);
      }
      res = $('xml').find('文');
      $('#view').append('<br>'+res.text());
    })
    </script>
    <body>
    <div id=view></div>
    <xml id=x>
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <bookstore>
    <book>
      <title lang="eng">Harry Potter</title>
      <price>29.99</price>
    </book>
    <book>
      <title lang="eng">Learning XML</title>
      <price>39.95</price>
    </book>
    <中><文>汉字</文></中>
    </bookstore>
    </xml>
    </body>
    Learning XML39.95
    39.95
    Expression is not a valid instance of the grammar
    汉字
      

  3.   

    可是jq好像不支持/bookstore/book[price>35.00]这种路径表达式
      

  4.   

    在jquery中[]里是属性,不是节点
      

  5.   

    解决不支持中文路径的方法:
    将 jquery.xpath.min.js 中的 \w 替换成 \w\u4e00-\u9fa5