【发布时间】:2012-08-05 22:15:12
【问题描述】:
我正在尝试使用 xsl 样式表显示 xml 文件。我似乎无法让它工作。
我使用的提要是这样的:http://screamingdeals.tdn.travelscream.com/atom.xml
这是它的 xml 的缩写副本:
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:ts="http://www.travelscream.com">
<title type="text">Travelscream Deal Alerts</title>
<updated>2012-08-08T11:09:44-06:00</updated>
<id>http://screamingdeals.tdn.travelscream.com/</id>
<link rel="alternate" type="text/html" hreflang="en" href="http://screamingdeals.tdn.travelscream.com"/>
<link rel="self" type="application/atom+xml" href="http://screamingdeals.tdn.travelscream.com/atom.xml"/>
<generator uri="http://screamingdeals.tdn.travelscream.com" version="1.0">screamingdeals.tdn.travelscream.com</generator>
<entry>
<title>New York City: Best Western President Hotel - $152</title>
<link rel="alternate" type="text/html" href="http://screamingdeals.tdn.travelscream.com/a477908344.htm"/>
<id>http://screamingdeals.tdn.travelscream.com/a477908344.htm</id>
<updated>2012-08-08T11:09:44-06:00</updated>
<published>2012-08-08T00:00:00-06:00</published>
<author>
<name>screamingdeals.tdn.travelscream.com</name>
<uri>http://screamingdeals.tdn.travelscream.com</uri>
</author>
<content type="html" xml:base="screamingdeals.tdn.travelscream.com" xml:lang="en"> <table> <tr> <td valign="middle"> <img width="80" width="60"
src="http://www.dealbase.com/assets/hotels/photos/000/002/953/icon.jpg"> </td> <td valign="top"> $152/night & up - Best Western President Hotel (New York City, NY) - 46%
Off Travel Dates: Through Dec 31, 2012 </td> </tr> </table> </content>
<ts:data>
<ts:primarykey>477908344</ts:primarykey>
<ts:location_data>
<ts:location id="50" selected="0" airport_codes="EWR,JFK,LGA" type="dest">New York</ts:location>
<ts:location id="55" selected="1" airport_codes="EWR,JFK,LGA" type="dest">New York City</ts:location>
<ts:location id="199" selected="0" airport_codes="" type="dest">United States</ts:location>
<ts:location id="288" selected="0" airport_codes="" type="dest">North America</ts:location>
<ts:location id="524" selected="0" airport_codes="EWR,JFK,LGA,NYC" type="dest">Manhattan</ts:location>
<ts:location id="525" selected="0" airport_codes="EWR,JFK,LGA,NYC" type="dest">Bronx</ts:location>
<ts:location id="526" selected="0" airport_codes="EWR,JFK,LGA,NYC" type="dest">Brooklyn</ts:location>
<ts:location id="527" selected="0" airport_codes="EWR,JFK,LGA" type="dest">Queens</ts:location>
<ts:location id="528" selected="0" airport_codes="EWR,JFK,LGA" type="dest">Staten Island</ts:location>
<ts:location id="613" selected="0" airport_codes="" type="dest">SoHo</ts:location>
</ts:location_data>
<ts:pkg_attributes>
<ts:attribute id="3" type="TYPE">Honeymoon</ts:attribute>
<ts:attribute id="40" type="MAIN">Hotel</ts:attribute>
<ts:attribute id="62" type="DTYP">City</ts:attribute>
</ts:pkg_attributes>
<ts:deal_expires>2012-09-07T14:54:05-06:00</ts:deal_expires>
<ts:deal_supplier>DealBase.com</ts:deal_supplier>
<ts:feed_image>http://www.dealbase.com/assets/hotels/photos/000/002/953/icon.jpg</ts:feed_image>
<ts:fullscreen_image/>
<ts:price1>15200</ts:price1>
<ts:alpha>0</ts:alpha>
<ts:coord>
<ts:lat>40.7606</ts:lat>
<ts:long>-73.9864</ts:long>
<ts:latlong>40.7606,-73.9864</ts:latlong>
</ts:coord>
<ts:desc>$152/night & up - Best Western President Hotel (New York City, NY) - 46% Off Travel Dates: Through Dec 31, 2012</ts:desc>
<ts:tacomm/>
<ts:direct>true</ts:direct>
<ts:top>false</ts:top>
<ts:traveldates>
<ts:from>01/01/2050</ts:from>
<ts:to>01/01/2050</ts:to>
</ts:traveldates>
</ts:data>
</entry>
</feed>
加载样式表并告诉它转换 xml 的 php 页面是这样编码的:
$xp = new XsltProcessor();
// create a DOM document and load the XSL stylesheet
$xsl = new DomDocument;
$xsl->load('deals_stylesheet.xsl');
// import the XSL styelsheet into the XSLT process
$xp->importStylesheet($xsl);
// create a DOM document and load the XML datat
$xml_doc = new DomDocument;
$xml_doc->load('http://screamingdeals.tdn.travelscream.com/atom.xml');
// transform the XML into HTML using the XSL file
if ($html = $xp->transformToXML($xml_doc)) {
echo $html;
} else {
trigger_error('XSL transformation failed.', E_USER_ERROR);
} // if
到目前为止……很好。我没有收到错误。 这是xsl文件的内容:
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Facebook Deals Page</title>
<style type="text/css">
<![CDATA[
<!--
-->
]]>
</style>
</head>
<body>
<table border="1">
<tr>
<td><b>Title</b></td>
<td><b>ID</b></td>
</tr>
<xsl:for-each select="feed/entry">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="id"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
这里显示它不起作用:http://www.margaretpickarddesign.com/clients/travelscream/facebook_deals/facebook_rss.php
查看 xml 结构,节点“feed”之后是节点“entry”,该节点具有子节点“title”和“id”。那么为什么它不起作用呢?我错过了什么?
【问题讨论】:
-
请显示输入 XML。指向 atom 提要的链接是不够的,因为数据会发生变化。请显示一个不起作用的特定 XML。
-
我没有查看提要,但我 99% 确定这是命名空间问题。在 SO 中搜索“xslt 默认命名空间”,您应该有足够的信息来找出问题所在。
-
@JimGarrison,我添加了 xml。
-
@DevNull - 我对这个文件中的命名空间感到非常困惑。这是一些 ts: 似乎在任何地方都没有定义的东西。所以你可能是对的,但搜索这方面的信息并没有让我到任何地方。您能看一下并告诉我您对我应该将哪个命名空间定义为默认命名空间和/或如何处理“ts”命名空间的想法吗?非常感谢。
-
ts前缀命名空间不是问题,因为您没有访问该命名空间中的任何元素。问题是默认命名空间(没有前缀的 xmlns)。您需要做的是将xmlns:a="http://www.w3.org/2005/Atom"添加到xsl:stylesheet并在您的选择中使用该前缀。 (select="a:feed/a:entry"、select="a:title"和select="a:id")还请注意,您可以使用任何前缀,而不仅仅是“a”。唯一必须相同的是命名空间本身 (http://www.w3.org/2005/Atom)。
标签: xml xslt namespaces foreach