0

I have this xml document.

<search-results>
    <physician>
        <name>some name</name>
        <address>
            <street>some street</street>
            <city>some city</city>
            <state>some state</state>
            <zip-code>some zip</zip-code>
        </address>
        <phone>some phone</phone>
        <url>some url</url>
        <logos>
            <logo type="1">logo1</logo>
            <logo type="2">logo2</logo>
        </logos>
    </physician>
</search-results>

I am able to list physicians that have a logo of type 1 using this code.

$(results).find('physician').each(function () {

          var $physician = $(this);
          var logos = $physician.children('logos');
          var logo = logos.children('logo');

          if (logo.attr('type') == "1")
          {

              var html = '<h3>' + $physician.children('name').text() + '</h3>';
              html += '<p>' + $physician.children('logos').children('logo').text() + '</p>';
              html += '<p>' + logo.attr('type') + '</p>';


              $('#results').append(html);
          }

      });

However, not all physicians have both logos.

I need to be able to list physicians that have a logo with an id of 1, physicians that have an id of 2 and then physicians that have both logos. I am having trouble finding the physicians that have both logos 1 and 2. Can anyone point me in the right direction?

1 Answer 1

1

If you specifically need to find and output physicians in the particular order you give (logo 1, log 2, then both), then your best bet is to pre-compute lists of each type of physician and then iterate over them in the order you need.

You could, for example, do something like this:

var physicians = $(results).find('physician');

var logo1 = physicians.filter(function ( ) {
    return $('logo[type="1"]', this).length > 0;
});
var logo2 = physicians.filter(function ( ) {
    return $('logo[type="2"]', this).length > 0;
});

var physiciansWithLogo1 = logo1.not(logo2);
var physiciansWithLogo2 = logo2.not(logo1);
var physiciansWithBothLogos = logo1.filter(logo2);

The first two filter expressions use filter functions to select those physicians which have a logo of type="1" and type="2" respectively (whether they have both kinds or not).

The next two lines use the not filter to select physicians with only one or the other type of logo. These are put into the physiciansWithLogo1 and physiciansWithLogo2 variables, which are jQuery objects containing the physicians which have only logos with type="1" and type="2" respectively.

The final line uses filter again to find the physicians containing both kinds of logo, which are put into a jQuery object physiciansWithBothLogos.

Once you've got those three objects, you can iterate over them (using each(), for example) and build your output as needed.

Sign up to request clarification or add additional context in comments.

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.