文档末尾的额外内容

时间:2015-04-09 19:24:15

标签: xml xslt sharepoint

我不断收到解析错误:文档末尾的额外内容但我无法理解。我认为这是因为没有根标签但是在那里。这是天气预报。

<!--?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>

1 个答案:

答案 0 :(得分:1)

样式表存在一些问题,无法解析和执行它以产生所需的输出。

首先,您构建<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>
相关问题