Quote:
|
How does the parser know which is meant to be a decimal, which a string, which a date, etc.
|
As has been said above, they are all text. If you can tell me how you can find the difference between them using the schema please let me know. I don't think the schema tells you if something is a time, a number, etc..
When I started to work on this I was under the impression that things would be simple in php because numbers and string normally change depending on whats being done to them and if you multiplied two variables they would try to cast them self as numbers. And javascript would be harder cause they don't. WRONG
I made a simple xml file, called multi.xml:
Code:
<?xml version="1.0"?>
<stuff>
<area>
<id>First</id>
<base>100.25</base>
<multiplyfactor>.5</multiplyfactor>
</area>
<area>
<id>Second</id>
<base>50</base>
<multiplyfactor>0.5</multiplyfactor>
</area>
</stuff>
Then worked on it in js and then in php. Here is the results and hope this will help you if you need to preform math on xml elements.
First js:
Code:
<!DOCTYPE html>
<html>
<body>
<script>
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","multi.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
var x=xmlDoc.getElementsByTagName("area");
for (i=0; i<=x.length; i++)
{
var one = x[i].getElementsByTagName("id")[0].childNodes[0].nodeValue;
var two = x[i].getElementsByTagName("base")[0].childNodes[0].nodeValue;
var three = x[i].getElementsByTagName("multiplyfactor")[0].childNodes[0].nodeValue;
document.write(one+"<br />");
document.write(two+"<br />");
document.write(three+"<br />");
var four = three * two;
document.write(two+" * "+three+" = "+four+"<br />---------<br />");
}
</script>
</body>
</html>
Here's the same thing in php. NOTE: I had a problem with the math because php did not change the variable to a number when I tried to multiply them together.
PHP Code:
<?php
$xml = simplexml_load_file("multi.xml");
$area = $xml->area;
for($i = 0; $i < count($area); $i++)
{
$id = $area[$i]->id;
echo $id . "<br />";
$base = $area[$i]->base;
echo $base . "<br />";
$factor = $area[$i]->multiplyfactor;
echo $factor . "<br />";
if(!is_numeric($base)) $base = (float)$base;
if(!is_numeric($factor)) $factor = (float)$factor;
$answer = $base * $factor;
echo $answer;
echo '<br />===================================<br />';
}
?>
The "How do you know which is a number" question. You have to know if your going to use it to preform math.