Find the Max Value for an XML Attribute, Across Records

699 Views Asked by At

Using JQuery & SPServices, I receive XML similar to the following:

<?xml version="1.0"?>
<soap:Envelope xmlns: . . .> <soap:Body>
<GetListItemsResponse xmlns=" ... ">
 <GetListItemsResult>
  <listitems xmlns:s=" ... >
   <rs:data ItemCount="4">
      <z:row  ows_CtID="5"  ows_ID="1"  ows_Ct="GR"   />
      <z:row  ows_CtID="9"  ows_ID="4"  ows_Ct="PC"   />
      <z:row  ows_CtID="2"  ows_ID="5"  ows_Ct="G"    />
      <z:row  ows_CtID="3"  ows_ID="7"  ows_Ct="O55"  />
   </rs:data>
  </listitems>
 </GetListItemsResult>
</GetListItemsResponse>
</soap:Body></soap:Envelope>

(I get a lot more, of course; this has been pared down for simplicity in showing the concept.)

You will note that the ordering is according to ows_ID, and that both ows_ID and ows_CtID have gaps in their values.

I want to get the min & max values of ows_CtID; I want “2” & “9,” in other words.

Using JavaScript, JQuery (and possibly SPServices), how can I get the Min and Max value of a specific attribute in the XML (ows_CtID, in this case)?

1

There are 1 best solutions below

0
On

You need to loop through all z:row's and keep a variable for min and max, updating them only when larger value or lesser value is found... Example, place the following inside the completefunc function of SPServices:

var min = null,
    max = null;

$(xData.responseXML).SPFilterNode("z:row").each(function() {

    var $this = $(this),
        val   = parseInt( $this.attr("ows_CtID") );

    if (min === null) {

        min = max = val;

    }

    if (val < min) {

        min = val;

    }

    if (val > max) {

        max = val;

    }

});

alert("Min: " + min + " | Max: " + max);

If your field (in this case, ows_CtID) is anything other an integer, then change the above to not use 'parseInt'... if it is a number with decimals, change it to parseFloat instead.

Paul.