Add xsl sheet and start with xslt processor

This commit is contained in:
hodasemi 2018-11-12 16:14:34 +01:00
parent 06b5973624
commit b63520e9fb
3 changed files with 84 additions and 10 deletions

View file

@ -1,7 +1,15 @@
<?php
/**
* Created by PhpStorm.
* User: Selin
* Date: 10.11.2018
* Time: 16:00
*/
require 'world_data_parser.php';
$parser = new WorldDataParser();
$csv_file = 'resources/world_data_v1.csv';
$result = $parser->saveXML($parser->parseCSV($csv_file));
if ($result) {
$parser->printXML('world_data.xml', 'resources/world_data_sheet.xslt');
// TODO: display table
} else {
echo "Failed saving xml file";
}

View file

@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h1>World Data Overview ...</h1>
<table border=1>
<tr>
<th>id</th>
<th>name</th>
<th>birth_rate_per_1000</th>
<th>cell_phones_per_100</th>
<th>children_per_woman</th>
<th>electricity_consumption_per_capita</th>
<th>gdp_per_capita</th>
<th>gdp__per__capita__growth</th>
<th>inflation_annual</th>
<th>internet_user_per_100</th>
<th>life_expectancy</th>
<th>military_expenditure_percent_of_gdp</th>
<th>gps_lat</th>
<th>gps_long</th>
</tr>
<xsl:for-each select="Countries/Country">
<tr>
<td><xsl:value-of select="id"/></td>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="birth_rate_per_1000"/></td>
<td><xsl:value-of select="cell_phones_per_100"/></td>
<td><xsl:value-of select="children_per_woman"/></td>
<td><xsl:value-of select="electricity_consumption_per_capita"/></td>
<td><xsl:value-of select="gdp_per_capita"/></td>
<td><xsl:value-of select="gdp__per__capita__growth"/></td>
<td><xsl:value-of select="inflation_annual"/></td>
<td><xsl:value-of select="internet_user_per_100"/></td>
<td><xsl:value-of select="life_expectancy"/></td>
<td><xsl:value-of select="military_expenditure_percent_of_gdp"/></td>
<td><xsl:value-of select="gps_lat"/></td>
<td><xsl:value-of select="gps_long"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

View file

@ -22,8 +22,10 @@ class WorldDataParser {
}
function saveXML($data) {
if (file_exists("world_data.xml")) {
unlink("world_data.xml");
$file = 'world_data.xml';
if (file_exists($file)) {
unlink($file);
}
$xml = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
@ -48,10 +50,28 @@ class WorldDataParser {
}
$xml .= '</Countries>'. PHP_EOL;
$file = 'world_data.xml';
file_put_contents($file, $xml);
return file_exists("world_data.xml");
return file_exists($file);
}
function printXML($xml_file, $xslt_sheet) {
$xsl_doc = new DOMDocument();
$xsl_res = $xsl_doc->loadXML($xslt_sheet);
if ($xsl_res === false) {
echo "reading xsl sheet failed: {$xsl_res}";
return;
}
$xml_doc = new DOMDocument();
if ($xml_doc->loadXML($xml_file) === false) {
echo "reading world data xml failed";
return;
}
$xsl = new XSLTProcessor();
$xsl->importStyleSheet($xsl_doc);
echo $xsl->transformToXML($xml_doc);
}
}