Convert XML Via PHP & Javascript Function
I wonder whether someone could help me please.
I have an xml file which is made up of approx 22,000 OS Grid References, an extract is shown below.
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
- <Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
- <Row>
<Name>Deserted medieval village</Name>
<NGR>SS 开发者_运维问答00000 11111</NGR>
</Row>
What I need to do for each 'NGR' is convert it to a Lat & Lng co-ordinate. If I were to do this manually I'd key the NGR onto a HTML form I have and use the following Javascript code (from nearby.org.uk) on a click event to convert it. I will then save it to a table in a mySQL database.
function converttolatlng() {
var gr = document.getElementById('osgridref').value;
var osgb = new GT_OSGB();
if (osgb.parseGridRef(gr)) {
var wgs84 = osgb.getWGS84();
document.getElementById('osgb36lat').value = wgs84.latitude;
document.getElementById('osgb36lon').value = wgs84.longitude;
}
else {
document.getElementById('osgb36lat').value = "n/a";
document.getElementById('osgb36lon').value = "n/a";
}
}
Because I have so many, I'd like to be able to run this conversion automatically and from what I've read on the Internet I think the best way is to run the xml through PHP, but I must admit I'm not sure where to begin.
I just wondered if someone could perhaps take a look at this please and show me what I would need to do to create the extraction process.
Sincere thanks
Have you considered using an XML database, since your input document is XML? The code would then look like:
for $row in doc("input.xml")//Row
let $coordinates := parseGridRef($row/NGR)
(: assuming parseGridRef implements the conversion :)
return some-insertion-function("some-collection-name",
<entry>
<name>{$row}</name>
<latitude>{$coordinates[1]}</latitude>
<longitude>{$coordinates[2]}</longitude>
</entry>)
精彩评论