小于、大于、等于在 XPath 中没有按预期工作 [英] Less than, greater than, equal to not working as expected in XPath

查看:30
本文介绍了小于、大于、等于在 XPath 中没有按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 xml 文件:-

 <产品><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></产品>

这是我的 xpath:-

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

我有三个按钮和两个文本框,代码是:

<头><script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script><脚本>var xml;$.get("xml_converted.xml",空值,功能(数据){xml = 数据;},xml");函数 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+',") 和 '+' translate(@value," -:","")]'+'/../../@value';var iterator = xml.evaluate(xPath, xml.documentElement, null,XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null);var thisNode = iterator.iterateNext();var str = '';而(这个节点){如果(字符串){str += ', ';}str += thisNode.textContent;thisNode = iterator.iterateNext();}$("#result").text(str);}<身体><input type="text" id="select"><input type="text" id="value"><input type="button" name="button" value="小于" 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="结果">

</html>

使用此代码 o/p 是:-
我在第一个文本框中输入 dist_map
在第二个文本框 3(它的 dist_map 属性值)
然后点击小于按钮它不给出任何输出
如果单击大于按钮,则它会同时显示 1,2(其 product_id 属性值)
这不完美吗.
这里 o/p 只有 1(它的 product_id 属性值) bcoz 3 小于 5
如果单击等于按钮,则可以完美运行 2(其 product_id 属性值)b'coz 两个文本框值在 xml 文件中都匹配
这里只有在我的 xpath 或参数传递中的问题,例如 (<, >)
请检查我的这个问题并帮助我解决这个问题...
谢谢

解决方案

我真的不明白你试图通过调用 containsconcat 来达到什么目的和 translate 在您的 XPath 表达式中.但是,如果您只是想列出 dist_map 值小于、等于或大于给定值的产品,则应使用以下方法:

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

这将创建如下表达式:

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

请注意,这不会验证输入.因此,例如,如果输入的值不是数字,则可能会导致无效的 XPath 表达式.

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

解决方案

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.

这篇关于小于、大于、等于在 XPath 中没有按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