我已经研究这个话题几个月了,但没有成功。许多帖子说这是可能的,但我还没有能够让它工作。
问题就在这里,我们正在使用一个提供XML格式数据文件的资源。可以在以下网址找到:http://www.idexonline.com/image_portal/Home/Graph/Base/IDEXOnlineDiamondIndex.xml
我想把这个数据插入到一个现有的网页。
我尝试过使用XSL、XML include和其他各种技术访问这些数据,但都没有成功。
我目前在这个网站上使用PHP。但是,我是开放的方法,它可以使用JavaScript或其他技术!的帮助!
谢谢
这对我有用:
<?php
$file = 'http://www.idexonline.com';
$file .= '/image_portal/Home/Graph/Base/IDEXOnlineDiamondIndex.xml';
$xml = file_get_contents($file);
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($xml);
$domXPath = new DOMXPath($xmlDoc);
$xPath = '/root/index';
$index = $domXPath->evaluate("string($xPath)");
$xPath = '/root/change';
$change = $domXPath->evaluate("string($xPath)");
$change = number_format(round($change, 2), 2);
echo "The index is '$index' and the change is '$change'";
当时的输出是这样的:
指数为'139.04',变化为'0.00'
如果网站没有屏蔽你,你可以获得如下数据
$xml = simplexml_load_file('http://www.idexonline.com/image_portal/Home/Graph/Base/IDEXOnlineDiamondIndex.xml');
print_r($xml);
我不太确定这个问题的正确解决方案。但是AFAIK从另一个位置访问数据需要远程机器上的跨域访问策略。维基百科对此的看法
这个XSLT转换:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pUrl" select=
"'http://www.idexonline.com/image_portal/Home/Graph/Base/IDEXOnlineDiamondIndex.xml'"/>
<xsl:template match="/">
<textarea>
<xsl:copy-of select="document($pUrl)"/>
</textarea>
</xsl:template>
</xsl:stylesheet>
当应用于任何XML文档时,产生:
<textarea>
<root>
<index>139.09</index>
<change>-0.00036470</change>
</root>
</textarea>