为什么xsl:for-each select =“”不能使用这个xml文件?

时间:2012-08-08 18:18:03

标签: xml xslt namespaces foreach

我正在尝试使用xsl样式表显示xml文件。我似乎无法让它工作。

我正在使用的Feed是: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”。那为什么不工作呢?我错过了什么?

1 个答案:

答案 0 :(得分: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)。