I am taking an xml file that looks like this
<FCDMC><rpt_info created="data generated 04/16/2013 16:45"/><gage_rain id="770" last_rpt="2013-04-16T14:22:11" min_10="0.00" min_30="0.00" hour_1="0.00" hour_3="0.00" hour_6="0.00" day_1="0.00" day_3="0.00" day_7="0.00" name="Tat Momolikot Dam" lat="032:39:04" long="111:55:41"/></FCDMC>
Using this xsl style sheet to change/modify the xml document.
<xsl:stylesheet version="1.0">
<xsl:output method="xml" encoding="utf-8" media-type="text/xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="rpt_info">
<xsl:element name="meta" select=".">
<xsl:for-each select="@created">
<xsl:element name="created" select=".">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
<xsl:template match="gage_rain">
<xsl:element name="data" select=".">
<xsl:for-each select="@id">
<xsl:element name="site" select=".">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
<xsl:for-each select="@lat">
<xsl:element name="latitude" select=".">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
<xsl:for-each select="@long">
<xsl:element name="longitude" select=".">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
<xsl:for-each select="@name">
<xsl:element name="name" select=".">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
<xsl:for-each select="@last_rpt">
<xsl:element name="last_rpt" select=".">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
<xsl:for-each select="@min_10">
<xsl:element name="rain" select=".">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
<xsl:for-each select="@min_30">
<xsl:element name="rain" select=".">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
<xsl:for-each select="@hour_1">
<xsl:element name="rain" select=".">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
<xsl:for-each select="@hour_3">
<xsl:element name="rain" select=".">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
<xsl:for-each select="@hour_6">
<xsl:element name="rain" select=".">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
<xsl:for-each select="@day_1">
<xsl:element name="rain" select=".">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
<xsl:for-each select="@day_3">
<xsl:element name="rain" select=".">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
<xsl:for-each select="@day_7">
<xsl:element name="rain" select=".">
<xsl:value-of select="."/>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Than I am using PHP to output the new xml file
<?php
header('Content-Type: application/xml');
$xml = new DOMDocument;
$xml->load('http://alert.fcd.maricopa.gov/alert/Google/xml/fcdmc_alert_rain.xml');
$xsl = new DOMDocument;
$xsl->load('http://alert.fcd.maricopa.gov/alert/Google/v3/xslt/fcdmc_alert_rain.xsl');
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);
echo $proc->transformToXML($xml);
?>
and this php to output JSON
<?php
$xml = simplexml_load_file('http://alert.fcd.maricopa.gov/alert/Google/v3/php/rainfall_data.php');
$json = json_encode($xml);
echo $json;
?>
This is my current JSON output
{"meta":{"created":"04-18-2013 12:45"},"data":[{"site":"770","latitude":"032:39:04","longitude":"111:55:41","name":"Tat Momolikot Dam","last_rpt":"2013-04-18T11:22:11","rain":["0.00","0.00","0.00","0.00","0.00","0.00","0.00","0.00"]}]}
This is what I need my JSON output to look like. I need to remove the double quotes ("")that are around the 0.00 values.
{"meta":{"created":"04-18-2013 12:45"},"data":[{"site":"770","latitude":"032:39:04","longitude":"111:55:41","name":"Tat Momolikot Dam","last_rpt":"2013-04-18T11:22:11","rain":[0.00,0.00,0.00,0.00,0.00,0.00,0.00,0.00]}]}
How do I change the "rain"
:[string]?
Do I do it in xsl? In php? Thank you.
You json-encode a
SimpleXMLElement
which by default returns the element node values as strings.If you want to change this behavior, you need to extend from it and change the way it encodes the object for json, e.g. the rain array (if it exists) should be converted to float values:
Your script then only needs the little change to hint the loading function to use the class with the changed serialization behavior:
And that's it already.
what about
As of php 5.3.3 you can pass a
JSON_NUMERIC_CHECK
flag to json_encode that will check if a value is numeric and encode the json string with a number instead of a string.Edit:
Per my last comment, using string replace, this would work:
http://codepad.viper-7.com/hiWxjH