9

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.

2
  • 2
    You apparently replaced your question with something completely different after someone tried answering it. Why not just start a new question? Commented Apr 21, 2013 at 6:03
  • I completely agree with @Barmar. It is not recommendable to completely change the question just on the day before the deadline. Please, ask a new question, if you have a new problem. Commented Apr 26, 2013 at 4:30

2 Answers 2

7
+50

Use:

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

An equivalent expression is:

var xPath = '//*[*/*[@value = "' + city +  ']]/@value';
Sign up to request clarification or add additional context in comments.

2 Comments

@DimitreNovatchev They're not equivalent, IMO. I'd rather use the second version as it doesn't require two way tree walking (also toward the root). I'm not sure if I could find any docs again, but something whispers that older implementations of xpath engines had issues with it. Also I'm not quite sure if you really mean using asterix. Shouldn't dots go therein, like var xPath = '//.[./.[@value = "' + city + ']]/@value';? Compare this.
@KrzysztofJabłoński, Yes, thesy are equivalent -- they select exactly the same attributes. And ("asterisk") /* means (on the child axis) any element child of the context node. Similarly, //* means any descendent element of the context node. And any compliant XPath implementation must support both expressions. On the other side //. means any node (not only element node) that is the context node or a descendent of the context node.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.