yahoo place finder api 响应xml显示 [英] yahoo place finder api response xml display

查看:25
本文介绍了yahoo place finder api 响应xml显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里尝试使用 yahoo place finder api.我想获取给定地名的特定位置的纬度和经度.当我使用 place finder api 时,我得到了 xml 中的响应文本,我尝试使用 Xslt 对其进行样式设置.

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.

我的问题:

http://where.yahooapis.com/geocode?location=701+First+Ave,+Sunnyvale,+CA&appid=myapiid

是获取请求,我在其中获取 xml 作为响应,我必须使用 xslt 设置样式并显示.

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>

这是我的 XSLT 样式表:

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>

从链接更新:雅虎回复

<?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,

您的样式表将始终输出一些 HTML,至少是一个表格,即使输入的 XML 错误或丢失.如果您甚至没有得到表,那么问题不在于输入的 XML 错误或丢失;而是加载或应用 XSLT 样式表有问题.确保样式表的 URL 正确.您有一个相对 URL(latitude.xsl"),因此请确保在相对于您正在访问的页面(而不是 yahooapis 页面)的 URL 的 URLlatitude.xsl"上可以访问样式表.

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).

您也可以在 xsl=loadXMLDoc("latitude.xsl"); 之后尝试测试/警报以确保加载了样式表.

You can also try a test/alert after the xsl=loadXMLDoc("latitude.xsl"); to make sure the stylesheet loaded.

(此外,'longitude' 在您的样式表中拼写错误,但在您成功加载样式表之前不会出现该问题.)

(Also, 'longitude' is misspelled in your stylesheet, but that problem won't show up until you are loading the stylesheet successfully.)

这篇关于yahoo place finder api 响应xml显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