如果投票&gt; XML仅显示<candidate> </candidate> 0

时间:2016-11-08 17:06:40

标签: javascript xml filter

我试图在拉特兰县展示<Candidate></Candidate>只有高于0的选票。现在它只是在拉特兰县展示它们。

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

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

到目前为止:

   $(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

1 个答案:

答案 0 :(得分:1)

看起来你似乎很随意,只是抓住元素并将它们串在一起。最好有条不紊地在一个城镇内办公室,然后是他们的候选人,然后通过投票等过滤那些候选人。

以下情况应该有效,但似乎目前在拉特兰一个城镇中只有一名候选人有任何选票:

//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>