-1

这是我的 xml 示例:-

<products>
    <product_id value="1">
        <tab_id value="351">
            <date value="30-09-1989" />
        </tab_id>
    </product_id>   
    <product_id value="2">
        <tab_id value="352">
            <date value="03-03-1990" />
        </tab_id>
    </product_id>
</products>

这是我试过的:-

<html>
    <head>
    <title>Less,Grater,Equal</title>
    <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_list()
            {
                var ename = $('#select').val();
                var xPath = '//*["' + ename + '" = ' +
                            ' //date/@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>
        <body>
        <input type="text" id="select">
        <input type="button" value="search">
        <div id="result"></div>
        </body>
    </head>
</html>

在这里,如果用户在文本框中输入日期,我想 o/p 然后它在 xml 树上的匹配,它的 soud 返回 xml 树的第一个元素属性值(product_id 值)o/p 是1

4

1 回答 1

0

改变这个:

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();
}

对此:

var node = xml.evaluate(xPath, xml.documentElement, null,
                  XPathResult.FIRST_ORDERED_NODE_TYPE, null);
var str = node.singleNodeValue ? node.singleNodeValue.textContent : '';

应该这样做。

于 2013-03-09T11:55:16.593 回答