Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get parent element attribute value

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.com/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.

like image 391
Jack Php Avatar asked Apr 18 '13 11:04

Jack Php


People also ask

How do you find the parent element?

To get the parent node of an HTML element, you can use the parentNode property. This property returns the parent node of the specified element as a Node object. The parentNode property is read-only, which means you can not modify it.

What is the difference between parentNode and parentElement?

parentNode gives the parent, while . parentElement gives undefined.


2 Answers

Use:

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

An equivalent expression is:

var xPath = '//*[*/*[@value = "' + city +  ']]/@value';
like image 77
Dimitre Novatchev Avatar answered Oct 21 '22 23:10

Dimitre Novatchev


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 recommended PHP clients for Redis, and its examples are quite clear

like image 27
Julio María Meca Hansen Avatar answered Oct 21 '22 22:10

Julio María Meca Hansen