Less than, greater than, equal to not working as expected in XPath

旧城冷巷雨未停 提交于 2019-12-13 06:15:38

问题


this is my xml file:-

   <products>
      <product_id value="1">
        <tab_id value="351">
          <tab_name value="test1"/>
          <dist_map value="5"/>
        </tab_id>
      </product_id>
      <product_id value="2">
        <tab_id value="352">
          <tab_name value="test2"/>
          <dist_map value="3"/>
        </tab_id>
      </product_id>
   </products>

this is my xpath:-

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

i have three buttons and two text-boxes, code is:

<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 = '//*[local-name() = "'+elName+'" and '+value+' and '+ value +' '+ls
        +' contains(concat(@value,","),"'+value+',")and '+
        ' 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);
        }
    </script>
</head>
<body>
    <input type="text" id="select">
    <input type="text" id="value">
    <input type="button" name="button" value="Less than" onclick="get_node('<')"></input>
    <input type="button" name="button" value="Grater than" onclick="get_node('>')"></input>
    <input type="button" name="button" value="Equal" onclick="get_node('=')"></input>
    <div id="result">
    </div>
</body>
</html>

with this code o/p is:-
i am enter in first textbox dist_map
in second textbox 3(its dist_map attribute value)
then click on less-than button its not give any output
if click on grater-than button then its display both 1,2(its product_id attribute value)
is this not perfect o/p.
here o/p is only 1(its product_id attribute value) bcoz 3 is less than to 5
and if click on equal to button then work perfect 2(its product_id attribute value)b'coz both textbox value is match in xml file
here only problem with in my xpath or argument pass in button like (<, >)
please check my this question and help to solve me this...
thanks


回答1:


I don't really understand what you are trying to achieve with the calls to contains, concat and translate in your XPath expression. But if you simply want to list products whose dist_map value is less than, equal, or greater than a given value, the following should work:

var xPath = '//*[local-name() = "' + elName + '" and @value ' + ls + ' ' + value
        + ']/../../@value';

This will create expressions like:

//*[local-name() = "dist_map" and @value > 3]/../../@value'

Note that this doesn't validate the input. So it may result in an invalid XPath expression if, for example, the entered value isn't a number.



来源:https://stackoverflow.com/questions/15448878/less-than-greater-than-equal-to-not-working-as-expected-in-xpath

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!