雅虎的地方取景器API响应XML显示 [英] yahoo place finder api response xml display

查看:138
本文介绍了雅虎的地方取景器API响应XML显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里试图与雅虎的地方取景器API工作。
我想获得的地方的特定定位置名称的纬度和经度。
当我使用的地方取景器API,我得到XML响应文本和IM试图用XSLT样式了。

我的问题:

<$p$p><$c$c>http://where.yahooapis.com/geo$c$c?location=701+First+Ave,+Sunnyvale,+CA&appid=myapiid

是GET请求在那里我得到一个XML作为回应,我必须使用XSLT和显示风格。

我有一个$ C $其中,C这确实that.I相信这是正确的。我总是得到一个空白页

 &LT; HTML和GT;
&LT; HEAD&GT;
&LT;脚本&GT;
函数loadXMLDoc(DNAME)
{
如果(window.XMLHtt prequest)
  {
  xhttp =新XMLHtt prequest();
  }
其他
  {
  xhttp =新的ActiveXObject(Microsoft.XMLHTTP);
  }
xhttp.open(GET,DNAME,FALSE);
xhttp.send();
返回xhttp.responseXML;
}功能displayResult()
{
xml=loadXMLDoc(\"http://where.yahooapis.com/geo$c$c?location=701+First+Ave,+Sunnyvale,+CA&appid=MYAPIKEY\");
的xsl = loadXMLDoc(latitude.xsl);
// code IE浏览器
如果(window.ActiveXObject)
  {
  EX = xml.transformNode(XSL);
  。的document.getElementById(榜样)的innerHTML =前;
  }
// code的Mozilla,Firefox和Opera等
否则,如果(document.implementation&安培;&安培; document.implementation.createDocument)
  {
  XSLTProcessor对象=新XSLTProcessor中();
  xsltProcessor.importStylesheet(XSL);
  resultDocument = xsltProcessor.transformToFragment(XML,文件);
  的document.getElementById(榜样)的appendChild(resultDocument)。
  }
}
&LT; / SCRIPT&GT;
&LT; /头&GT;
&LT;身体的onload =displayResult()&GT;
&LT; D​​IV ID =榜样/&GT;
&LT; /身体GT;
&LT; / HTML&GT;

下面是我的XSLT样式表:

 &LT;?XML版本=1.0编码=ISO-8859-1&GT?;
&LT;的xsl:样式版本=1.0
的xmlns:XSL =htt​​p://www.w3.org/1999/XSL/Transform&GT;&LT;的xsl:模板匹配=/&GT;
  &LT; HTML和GT;
  &LT;身体GT;
  &LT; H2&GT;纬度经度取景器和LT; / H&GT;
  &LT;表边框=1&GT;
    &所述; TR BGCOLOR =#9acd32&GT;
      &LT;第i个纬度与LT; /第i
      &LT;第i个经度&LT; /第i
    &LT; / TR&GT;
    &所述; TR&GT;
      &LT; TD&GT;&LT; XSL:选择=结果/纬度/&GT值的;&LT; / TD&GT;
      &LT; TD&GT;&LT; XSL:选择=结果/经度/&GT值的;&LT; / TD&GT;
    &LT; / TR&GT;
  &LT; /表&gt;
  &LT; /身体GT;
  &LT; / HTML&GT;
&LT; / XSL:模板&GT;&LT; / XSL:样式&GT;

从链接更新:雅虎回应

 &LT;?XML版本=1.0编码=UTF-8&GT?;
&LT; ResultSet的版本=1.0&GT;
    &LT;错误&GT; 0℃; /错误&GT;
    &LT;&的ErrorMessage GT;没有错误&LT; /&的ErrorMessage GT;
    &LT;区域设置&GT; us_US&LT; /区域设置&GT;
    &LT;质量&GT; 87 LT; /质量&GT;
    &所述;实测值大于1&下; /实测&GT;
    &LT;结果&GT;
        &LT;质量与GT; 87 LT; /质量&GT;
        &LT;纬度和GT; 37.416275&LT; /纬度和GT;
        &LT;经度&GT; -122.025092&LT; /经度&GT;
        &LT; offsetlat&GT; 37.416397&LT; / offsetlat&GT;
        &LT; offsetlon&GT; -122.025055&LT; / offsetlon&GT;
        &所述;半径大于500℃/半径大于
        &LT;名称&gt;&LT; /名称&gt;
        &LT;一号线&GT; 701第一大道&LT; /线路1&GT;
        &LT; 2号线&GT;加利福尼亚州桑尼维尔,94089-1019&LT; / 2号线&GT;
        &LT; 3号线&GT;&LT; / 3号线&GT;
        &LT; 4号线&GT;美国&LT; / 4号线&GT;
        &LT;房子&GT; 701 LT; /房子&GT;
        &LT;街道&GT;第一大道&LT; /街道&GT;
        &LT;&XSTREET GT;&LT; / XSTREET&GT;
        &LT;&UNITTYPE GT;&LT; / UNITTYPE&GT;
        &所述;部&gt;&下; /部&gt;
        &LT;邮政&GT; 94089-1019&LT; /邮政&GT;
        &LT;&居委会GT;&LT; /居委会&GT;
        &LT;城市&GT;&桑尼维尔LT; /城市&GT;
        &LT;&县GT;圣克拉拉县和LT; /县&GT;
        &LT;州及GT;&加州LT; /州&GT;
        &LT;国家&GT;美国&LT; /国家&GT;
        &LT;国家code&GT;美国和LT; /国家code&GT;
        &LT;状态code&GT; CA&LT; /状态code&GT;
        &LT;县code&GT;&LT; /县code&GT;
        &LT; uzip&GT; 94089&LT; / uzip&GT;
        &LT;&哈希GT; DDAD1896CC0CDC41&LT; /散列&GT;
        &LT;&WOEID GT; 12797150&LT; / WOEID&GT;
        &所述; woetype&GT 11所述; / woetype&GT;
    &LT; /&结果GT;
&LT; / ResultSet的&GT;
&LT;! - gws30.maps.re3.yahoo.com uncom pressed /分块周三1月12日16点29分58秒的PST 2011 - &GT;


解决方案

鲍比,

您的样式表将一直输出一些HTML,至少一个表,即使输入XML错误或丢失。如果你没有得到,甚至一个表,那么问题不在于输入XML错误或丢失;而什么是错装或应用XSLT样式表。确保URL样式表是正确的。你有一个相对URL(latitude.xsl),所以一定要确保样式表的URL访问latitude.xsl相对于正在访问的页面(不是yahooapis页)的URL。

您也可以尝试在经过测试/警报XSL = loadXMLDoc(latitude.xsl); ,以确保样式表加载

(另外,'东经'拼写错误在你的样式表,但这个问题不会出现,直到你成功加载样式表。)

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

这篇关于雅虎的地方取景器API响应XML显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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