文档末尾的额外内容

Extra content at end of document

我一直收到解析错误:文档末尾的额外内容,但我无法理解。我认为这是由于没有根标签但存在。这是天气预报。

<!--?xml version="1.0" encoding="ISO-8859-1"?-->
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0"
    xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
    <xsl:output method="html" indent="yes" />
    <xsl:template match="/">
        <xsl:variable name="scale" select="rss/channel/yweather:units/@temperature"/>
        <table width="100%" border="0" cellspacing="0" cellpadding="3" class="Normal">
            <tr bgcolor="#075C70">
                <td colspan="2">
                    <strong>
                        <font color="white">Weather Report – 
                            <xsl:value-of select="rss/channel/item/title"/>
                        </font>
                    </strong>
                </td>
            </tr>
            <tr>
                <td>
                    <strong>
                        <font size="4">
                            <xsl:value-of select="rss/channel/item/yweather:condition/@temp"/>
                            <xsl:text></xsl:text>
                            <xsl:copy-of select="$scale" />
                        </font>
                    </strong>
                    <br/>
High 
                    <xsl:value-of select="rss/channel/item/yweather:forecast/@high"/>
                    <xsl:text></xsl:text>
                    <xsl:copy-of select="$scale" />
                    <br/>
Low 
                    <xsl:value-of select="rss/channel/item/yweather:forecast/@low"/>
                    <xsl:text></xsl:text>
                    <xsl:copy-of select="$scale" />
                </td>
                <td>
                    <xsl:text disable-output-escaping="yes">
                        <img src="http://us.i1.yimg.com/us.yimg.com/i/us/we/52/
                        </xsl:text>
                        <xsl:value-of select="rss/channel/item/yweather:condition/@code"/>
                        <xsl:text disable-output-escaping="yes">.gif"/></xsl:text>
                    <br/>
                    <xsl:value-of select="rss/channel/item/yweather:condition/@text"/>
                </td>
            </tr>
            <tr bgcolor="#075C70">
                <td colspan="2">
                    <strong>
                        <font color="white">2 Day Forecast</font>
                    </strong>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <table width="100%" border="0" cellspacing="0" cellpadding="3" class="Normal">
                        <xsl:for-each select="(rss/channel/item/yweather:forecast)[position() < 3]">
                            <tr>
                                <td>
                                    <xsl:value-of select="@day"/>
                                </td>
                                <td>
                                    <xsl:text disable-output-escaping="yes">
                                        <img src="http://us.i1.yimg.com/us.yimg.com/i/us/we/52/
                                        </xsl:text>
                                        <xsl:value-of select="@code"/>
                                        <xsl:text disable-output-escaping="yes">.gif"/></xsl:text>
                                </td>
                                <td>
                                    <xsl:value-of select="@text"/>
                                    <br/>High: 
                                    <xsl:value-of select="@high"/>
                                    <xsl:text></xsl:text>
                                    <xsl:copy-of select="$scale" />
Low: 
                                    <xsl:value-of select="@low"/>
                                    <xsl:text></xsl:text>
                                    <xsl:copy-of select="$scale" />
                                </td>
                            </tr>
                        </xsl:for-each>
                    </table>
                </td>
            </tr>
        </table>
    </xsl:template>
</xsl:stylesheet>

您的样式表存在多个问题,导致无法对其进行解析和执行以生成所需的输出。

首先,您尝试构建 <img> 元素和构建 @src 的值将不起作用。在 XSLT 中,您不构造 XML 字符串,您需要构造恰好被序列化为 XML 的节点。而不是:

<xsl:text disable-output-escaping="yes">
  <img src="http://us.i1.yimg.com/us.yimg.com/i/us/we/52/
</xsl:text>
<xsl:value-of select="@code"/>
<xsl:text disable-output-escaping="yes">.gif"/></xsl:text>

您应该创建一个格式正确的 img 元素并使用属性值模板来构建 @src 值:

<img src="http://us.i1.yimg.com/us.yimg.com/i/us/we/52/{@code}.gif"/>

其次,XPath中的<字符:<xsl:for-each select="(rss/channel/item/yweather:forecast)[position() < 3]">比较for-each中项目的位置需要编码:<xsl:for-each select="(rss/channel/item/yweather:forecast)[position() &lt; 3]">

