Jquery query XML document where clause

2019-06-07 16:25发布

问题:

I have an XML document that I want to search a specific date and get information for just that date. My XML looks like this:

    <month id="01">
        <day id="1">
            <eitem type="dayinfo">
                <caption>
                    <text lang="cy">f. 3 r.</text>
                    <text lang="en">f. 3 r.</text>
                </caption>
                <ref href="link" id="3"/>
                <thumb href="link" id="3"/>
            </eitem>
        </day>
        <day id="7">
            <eitem type="dayinfo">
                <caption>
                    <text lang="cy">f. 5 v.</text>
                    <text lang="en">f. 5 v.</text>
                </caption>
                <ref href="link" id="4"/>
                <thumb href="link" id="4"/>
            </eitem>
        </day>
        <day id="28">
            <eitem type="dayinfo2">
                <caption id="1">
                    <text lang="cy">test</text>
                    <text lang="en">test2</text>
                </caption>
                <ref href="link" id="1"/>
                <thumb href="link" id="1"/>
            </eitem>
        </day>
        <day id="28">
            <eitem type="dayinfo">
                <caption>
                    <text lang="cy">f. 14 v.</text>
                    <text lang="en">f. 14 v.</text>
                </caption>
                <ref href="link" id="20"/>
                <thumb href="link" id="20"/>
            </eitem>
        </day>
    </month>

My Jquery looks like this:

            $(xml).find('month[id=01]').each(function()
            {
                $(xml).find("day").each(function()
                {
                    var day = $(this).attr('id');
                    alert(day);
                });
            });

In the XML example above I only shown one month, however there are many more. In my JQuery I've tried to do 'Where month = 1' and get all the days info for that month, however that JQuery brings back days for every month. How do I do a where clause with JQuery/JavaScript on a XML document? thanks.

回答1:

   $(xml).find('month[id=01]').each(function()
        {
            $(this).find("day").each(function()
          //  ^^^^
            {
                var day = $(this).attr('id');
                alert(day);
            });
        });