Umbraco : Create an RSS Feed problems

女生的网名这么多〃 提交于 2019-12-12 18:27:05

问题


I am pretty new to Umbraco but have managed to do alot of cool things in a short space of time. One thing that I simply cannot do is the RSS Feed! This seems to be the most difficult thing to do! I have been trying for days to get this damn thing working but it wont!

Ok, here's what i have done,

I went into XSLT Files, created a new RSS Feed document, then added the URL to the section in the site, i.e. News, which contains news files.

You can check my code down below.

Once I have created this XSLT, I go into the document types, create a new one, with a new master page, add the macro in, then create the page in the Content section.

When I do this I Get the following error:

ERROR:

This page contains the following errors:

error on line 3 at column 6: XML declaration allowed only at the start of the document

Below is a rendering of the page up to the first error.

Does anyone have any idea where I am going wrong? I have looked at the source and it looks like it's not looping through the directory files of News.

However when I add this macro to my home page, it shows errors, but when looking at the source I can see a load of lovely XML ?

What's going wrong on here ?

XSLT:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet

  version="1.0"

  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

  xmlns:rssdatehelper="urn:rssdatehelper"

  xmlns:dc="http://purl.org/dc/elements/1.1/"

  xmlns:content="http://purl.org/rss/1.0/modules/content/"

  xmlns:msxml="urn:schemas-microsoft-com:xslt" 

  xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" 

  exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">





  <xsl:output method="xml" omit-xml-declaration="yes"/>


  <xsl:param name="currentPage"/> 

  <!-- Update these variables to modify the feed -->

  <xsl:variable name="RSSNoItems" select="string('10')"/>

  <xsl:variable name="RSSTitle" select="string('My sample rss')"/>

    <xsl:variable name="SiteURL" select="string('http://localhost:58281/news.aspx')"/>

  <xsl:variable name="RSSDescription" select="string('Add your description here')"/>


  <!-- This gets all news and events and orders by updateDate to use for the pubDate in RSS feed -->

  <xsl:variable name="pubDate">

    <xsl:for-each select="$currentPage/* [@isDoc]">

      <xsl:sort select="@createDate" data-type="text" order="descending" />

      <xsl:if test="position() = 1">

        <xsl:value-of select="updateDate" />

      </xsl:if>

    </xsl:for-each>

  </xsl:variable> 

  <xsl:template match="/">

    <!-- change the mimetype for the current page to xml -->

    <xsl:value-of select="umbraco.library:ChangeContentType('text/xml')"/> 

    <xsl:text disable-output-escaping="yes">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</xsl:text>

    <rss version="2.0"

    xmlns:content="http://purl.org/rss/1.0/modules/content/"

    xmlns:wfw="http://wellformedweb.org/CommentAPI/"

    xmlns:dc="http://purl.org/dc/elements/1.1/"


      <channel>

        <title>

          <xsl:value-of select="$RSSTitle"/>

        </title>

        <link>

          <xsl:value-of select="$SiteURL"/>

        </link>

        <pubDate>

          <xsl:value-of select="$pubDate"/>

        </pubDate>

        <generator>umbraco</generator>

        <description>

          <xsl:value-of select="$RSSDescription"/>

        </description>

        <language>en</language> 

        <xsl:apply-templates select="$currentPage/* [@isDoc and string(umbracoNaviHide) != '1']">

          <xsl:sort select="@createDate" order="descending" />

        </xsl:apply-templates>

      </channel>

    </rss> 

  </xsl:template> 

  <xsl:template match="* [@isDoc]">

    <xsl:if test="position() &lt;= $RSSNoItems">

      <item>

        <title>

          <xsl:value-of select="@nodeName"/>

        </title>

        <link>

          <xsl:value-of select="$SiteURL"/>

          <xsl:value-of select="umbraco.library:NiceUrl(@id)"/>

        </link>

        <pubDate>

          <xsl:value-of select="umbraco.library:FormatDateTime(@createDate,'r')" />

        </pubDate>

        <guid>

          <xsl:value-of select="$SiteURL"/>

          <xsl:value-of select="umbraco.library:NiceUrl(@id)"/>

        </guid>

        <content:encoded>

          <xsl:value-of select="concat('&lt;![CDATA[ ', ./bodyText,']]&gt;')" disable-output-escaping="yes"/>

        </content:encoded>

      </item>

    </xsl:if>

  </xsl:template> 

</xsl:stylesheet>

回答1:


By default, all user-created templates start off as Master Pages, which in turn inherit from a hidden Master Page called Default.master. This means all templates contain a <asp:Content> section, and if the XML declaration is not put immediately after the opening <asp:Content> tag then it would put an empty line at the top of the source code causing the error you've described.

I usually get around this problem by putting the XML declaration in the template (See line 2 below and scroll right) and leaving the macro to generate the rest of the XML content.

<%@ Master Language="C#" MasterPageFile="~/umbraco/masterpages/default.master" AutoEventWireup="true" %>
<asp:Content ContentPlaceHolderID="ContentPlaceHolderDefault" runat="server"><?xml version="1.0" encoding="UTF-8"?>
    <umbraco:Macro Alias="MyRssFeed" runat="server" />
</asp:Content>

Alternatively, if you do wish to keep the XML declaration in the macro, just make sure in the template that the <umbraco:Macro> tag immediately follows the <asp:Content> tag without any white space or line breaks.




回答2:


If you are using razor, you can avoid this error by replacing the line break like this:

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
    Layout = null;
}<?xml version="1.0" encoding="UTF-8"?>
@{
    umbraco.library.ChangeContentType("text/xml");      
    var siteURL = "http://" +  Request.Url.Host.ToString();
    var rssPage = CurrentPage.AncestorOrSelf(1).Rss.First();
    var articles = CurrentPage.AncestorOrSelf(1).Descendants("Article").OrderBy("date desc");
}   
    <rss version="2.0">
        <channel>
            <title>@rssPage.title</title>
            @Html.Raw("<link>")@siteURL@Html.Raw("</link>")
            <description>@rssPage.description</description>
            <pubDate>@String.Format("{0:ddd, dd MMM yyyy HH:mm:ss}", @rssPage.CreateDate)</pubDate>
            <lastBuildDate>@String.Format("{0:ddd, dd MMM yyyy HH:mm:ss}", DateTime.Now)</lastBuildDate>
            <language>en</language>
            <generator>Umbraco</generator>

            @foreach(var article in articles)
            {
                <item>
                    <title>@if(article.HasValue("title")){@article.title}else{@article.Name}</title>
                    @Html.Raw("<link>")@siteURL@article.Url@Html.Raw("</link>")
                    <description>@article.previewText</description>
                    <pubDate>@String.Format("{0:ddd, dd MMM yyyy} {1:HH:mm:ss}", @article.date, @article.CreateDate)</pubDate>
                </item>
            }
        </channel>
    </rss>

The key-part of this code snip is this line - ensure its not on the line below:

}<?xml version="1.0" encoding="UTF-8"?>


来源:https://stackoverflow.com/questions/8714609/umbraco-create-an-rss-feed-problems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!