xml - Extra content at end of document -


i keep getting parsing error: content @ end of document cannot figure out. thought due not having root tag there. weather feed.

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

there several issues stylesheet prevent being parsed , executed produce desired output.

first, attempts construct <img> elements , construct value @src not work. in xslt not construct xml string, need construct nodes happen serialized xml. instead of:

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

you should create well-formed img element , use attribute value template construct @src value:

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

second, < character in xpath: <xsl:for-each select="(rss/channel/item/yweather:forecast)[position() < 3]"> comparing position of items in for-each needs encoded: <xsl:for-each select="(rss/channel/item/yweather:forecast)[position() &lt; 3]">

finally, in several places using <xsl:copy-of select="$scale"/>, attribute. using in places in attribute cannot copied. believe intended achieve emit value of attribute, should instead use: <xsl:value-of select="$scale"/>.

applying fixes 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> 

produces following output when applied yahoo weather forecast:

<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 princeton, ma @ 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> 

Popular posts from this blog