Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用带有变量的外部XMLURI_Php_Xml_Xslt_Xslt 1.0 - Fatal编程技术网

Php 使用带有变量的外部XMLURI

Php 使用带有变量的外部XMLURI,php,xml,xslt,xslt-1.0,Php,Xml,Xslt,Xslt 1.0,我在这里和大G上搜索过,我愿意学习,但我还没有找到答案 我正在尝试使用XSLT转换外部XML数据,以便在HTML或PHP中轻松读取。我测试了一些东西,并成功地用XSL和PHP转换了一些简单的XML文件。问题是我需要使用的实际XML文件并不是我们通常看到的典型的点XML文件,而是更像格式“”。当我使用这些地址时,它似乎正确地读取了这些文件和XSL样式表,解析了正确数量的表单元格,但返回的却是空的 怎么了 此外,它是否与外部站点使用的xml格式有关?我注意到他们的XML比我以前看到的其他文件更“XH

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

我正在尝试使用XSLT转换外部XML数据,以便在HTML或PHP中轻松读取。我测试了一些东西,并成功地用XSL和PHP转换了一些简单的XML文件。问题是我需要使用的实际XML文件并不是我们通常看到的典型的点XML文件,而是更像格式“”。当我使用这些地址时,它似乎正确地读取了这些文件和XSL样式表,解析了正确数量的表单元格,但返回的却是空的

怎么了

此外,它是否与外部站点使用的xml格式有关?我注意到他们的XML比我以前看到的其他文件更“XHTML风格”

它们的样式使用一个大标记并以斜线闭合:

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

相同的示例,如果使用usua树进行写入:

<vehicle> <id>5464</id> <routeTag>14</routeTag> <dirTag>14_IB2</dirTag> ... </vehicle>
5464 14_IB2。。。
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);
?> 
load('http://webservices.nextbus.com/service/publicXMLFeed?command=vehicleLocations&a=sf-穆尼&r=14&t=0’;
//加载XSL文件
$xsl=新文档;
$xsl->load('route14.xsl');
//配置转换器
$proc=新的XSLTProcessor;
//附加xsl规则
$proc->importStyleSheet($xsl);
echo$proc->transformToXML($xml);
?> 

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

更改这些行

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


您的代码很好,但要获取属性值,您需要在route14.xsl中用@作为所选属性名称的前缀

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

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