You are here:  » Only display elements based on matching fields


Only display elements based on matching fields

Submitted by uhsaa on Tue, 2013-03-19 04:21 in

Hi,

I am brand new to XML. How would I go about only showing elements with matching
"League" values from the below XML feed? Thank you for any help.

<Result>
<Standings>
<Division Name="Class 2A">
<Team Name="Parowan" City="Parowan" State="UT" IsMemberSchool="Yes" League="2A Region 13" Wins="2" Losses="0" Ties="0" WinningPercentage="1.000" PointsFor="9" PointsAgainst="4" LeagueWins="1" LeagueLosses="0" LeagueTies="0" LeagueWinningPercentage="1.000" LeaguePointsFor="6" LeaguePointsAgainst="3"/>
<Team Name="Beaver" City="Beaver" State="UT" IsMemberSchool="Yes" League="2A Region 13" Wins="2" Losses="0" Ties="0" WinningPercentage="1.000" PointsFor="16" PointsAgainst="3" LeagueWins="1" LeagueLosses="0" LeagueTies="0" LeagueWinningPercentage="1.000" LeaguePointsFor="7" LeaguePointsAgainst="1"/>
<Team Name="Gunnison Valley" City="Gunnison" State="UT" IsMemberSchool="Yes" League="2A Region 16" Wins="5" Losses="1" Ties="0" WinningPercentage="0.833" PointsFor="35" PointsAgainst="28" LeagueWins="0" LeagueLosses="1" LeagueTies="0" LeagueWinningPercentage="0.000" LeaguePointsFor="1" LeaguePointsAgainst="12"/>
<Team Name="Manti" City="Manti" State="UT" IsMemberSchool="Yes" League="2A Region 16" Wins="3" Losses="1" Ties="0" WinningPercentage="0.750" PointsFor="35" PointsAgainst="19" LeagueWins="1" LeagueLosses="0" LeagueTies="0" LeagueWinningPercentage="1.000" LeaguePointsFor="12" LeaguePointsAgainst="1"/>
<Team Name="Millard" City="Fillmore" State="UT" IsMemberSchool="Yes" League="2A Region 13" Wins="3" Losses="1" Ties="0" WinningPercentage="0.750" PointsFor="41" PointsAgainst="7" LeagueWins="1" LeagueLosses="1" LeagueTies="0" LeagueWinningPercentage="0.500" LeaguePointsFor="22" LeaguePointsAgainst="7"/>
<Team Name="Emery" City="Castle Dale" State="UT" IsMemberSchool="Yes" League="2A Region 15" Wins="3" Losses="1" Ties="0" WinningPercentage="0.750" PointsFor="27" PointsAgainst="16" LeagueWins="0" LeagueLosses="0" LeagueTies="0" LeagueWinningPercentage="0.000" LeaguePointsFor="0" LeaguePointsAgainst="0"/>
<Team Name="Diamond Ranch Academy" City="Hurricane" State="UT" IsMemberSchool="Yes" League="2A Region 13" Wins="2" Losses="1" Ties="0" WinningPercentage="0.667" PointsFor="29" PointsAgainst="19" LeagueWins="2" LeagueLosses="0" LeagueTies="0" LeagueWinningPercentage="1.000" LeaguePointsFor="21" LeaguePointsAgainst="10"/>
<Team Name="North Sevier" City="Salina" State="UT" IsMemberSchool="Yes" League="2A Region 16" Wins="2" Losses="1" Ties="0" WinningPercentage="0.667" PointsFor="17" PointsAgainst="23" LeagueWins="0" LeagueLosses="0" LeagueTies="0" LeagueWinningPercentage="0.000" LeaguePointsFor="0" LeaguePointsAgainst="0"/>
<Team Name="Grand County" City="Moab" State="UT" IsMemberSchool="Yes" League="2A Region 15" Wins="3" Losses="2" Ties="0" WinningPercentage="0.600" PointsFor="45" PointsAgainst="18" LeagueWins="0" LeagueLosses="0" LeagueTies="0" LeagueWinningPercentage="0.000" LeaguePointsFor="0" LeaguePointsAgainst="0"/>
<Team Name="American Leadership Academy" City="Spanish Fork" State="UT" IsMemberSchool="Yes" League="2A Region 16" Wins="2" Losses="2" Ties="0" WinningPercentage="0.500" PointsFor="19" PointsAgainst="11" LeagueWins="0" LeagueLosses="0" LeagueTies="0" LeagueWinningPercentage="0.000" LeaguePointsFor="0" LeaguePointsAgainst="0"/>
<Team Name="Richfield" City="Richfield" State="UT" IsMemberSchool="Yes" League="2A Region 15" Wins="1" Losses="1" Ties="0" WinningPercentage="0.500" PointsFor="30" PointsAgainst="9" LeagueWins="0" LeagueLosses="0" LeagueTies="0" LeagueWinningPercentage="0.000" LeaguePointsFor="0" LeaguePointsAgainst="0"/>
<Team Name="Enterprise" City="Enterprise" State="UT" IsMemberSchool="Yes" League="2A Region 13" Wins="4" Losses="4" Ties="0" WinningPercentage="0.500" PointsFor="56" PointsAgainst="37" LeagueWins="0" LeagueLosses="2" LeagueTies="0" LeagueWinningPercentage="0.000" LeaguePointsFor="8" LeaguePointsAgainst="15"/>
<Team Name="San Juan" City="Blanding" State="UT" IsMemberSchool="Yes" League="2A Region 15" Wins="1" Losses="2" Ties="0" WinningPercentage="0.333" PointsFor="15" PointsAgainst="25" LeagueWins="0" LeagueLosses="0" LeagueTies="0" LeagueWinningPercentage="0.000" LeaguePointsFor="0" LeaguePointsAgainst="0"/>
<Team Name="South Sevier" City="Monroe" State="UT" IsMemberSchool="Yes" League="2A Region 15" Wins="1" Losses="3" Ties="0" WinningPercentage="0.250" PointsFor="14" PointsAgainst="35" LeagueWins="0" LeagueLosses="0" LeagueTies="0" LeagueWinningPercentage="0.000" LeaguePointsFor="0" LeaguePointsAgainst="0"/>
<Team Name="North Summit" City="Coalville" State="UT" IsMemberSchool="Yes" League="2A Region 14" Wins="1" Losses="4" Ties="0" WinningPercentage="0.200" PointsFor="27" PointsAgainst="40" LeagueWins="0" LeagueLosses="0" LeagueTies="0" LeagueWinningPercentage="0.000" LeaguePointsFor="0" LeaguePointsAgainst="0"/>
<Team Name="Monticello" City="Monticello" State="UT" IsMemberSchool="Yes" League="2A Region 15" Wins="0" Losses="1" Ties="0" WinningPercentage="0.000" PointsFor="5" PointsAgainst="8" LeagueWins="0" LeagueLosses="0" LeagueTies="0" LeagueWinningPercentage="0.000" LeaguePointsFor="0" LeaguePointsAgainst="0"/>
<Team Name="South Summit" City="Kamas" State="UT" IsMemberSchool="Yes" League="2A Region 14" Wins="0" Losses="2" Ties="0" WinningPercentage="0.000" PointsFor="6" PointsAgainst="35" LeagueWins="0" LeagueLosses="0" LeagueTies="0" LeagueWinningPercentage="0.000" LeaguePointsFor="0" LeaguePointsAgainst="0"/>
<Team Name="Milford" City="Milford" State="UT" IsMemberSchool="Yes" League="2A Region 13" Wins="0" Losses="2" Ties="0" WinningPercentage="0.000" PointsFor="4" PointsAgainst="32" LeagueWins="0" LeagueLosses="2" LeagueTies="0" LeagueWinningPercentage="0.000" LeaguePointsFor="4" LeaguePointsAgainst="32"/>
<Team Name="Wasatch Academy" City="Mt. Pleasant" State="UT" IsMemberSchool="Yes" League="2A Region 16" Wins="0" Losses="0" Ties="0" WinningPercentage="0.000" PointsFor="0" PointsAgainst="0" LeagueWins="0" LeagueLosses="0" LeagueTies="0" LeagueWinningPercentage="0.000" LeaguePointsFor="0" LeaguePointsAgainst="0"/>
<Team Name="Kanab" City="Kanab" State="UT" IsMemberSchool="Yes" League="2A Region 13" Wins="0" Losses="1" Ties="0" WinningPercentage="0.000" PointsFor="5" PointsAgainst="9" LeagueWins="0" LeagueLosses="0" LeagueTies="0" LeagueWinningPercentage="0.000" LeaguePointsFor="0" LeaguePointsAgainst="0"/>
<Team Name="Rowland Hall-St. Marks" City="Salt Lake City" State="UT" IsMemberSchool="Yes" League="2A Region 14" Wins="0" Losses="0" Ties="0" WinningPercentage="0" PointsFor="0" PointsAgainst="0" LeagueWins="0" LeagueLosses="0" LeagueTies="0" LeagueWinningPercentage="0" LeaguePointsFor="0" LeaguePointsAgainst="0"/>
</Division>

