Jeff Jeff - 1 month ago 21
Javascript Question

XML only show <Candidate></Candidate> if votes > 0

I am trying to show

<Candidate></Candidate>
in Rutland County with only votes higher than 0. Right now it shows them all just in Rutland County.

https://jsfiddle.net/jeffd/yqbwaxts/3/

https://99centbeats.com/ResultsData.xml

So Far:

$(document).ready(function() {
$.ajax({
type: 'GET',
url: 'https://99centbeats.com/ResultsData.xml',
//url: 'https://vtelectionresults.sec.state.vt.us/rss/2713/ResultsData.xml',
crossDomain: true,
success: parseXml
});
});
function parseXml(xml) {
$(xml).find('Town').each(function() {
var county = $(this).find('County').text(); //change to county for 'County'
window.votes = $(this).find('Votes').text();
if (county == 'RUTLAND') { // change to 'RUTLAND' for Rutland County


if (votes !== '0'){


window.townname = $(this).find('TownName').text();
window.officename = $(this).find('OfficeName, Name, Votes');
var newItems = $.map(officename, function(i) {
$(i).filter('0')
return $(i).text();
});
newItems2 = ("" + newItems).replace(/,/g, " - ");
$(".marquee").append(' - <strong>' + townname + '</strong>' + ' - ' + newItems2);
}
}
});
$('.marquee').marquee({
duration: 5000 // Speed of the counter (Lower = Faster)
});
}
setTimeout(function() {
window.location.reload(1);
}, 300000); // Refresh Results Every 5 Minutes

Answer

It seems like you're going at this rather haphazardly, just grabbing elements and stringing them together. It's better to methodically get the offices within a town, then their candidates, then filter those candidates by votes, etc.

The following should work, though it seems that there's currently only one candidate in one Rutland town that has any votes:

//Must have Allow-Control-Allow-Origin chrome plugin installed to work

$(document).ready(function() {
  $.ajax({
    type: 'GET',
    url: 'https://99centbeats.com/ResultsData.xml',
    //url: 'https://vtelectionresults.sec.state.vt.us/rss/2713/ResultsData.xml',
    crossDomain: true,
    success: parseXml
  });
});

function textForOffice(office) {
  var candidates = $(office).find('Candidate').filter(function(i, el) {
    return $(el).find('Votes').text() > 0;
  });

  if (candidates.length === 0) {
    return '';
  }

  var officeName = $(office).find('OfficeName').text();

  var candidateValues = candidates.toArray().map(function (el) {
      return $(el).find('Name').text() + ' - ' + $(el).find('Votes').text();
  });

  return ' - ' + officeName + ' - ' + candidateValues.join(' - ');
}

function parseXml(xml) {
  $(xml).find('Town').each(function() {
    var town = $(this);
    var county = town.find('County').text(); //change to county for 'County'

    if (county == 'RUTLAND') { // change to 'RUTLAND' for Rutland County
      var townname = town.find('TownName').text();

      var offices = town.find('Office');

      var textForOffices = offices.toArray().map(textForOffice);
      
      $(".marquee").append(' - <strong>' + townname + '</strong>' + textForOffices.join(''));
    }
  });
  $('.marquee').marquee({
    duration: 5000 // Speed of the counter (Lower = Faster)
  });
}
setTimeout(function() {
  window.location.reload(1);
}, 300000); // Refresh Results Every 5 Minutes
.rssfeed {
  background-color: black;
  color: white;
  bottom: 0;
  font-family: Gotham, Helvetica Neue, Helvetica, Arial, " sans-serif";
  height: 150px;
  line-height: 150px;
  font-size: 32px;
}

.marquee {
  overflow: hidden;
  width: 100%;
  white-space: nowrap;
}

.description {
  width: 100%;
  height: 50px;
  background: red;
  color: white;
  line-height: 50px;
  text-align: center;
  font-family: Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif;
  font-size: 24px;
}
<div class="description">Rutland City - Vote Count Per Ward</div>
<div class="rssfeed">
  <div class="marquee"></div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.marquee/1.4.0/jquery.marquee.min.js"></script>