0

我正在尝试使用 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"> &lt;table&gt; &lt;tr&gt; &lt;td valign="middle"&gt; &lt;img width="80" width="60"
            src="http://www.dealbase.com/assets/hotels/photos/000/002/953/icon.jpg"&gt; &lt;/td&gt; &lt;td valign="top"&gt; $152/night &amp; up - Best Western President Hotel (New York City, NY) - 46%
            Off Travel Dates: Through Dec 31, 2012 &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; </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 &amp; 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”。那么为什么它不起作用呢?我错过了什么?

4

1 回答 1

2

添加评论作为实际答案...

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)。

于 2012-08-10T15:53:35.667 回答