最后,您在几个地方使用了 <xsl:copy-of select="$scale"/>,这是一个属性。您在无法复制属性的地方使用它。我相信您打算实现的是发出属性的值,因此您应该改用:<xsl:value-of select="$scale"/>.

正在将修复应用到您的 XSLT:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0"
    xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
    <xsl:output method="html" indent="yes" />
    <xsl:template match="/">
        <xsl:variable name="scale" select="rss/channel/yweather:units/@temperature"/>
        <table width="100%" border="0" cellspacing="0" cellpadding="3" class="Normal">
            <tr bgcolor="#075C70">
                <td colspan="2">
                    <strong>
                        <font color="white">Weather Report – 
                            <xsl:value-of select="rss/channel/item/title"/>
                        </font>
                    </strong>
                </td>
            </tr>
            <tr>
                <td>
                    <strong>
                        <font size="4">

                            <xsl:value-of select="rss/channel/item/yweather:condition/@temp"/>
                            <xsl:text></xsl:text>
                            <xsl:value-of select="$scale" />
                        </font>
                    </strong>
                    <br/>
                    High 
                    <xsl:value-of select="rss/channel/item/yweather:forecast/@high"/>
                    <xsl:text></xsl:text>
                    <xsl:value-of select="$scale" />
                    <br/>
                    Low 
                    <xsl:value-of select="rss/channel/item/yweather:forecast/@low"/>
                    <xsl:text></xsl:text>
                    <xsl:value-of select="$scale" />
                </td>
                <td>
                    <img src="http://us.i1.yimg.com/us.yimg.com/i/us/we/52/{rss/channel/item/yweather:condition/@code}.gif"/>
                    <br/>
                    <xsl:value-of select="rss/channel/item/yweather:condition/@text"/>
                </td>
            </tr>
            <tr bgcolor="#075C70">
                <td colspan="2">
                    <strong>
                        <font color="white">2 Day Forecast</font>
                    </strong>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <table width="100%" border="0" cellspacing="0" cellpadding="3" class="Normal">
                        <xsl:for-each select="(rss/channel/item/yweather:forecast)[position() &lt; 3]">
                            <tr>
                                <td>
                                    <xsl:value-of select="@day"/>
                                </td>
                                <td>
                                    <img src="http://us.i1.yimg.com/us.yimg.com/i/us/we/52/{@code}.gif"/>
                                </td>
                                <td>
                                    <xsl:value-of select="@text"/>
                                    <br/>High: 
                                    <xsl:value-of select="@high"/>
                                    <xsl:text></xsl:text>
                                    <xsl:value-of select="$scale" />
                                    Low: 
                                    <xsl:value-of select="@low"/>
                                    <xsl:text></xsl:text>
                                    <xsl:value-of select="$scale" />
                                </td>
                            </tr>
                        </xsl:for-each>
                    </table>
                </td>
            </tr>
        </table>
    </xsl:template>
</xsl:stylesheet>

应用于雅虎天气预报时产生以下输出:

<table xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" width="100%" border="0" cellspacing="0" cellpadding="3" class="Normal">
   <tr bgcolor="#075C70">
      <td colspan="2"><strong><font color="white">Weather Report – 
               Conditions for Princeton, MA at 9:08 pm EDT</font></strong></td>
   </tr>
   <tr>
      <td><strong><font size="4">36F</font></strong><br>
         High 
         37F<br>
         Low 
         33F
      </td>
      <td><img src="http://us.i1.yimg.com/us.yimg.com/i/us/we/52/26.gif"><br>Cloudy
      </td>
   </tr>
   <tr bgcolor="#075C70">
      <td colspan="2"><strong><font color="white">2 Day Forecast</font></strong></td>
   </tr>
   <tr>
      <td colspan="2">
         <table width="100%" border="0" cellspacing="0" cellpadding="3" class="Normal">
            <tr>
               <td>Thu</td>
               <td><img src="http://us.i1.yimg.com/us.yimg.com/i/us/we/52/12.gif"></td>
               <td>Rain<br>High: 
                  37F
                  Low: 
                  33F
               </td>
            </tr>
            <tr>
               <td>Fri</td>
               <td><img src="http://us.i1.yimg.com/us.yimg.com/i/us/we/52/11.gif"></td>
               <td>Showers<br>High: 
                  56F
                  Low: 
                  43F
               </td>
            </tr>
         </table>
      </td>
   </tr>
</table>