fork download
  1. <?php
  2.  
  3. // pretend these are the vars coming in
  4. $_GET['name'] = 'England';
  5. $_GET['area'] = 'Cleveland';
  6. $_GET['type'] = 'Homocide';
  7.  
  8. $xmlstr = <<<XML
  9. <?xml version='1.0' encoding='UTF-8'?>
  10.  
  11. <crimes year='2013' xmlns:xsi="http://w...content-available-to-author-only...3.org/2001/XMLSchema-instance"
  12.  xsi:noNamespaceSchemaLocation="stats.xsd">
  13.   <data country='England' region='North East Region' area='Cleveland'>
  14.   <heading name='Victim-based crime'>
  15.   <category name='Violence against the person'>
  16.   <number type='Homocide' total='3'/>
  17.   <number type='Violence with injury' total='3737'/>
  18.   <number type='Violence without injury' total='2630'/>
  19.   </category>
  20.   <category type='Sexual offences' total='563' />
  21.   <category type='Robbery' total='259' />
  22.   <category type='Theft offences'>
  23.   <number type='Burglary' total='4561' />
  24.   <number type='Domestic Burglary' total='2054' />
  25.   <number type='Non-domestic burglary' total='2057' />
  26.   <number type='Vehicle offences' total='3329' />
  27.   <number type='Theft from the person' total='372' />
  28.   <number type='Bycycle theft' total='994' />
  29.   <number type='Shoplifting' total='5174' />
  30.   <number type='All other theft offences' total='5381' />
  31.   </category>
  32.   <category type='Criminal damage and arson' total='7934' />
  33.   </heading>
  34.   <heading name='Other crimes against society'>
  35.   <category type='Drug offences' total='2116' />
  36.   <category type='Possession of weapons offences' total='267' />
  37.   <category type='Public order offences' total='1342' />
  38.   <category type='Miscellaneous crimes against society' total='405' />
  39.   <category type='Fraud' total='405' />
  40.   </heading>
  41.   </data>
  42. </crimes>
  43. XML;
  44.  
  45. $xml = simplexml_load_string($xmlstr);
  46.  
  47. $xpath = sprintf(
  48. '/crimes/data[@country="%s"][@area="%s"]/heading/category/number[@type="%s"]',
  49. $_GET['name'],
  50. $_GET['area'],
  51. $_GET['type']
  52. );
  53.  
  54. $nodeList = $xml->xpath($xpath);
  55. echo 'Total: ', $nodeList[0]['total'];
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
Total: 3