I have the PHP to return all elements on the page and formatted correctly,
I just don't know how to only select specific leagues. Thanks

Submitted by support on Tue, 2013-03-19 08:44

Hi uhsaa, and welcome to the forum.

This XML format is straight forward to use with Magic Parser, and the Format String which you have probably discovered already using the demo tool is

xml|RESULT/STANDINGS/DIVISION/TEAM/

So let's say you have this XML saved locally as division.xml, to process only recordsmatching the league name, for example, "2A Region 14" all you need to do check the $record["TEAM-LEAGUE"] variable within your myRecordHandler function before deciding whether to process the record or not. Consider the following example:

<?php
  
require("MagicParser.php");
  function 
myRecordHandler($record)
  {
    if (
$record["TEAM-LEAGUE"]<>"2A Region 14") return;
    print 
"<h2>".$record["TEAM-NAME"]."</h2>";
    print 
"<h3>".$record["TEAM-CITY"].",".$record["TEAM-STATE"]."</h3>";
    print 
"<table>";
    print 
"<tr><th>Wins</th><td>".$record["TEAM-WINS"]."</td></tr>";
    print 
"<tr><th>Losses</th><td>".$record["TEAM-LOSSES"]."</td></tr>";
    print 
"<tr><th>Ties</th><td>".$record["TEAM-TIES"]."</td></tr>";
    print 
"</table>";
  }
  
