如何使用 XSLT 显示来自 url 的 XML 数据

How to display XML data from a url using XSLT

我通过 Yahoo API 中的 url 获得了动态 XML 体育数据,我想使用 XSLT 在我的网站上显示和排序这些数据。这是第一次玩XML和XLST。在测试时,我已经弄清楚当 XML 代码被手动粘贴到服务器上的 XML 文件中时如何正确显示它,但我正在努力获取 XML从 url 到网页的数据馈送(每天更新)。

我有一个 PHP 文件,我在其中成功连接到提要并打印原始 XML 数据(称之为 'feed.php')。我认为部分问题是 XML 数据在该文件中作为字符串输出,因此需要将其转换为文档(对象?),以便 XSLT 可以与 [=39= 中的元素标签交互].在我要显示所选数据的网页上:

<main>
    <button type="button" onclick="loadStandings(displayOverallStandings)">League Standings</button>
    
    <article id="standings-division">
*league standings to display here when the button is clicked*
    </article>

<script>

// found elsewhere on SO to turn string into XML document

function loadStandings(displayOverallStandings) {
    var xhr = new XMLHttpRequest();
    xhr.onload = function() {
    dump(xhr.responseXML.documentElement.nodeName);
}
    xhr.onerror = function() {
  dump("Error while getting XML.");
}
xhr.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200) {
                displayOverallStandings(this);
        }
    }
xhr.open("GET", "feed.php"); // the path to the feed.php file
xhr.responseType = "document";
xhr.send();

}

// more code I found on SO to load xml and xsl docs in browser to display league standings

function displayOverallStandings(xhr) {

    xml = xhr.responseXML;
    xsl = loadXMLDoc("standings.xsl"); // path to the XSL file I've created
// code for IE
if (window.ActiveXObject || xhttp.responseType == "msxml-document")
  {
  ex = xml.transformNode(xsl);
  document.getElementById("standings-division").innerHTML = ex;
  }
// code for Chrome, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
  {
  xsltProcessor = new XSLTProcessor();
  xsltProcessor.importStylesheet(xsl);
  resultDocument = xsltProcessor.transformToFragment(xml, document);
  document.getElementById("standings-division").appendChild(resultDocument);
  }
}
</script>

更新:根据要求,这是 XSLT 代码:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <ol>
    <div class=''>
        <span>Team</span>
        <span>Points</span>
        <span>Points Change</span>
    </div>
    <xsl:for-each select="fantasy_content/leagues/league/standings/teams/team">
      <xsl:sort select="team_standings/points_for" data-type="number" order="descending"/>
      <li>
        <span><xsl:value-of select="name"/></span>
        <span><xsl:value-of select="team_standings/points_for"/></span>
        <span><xsl:value-of select="team_standings/points_change"/></span>
      </li>
    </xsl:for-each>
  </ol>
</xsl:template>

</xsl:stylesheet>

在网页上,显示了来自 XSL 文件的 html,但没有提取任何 XML 数据。我不认为这是 XSL 文件的问题,因为正如我所说,如果在 displayOverallStandings() 中我只是加载一个 'static file' 并粘贴 XML 代码,它会正确显示。

问题的另一个原因可能是 Yahoo 提要中提供的完整 XML 代码的根元素包含大量关于提要的附加信息,我不得不在 'static file' 将显示粘贴的 XML 代码。就是这样:

<?xml version="1.0" encoding="UTF-8"?>
<content xml:lang="en-US" yahoo:uri="*feed url*" time="399.72496032715ms" copyright="Data provided by Yahoo! and STATS, LLC" refresh_rate="60" xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" xmlns="http://fantasysports.yahooapis.com/fantasy/v2/base.rng">
* rest of the XML code *

但我不知道如何从源提要中自动删除那些东西 url 所以它会在上面的代码尝试用 XSLT 显示它之前 <?xml version="1.0" encoding="UTF-8"?><content>... 开始。

感谢您的帮助!

我在 'feed.php' 文件中使用 PHP 找到了一个解决方法,将提要 xml 数据转换为简单的 XML 对象(将数据显示为数组), 遍历数组以 select 我想要的特定数据,然后使用 SimpleXMLElement() 函数将其转回 xml 标记。这允许创建一个更简单的 xml 文档,其中从标签中排除了无关的 Yahoo 简介,这在 XSLT 中表现正确。

<?php
(Feed data saved in $response variable)

// convert xml data string into XML object
$xml = simplexml_load_string($response);

// selected output from object array as manually created XML tags
$output = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><content></content>');
$date = $output->addChild('date',$xml->date); //append 'date' xml tags and get the date content from the XML object

(create more xml tags containing data, etc...)

echo $output->asXML();

?>