使用带有变量的外部 XML uri [英] Using external XML uri with variables

查看:21
本文介绍了使用带有变量的外部 XML uri的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这和大G上搜索过,我愿意学习,但我还没有找到答案.

I searched here and on the big G, I am willing to learn, but I didn't found the answer yet.

我正在尝试使用 XSLT 转换外部 XML 数据,以便在 HTML 或 PHP 中轻松读取.我测试了一些东西,我成功地用 XSL 和 PHP 转换了一些简单的 XML 文件.问题是我需要使用的实际 XML 文件并不是我们通常看到的典型的 dot xml 文件,而是更像是http://webservices.nextbus.com/service/publicXMLFeed?command=vehicleLocations&a=sf-muni&r=14&;t=0".当我使用这些地址时,它似乎可以正确读取这些文件和 XSL 样式表,解析正确数量的表格单元格,但将它们返回为空.

I am trying to transform external XML data with XSLT to be easily read in HTML or PHP. I tested a few things and I successfully transformed some simple XML files with XSL and PHP. The problem is that the actual XML files I need to use are not really the typical dot xml files we generally see, but more like the format "http://webservices.nextbus.com/service/publicXMLFeed?command=vehicleLocations&a=sf-muni&r=14&t=0". When I use these addresses, it seems to read properly these files and the XSL stylesheet, parse the correct numbers of table cells, but return them empty.

怎么了?

另外,会不会和外部站点使用的xml格式有关?我注意到他们的 XML 更像是XHTML 样式",而不是我过去看到的其他文件.

Also, can it be related to the xml formatting used by the external site? I noticed that their XML is more "XHTML styled" rather that the other files I am seen in the past.

他们的风格使用一个大标签并以斜线结束:

Their style using one big tag and closed by a slash:

<vehicle id="5464" routeTag="14" dirTag="14_IB2" lat="37.7637" lon="-122.4087" secsSinceReport="86" predictable="true" heading="218" speedKmHr="0"/>

同样的例子,如果它是使用 usua 树编写的:

The same example, if it was writting using the usua tree:

<vehicle> <id>5464</id> <routeTag>14</routeTag> <dirTag>14_IB2</dirTag> ... </vehicle>

route14.xsl:

route14.xsl:

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

<xsl:template match="/">
  <html>
  <body>
  <h2>Route 14</h2>
  <table border="1">
    <tr bgcolor="#9acd32">
      <th style="text-align:left">Vehicle</th>
      <th style="text-align:left">Direction</th>
    </tr>
    <xsl:for-each select="body/vehicle">
    <tr>
      <td><xsl:value-of select="id" /></td>
      <td><xsl:value-of select="dirTag" /></td>
    </tr>
    </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>   

PHP 代码:

<?php
// Load XML file
$xml = new DOMDocument;
$xml->load('http://webservices.nextbus.com/service/publicXMLFeed?command=vehicleLocations&a=sf-muni&r=14&t=0');

// Load XSL file
$xsl = new DOMDocument;
$xsl->load('route14.xsl');

// Configure the transformer
$proc = new XSLTProcessor;

// Attach the xsl rules
$proc->importStyleSheet($xsl);

echo $proc->transformToXML($xml);
?> 

推荐答案

你走在正确的道路上,但是在访问属性值时,你必须在它们前面加上@

you are on the right path, but when accessing attribute values, you have to prefix them with @

更改这些行

<tr>
  <td><xsl:value-of select="id" /></td>
  <td><xsl:value-of select="dirTag" /></td>
</tr>

<tr>
  <td><xsl:value-of select="@id" /></td>
  <td><xsl:value-of select="@dirTag" /></td>
</tr>

这篇关于使用带有变量的外部 XML uri的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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