function print_rspec($obj, $pretty, $filterToAM)
{
    $args = array_keys($obj);
    // How many AMs reported actual results
    $amc = 0;
    foreach ($args as $arg) {
        if (is_array($obj[$arg]) and array_key_exists('value', $obj[$arg]) and array_key_exists('code', $obj[$arg]) and is_array($obj[$arg]['code']) and array_key_exists('geni_code', $obj[$arg]['code']) and $obj[$arg]['code']['geni_code'] == 0) {
            $amc = $amc + 1;
        }
    }
    foreach ($args as $arg) {
        $arg_url = $arg;
        $am_id = am_id($arg_url);
        $arg_name = am_name($arg_url);
        $arg_urn = am_urn($arg_url);
        if (is_array($obj[$arg]) and array_key_exists('value', $obj[$arg])) {
            $xml = $obj[$arg]['value'];
        } else {
            $xml = "";
        }
        $code = -1;
        if (is_array($obj[$arg]) and array_key_exists('code', $obj[$arg]) and is_array($obj[$arg]['code']) and array_key_exists('geni_code', $obj[$arg]['code'])) {
            $code = $obj[$arg]['code']['geni_code'];
        }
        if (is_array($obj[$arg]) and array_key_exists('output', $obj[$arg])) {
            $output = $obj[$arg]['output'];
        } else {
            if (!is_array($obj[$arg]) or !array_key_exists('code', $obj[$arg])) {
                $output = (string) $obj[$arg];
            } else {
                $output = "";
            }
        }
        /* If pretty, keep output clean by only printing RSpec for
              aggregates which have a slice (ie code!=12 or code !==2).
              Also don't print if no code was returned (ie code!=-1) because
              something catastrophic happened.
              -- unless there are no aggregates with resources, in which case
              we print the error.
           */
        // error_log("Aggregate listresources code is " . $code);
        if (!(($code == -1 or $code == 12 or $code == 2) and $pretty)) {
            if ($pretty) {
                print "<div class='aggregate' id='aggT_" . $am_id . "'>Aggregate <b>" . $arg_name . "'s</b> Resources:</div>";
            } else {
                print "<div class='aggregate' id='aggT_" . $am_id . "'>Aggregate <b>" . $arg_name . "'s</b> Raw Resources:</div>";
            }
            print "<div class='resources' id='agg_" . $am_id . "'>";
            if ($code == 0) {
                if ($pretty) {
                    /* Parsed into a table */
                    print_rspec_pretty($xml, False, $filterToAM, $arg_urn, $am_id);
                } else {
                    //	  /* As plain XML but with newlines after each block */
                    //	  $xml = str_replace(">", ">\n", $xml);
                    print_xml($xml);
                }
            } else {
                if ($output == "") {
                    $output = "[None. Return code: " . $code . "]";
                }
                echo "<p>Returned: <i>{$output}</i></p>";
            }
            print "</div>\n";
        }
    }
}
function make_pretty_results_table($xml, $am_id)
{
    // set AM if exists
    if ($am_id) {
        $am = get_service_by_id($am_id);
        $am_url = $am[SR_ARGUMENT::SERVICE_URL];
        $am_urn = am_urn($am_url);
        $filterToAM = True;
    } else {
        $am_urn = "";
        $filterToAM = False;
    }
    $manifestOnly = True;
    ob_start();
    $obj = print_rspec_pretty($xml, $manifestOnly, $filterToAM, $am_urn, $am_id);
    return ob_get_clean();
}