admin管理员组

文章数量:1023187

this is my xml document:-

<root>
    <child_1 entity_id = "1" value="india">
        <child_2 entity_id = "2" value="gujarat">
            <child_3 entity_id = "3" value="Ahemdabad"/>
            <child_4 entity_id = "4" value="Surat"/>
            <child_5 entity_id = "5" value="Rajkot"/>           
        </child_2>
    </child_1>
</root>

this is my javascript html code:-

<html>
<head>
    <script src=".8.3/jquery.min.js"></script>
    <script>
        var xml;
        var ind_sex;
        $.get(
        "code.xml",
        null,
        function (data) {
            xml = data;
        },
        "xml"
    );
        function get_list() {
            var city = $('#name').val();
            alert(city);
            var xPath = '//*[@value = "city"]' + 
                            '/../../@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="name"></input>
    <input type="button" name="button" value="Search" onclick="get_list()">
    <div id="result">
    </div>
</body>
</html>

here i am try to input in textbox city name if its match on my xml file then its return me there country name.
i dont get any error but not getting result.
i think problem in my xPath pleasae help me out of this.

this is my xml document:-

<root>
    <child_1 entity_id = "1" value="india">
        <child_2 entity_id = "2" value="gujarat">
            <child_3 entity_id = "3" value="Ahemdabad"/>
            <child_4 entity_id = "4" value="Surat"/>
            <child_5 entity_id = "5" value="Rajkot"/>           
        </child_2>
    </child_1>
</root>

this is my javascript html code:-

<html>
<head>
    <script src="http://ajax.googleapis./ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script>
        var xml;
        var ind_sex;
        $.get(
        "code.xml",
        null,
        function (data) {
            xml = data;
        },
        "xml"
    );
        function get_list() {
            var city = $('#name').val();
            alert(city);
            var xPath = '//*[@value = "city"]' + 
                            '/../../@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="name"></input>
    <input type="button" name="button" value="Search" onclick="get_list()">
    <div id="result">
    </div>
</body>
</html>

here i am try to input in textbox city name if its match on my xml file then its return me there country name.
i dont get any error but not getting result.
i think problem in my xPath pleasae help me out of this.

Share Improve this question edited Apr 26, 2013 at 4:27 Dimitre Novatchev 244k27 gold badges303 silver badges437 bronze badges asked Apr 18, 2013 at 11:08 Jack PhpJack Php 5772 gold badges7 silver badges26 bronze badges 2
  • 2 You apparently replaced your question with something pletely different after someone tried answering it. Why not just start a new question? – Barmar Commented Apr 21, 2013 at 6:03
  • I pletely agree with @Barmar. It is not remendable to pletely change the question just on the day before the deadline. Please, ask a new question, if you have a new problem. – Dimitre Novatchev Commented Apr 26, 2013 at 4:30
Add a ment  | 

2 Answers 2

Reset to default 7 +50

Use:

var xPath = '//*[@value = "' + city + '"]/../../@value';

An equivalent expression is:

var xPath = '//*[*/*[@value = "' + city +  ']]/@value';

As far as I remember, lists don't work like that... they're variables associated with a given value, so it's more like:

list($a, $b, $c) = [1, 2, 3];

Anyway, why don't you take a look at the 'Lists' section in the phpredis page? It's one of the remended PHP clients for Redis, and its examples are quite clear

this is my xml document:-

<root>
    <child_1 entity_id = "1" value="india">
        <child_2 entity_id = "2" value="gujarat">
            <child_3 entity_id = "3" value="Ahemdabad"/>
            <child_4 entity_id = "4" value="Surat"/>
            <child_5 entity_id = "5" value="Rajkot"/>           
        </child_2>
    </child_1>
</root>

this is my javascript html code:-

<html>
<head>
    <script src=".8.3/jquery.min.js"></script>
    <script>
        var xml;
        var ind_sex;
        $.get(
        "code.xml",
        null,
        function (data) {
            xml = data;
        },
        "xml"
    );
        function get_list() {
            var city = $('#name').val();
            alert(city);
            var xPath = '//*[@value = "city"]' + 
                            '/../../@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="name"></input>
    <input type="button" name="button" value="Search" onclick="get_list()">
    <div id="result">
    </div>
</body>
</html>

here i am try to input in textbox city name if its match on my xml file then its return me there country name.
i dont get any error but not getting result.
i think problem in my xPath pleasae help me out of this.

this is my xml document:-

<root>
    <child_1 entity_id = "1" value="india">
        <child_2 entity_id = "2" value="gujarat">
            <child_3 entity_id = "3" value="Ahemdabad"/>
            <child_4 entity_id = "4" value="Surat"/>
            <child_5 entity_id = "5" value="Rajkot"/>           
        </child_2>
    </child_1>
</root>

this is my javascript html code:-

<html>
<head>
    <script src="http://ajax.googleapis./ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script>
        var xml;
        var ind_sex;
        $.get(
        "code.xml",
        null,
        function (data) {
            xml = data;
        },
        "xml"
    );
        function get_list() {
            var city = $('#name').val();
            alert(city);
            var xPath = '//*[@value = "city"]' + 
                            '/../../@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="name"></input>
    <input type="button" name="button" value="Search" onclick="get_list()">
    <div id="result">
    </div>
</body>
</html>

here i am try to input in textbox city name if its match on my xml file then its return me there country name.
i dont get any error but not getting result.
i think problem in my xPath pleasae help me out of this.

Share Improve this question edited Apr 26, 2013 at 4:27 Dimitre Novatchev 244k27 gold badges303 silver badges437 bronze badges asked Apr 18, 2013 at 11:08 Jack PhpJack Php 5772 gold badges7 silver badges26 bronze badges 2
  • 2 You apparently replaced your question with something pletely different after someone tried answering it. Why not just start a new question? – Barmar Commented Apr 21, 2013 at 6:03
  • I pletely agree with @Barmar. It is not remendable to pletely change the question just on the day before the deadline. Please, ask a new question, if you have a new problem. – Dimitre Novatchev Commented Apr 26, 2013 at 4:30
Add a ment  | 

2 Answers 2

Reset to default 7 +50

Use:

var xPath = '//*[@value = "' + city + '"]/../../@value';

An equivalent expression is:

var xPath = '//*[*/*[@value = "' + city +  ']]/@value';

As far as I remember, lists don't work like that... they're variables associated with a given value, so it's more like:

list($a, $b, $c) = [1, 2, 3];

Anyway, why don't you take a look at the 'Lists' section in the phpredis page? It's one of the remended PHP clients for Redis, and its examples are quite clear

本文标签: javascriptget parent element attribute valueStack Overflow