yahoo place finder api response xml display
Im here trying to work with yahoo place finder api. I want to get the latitude and longitude of a particular location given name of place. When i use place finder api i get the response text in xml and im trying to style it with Xslt.
My problem:
http://where.yahooapis.com/geocode?location=701+First+Ave,+Sunnyvale,+CA&appid=myapiid
is the get request where i get an xml as response which i have to style with xslt and display.
i have a code here which does that.I believe it is right .I always get a blank page
<html>
<head>
<script>
function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",dname,false);
xhttp.send("");
return xhttp.responseXML;
}
function displayResult()
{
xml=loadXMLDoc("http://where.yahooapis.com/geocode?location=701+First+Ave,+Sunnyvale,+CA&appid=MYAPIKEY");
xsl=loadXMLDoc("latitude.xsl");
// code for IE
if (window.ActiveXObject)
{
ex=xml.transformNode(xsl);
document.getElementById("example").innerHTML=ex;
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
{
xsltProcessor=new XSLTProcessor();
xsltProcessor.importStylesheet(xsl);
resultDocument = xsltProcessor.transformToFragment(xml,document);
document.getElementById("example").appendChild(resultDocument);
}
}
</script>
</head>
<body onload="displayResult()">
<div id="example" />
</body>
</html>
Here is my XSLT stylesheet:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Latitude Longitute finder</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Latitude</th>
<th>Longitude</th>
</tr>
<tr>
<td><xsl:value-of select="Result/latitude"/></td>
<td><xsl:value-of select="Result/longitute"/></td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Update from link: Yahoo response
<?xml version="1.0" encoding="UTF-8"?>
<ResultSet version="1.0">
<Error>0</Error>
<ErrorMessage>No error</ErrorMessage>
<Locale>us_US</Locale>
<Quality>87</Quality>
<Found>1</Found>
<Result>
<quality>87</quality>
<latitude>37.416275</latitude>
<longitude>-122.025092</longitude>
<offsetlat>37.416397</offsetlat>
<offsetlon>-122.025055</offsetlon>
<radius>500</radius>
<name></name>
<line1>701 1st Ave</line1>
<line2>Sunnyvale, CA 94089-1019</line2>
<line3></line3>
<line4>United States</line4>
<house>701</house>
<street>1st Ave</street>
<xstreet></xstreet>
<unittype></unittype>
<unit></unit>
<postal>94089-1019</postal>
开发者_如何学运维 <neighborhood></neighborhood>
<city>Sunnyvale</city>
<county>Santa Clara County</county>
<state>California</state>
<country>United States</country>
<countrycode>US</countrycode>
<statecode>CA</statecode>
<countycode></countycode>
<uzip>94089</uzip>
<hash>DDAD1896CC0CDC41</hash>
<woeid>12797150</woeid>
<woetype>11</woetype>
</Result>
</ResultSet>
<!-- gws30.maps.re3.yahoo.com uncompressed/chunked Wed Jan 12 16:29:58 PST 2011 -->
Bobby,
Your stylesheet will always output some HTML, at least a table, even if the input XML is wrong or missing. If you're not even getting a table, then the problem is not that the input XML is wrong or missing; rather something is wrong with loading or applying the XSLT stylesheet. Make sure the URL to the stylesheet is correct. You have a relative URL ("latitude.xsl"), so make sure the stylesheet is accessible at the URL "latitude.xsl" relative to the URL of the page you are accessing (not the yahooapis page).
You can also try a test/alert after the xsl=loadXMLDoc("latitude.xsl");
to make sure the stylesheet loaded.
(Also, 'longitude' is misspelled in your stylesheet, but that problem won't show up until you are loading the stylesheet successfully.)
With the input source is clear that you need to change the pattern from
<xsl:template match="/">
To
<xsl:template match="/ResultSet">
精彩评论