MagicParser_parse("division.xml","myRecordHandler","xml|RESULT/STANDINGS/DIVISION/TEAM/");
?>

Hope this helps!
Cheers,
David
--
MagicParser.com

Submitted by uhsaa on Tue, 2013-03-19 15:08

This is great thank you so much. This leads me to my next question of if it is possible to run the require MagicParser script more than once on a page. For instance at {link saved} you will see I am trying to display results containing 2A Region 14 under the Team-League followed then by 2A Region 15, but it's not quite working. It pulls only the first record and then stops. Can you point me in the right direction on this? I've played around a bit trying to rename/copy the original Magic Parser script so it wasn't loading the same exact page twice, but I've hit a wall. Below is the code behind the page at the previously mentioned url. Thank you so much, this product was definitely worth the price.

{code saved}

Submitted by support on Tue, 2013-03-19 16:13

Hi!

Rather than make the second parse from within the first parse's record handler, it's best to use just a single myRecordHandler function; and put the TEAM-LEAGUE you want to display inside a global variable. For your header and footer code that goes before and after each team, use a header() and footer() function, that also use the same global variable name to display the TEAM-LEAGUE.

Also, since parsing the same source within a single script, to save making 2 requests to the remote server, the XML can be loaded into a string, and passed to MagicParser_parse() using string://

Consider the following example:

<?php
  require("MagicParser.php");
  function myHeader()
  {
    global $teamLeague;
?>
<!-- header HTML per league here -->
<h1><?php print $teamLeague?></h1>
<?
  }
  function myRecordHandler($record)
  {
    global $teamLeague;
    if ($record["TEAM-LEAGUE"]<>$teamLeague) return;
    print "<h2>".$record["TEAM-NAME"]."</h2>";
    print "<h3>".$record["TEAM-CITY"].",".$record["TEAM-STATE"]."</h3>";
    print "<table>";
    print "<tr><th>Wins</th><td>".$record["TEAM-WINS"]."</td></tr>";
    print "<tr><th>Losses</th><td>".$record["TEAM-LOSSES"]."</td></tr>";
    print "<tr><th>Ties</th><td>".$record["TEAM-TIES"]."</td></tr>";
    print "</table>";
  }
  function myFooter()
  {
?>
<!-- footer HTML per league here -->
<hr />
<?
  }
  $xml = file_get_contents("http://www.example.com/division.xml");
  $teamLeague = "2A Region 14";
  myHeader();
  MagicParser_parse("string://".$xml,"myRecordHandler","xml|RESULT/STANDINGS/DIVISION/TEAM/");
  myFooter();
  $teamLeague = "2A Region 15";
  myHeader();
  MagicParser_parse("string://".$xml,"myRecordHandler","xml|RESULT/STANDINGS/DIVISION/TEAM/");
  myFooter();
?>

Simply replace the URL in the file_get_contents() line with your actual XML source, and that should be pretty close...

Hope this helps!
Cheers,
David
--
MagicParser.com