如何在一个查询中设置两个xPath查询?

时间:2013-03-14 12:32:15

标签: xpath xquery

这是我的完整代码: -

<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script>
        var xml;
        $.get(
        "xml_converted.xml",
        null,
        function (data) {
            xml = data;
        },
        "xml"
    );

        function get_node(ls)
        {
         var elName = $('#select').val();
         var value = $('#value').val();

        var xPath = '//*[translate(translate(translate("' + elName + '","-","")," ",""),":","") '+ ls +' translate(translate(translate(@value,"-","")," ",""),":","")]/../../@value';

         var iterator = xml.evaluate(xPath, xml.documentElement, null,
                XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);
            var thisNode = iterator.iterateNext();
            var str = '';
            while (thisNode) {
                if (str) {
                    str += ', ';
                }
                str += thisNode.textContent;
                thisNode = iterator.iterateNext();
            }
            $("#result").text(str);
            if (str == null || str == "")
            return false;
            else
            return true;
        }
    </script>
</head>
<body>
    <input type="text" id="select">
    <input type="text" id="value">
    <input type="button" value="Less than" onclick="get_node('>')">
    <input type="button" value="Grater than" onclick="get_node('<')">
    <input type="button" value="Equal" onclick="get_node('=')">
    <div id="result">
    </div>
</body>
</html>

这是我的xml领域样本: -

    <products>
      <product_id value="1">
        <tab_id value="351">
          <tab_name value="test1"/>
          <dist_region value="5,5068,5069"/>
          <reg_str_dt value="2013-01-14 20:35:00"/>
          <individual_strdt value="2013-01-14 20:35:00"/>
        </tab_id>
      </product_id>
      <product_id value="2">
        <tab_id value="352">
          <tab_name value="test2"/>
          <dist_region value="5,5069,5070"/>
          <reg_str_dt value="2013-01-14 20:35:00"/>
          <individual_strdt value="2013-01-14 20:35:00"/>
        </tab_id>
      </product_id>
   </productS>

在我的xml文件中,我有一个标签dist_region_value="5,5069,5070"这种类型的值用于检查我有一个xpath: -

var xPath = '//*[contains(concat(@value, ","),"' + elName+ ',")]/../../@value';

和另一个xpath查询是: -

var xPath = '//*[translate(translate(translate("' + elName + '","-","")," ",""),":","") '+ ls +' translate(translate(translate(@value,"-","")," ",""),":","")]/../../@value';

如何在一个查询中设置两者?

感谢...

1 个答案:

答案 0 :(得分:0)

这个怎么样:

var xPath = '//*[contains(concat(@value, ","), "' + elNname+ ',") and ' + 
                ' translate("' + elName + '"," -:","") '+ ls + 
                ' translate(@value," -:","")]/../../@value';

如此处所示,您也不需要将多个翻译嵌套在一起。