我正嘗試在Umbraco中使用XSLT宏讀取XML源,並讓它顯示格式良好的內容。當Feed可用時,My Macro可以正常工作,但如果Feed返回404,我無法設法讓XSLT正常處理它。我發現它創建了一個解析錯誤,有時它只是崩潰的網站,而不是返回我指定的錯誤文本。我發現它正在創建一個解析錯誤,有時它只是崩潰的網站,而不是返回我指定的錯誤文本。在Umbraco XSLT宏中優雅地處理缺少的XML源
我也嘗試在文檔()測試中包裝GetXmlDocumentByUrl()以查看是否可以使用它來更好地處理錯誤。我發現雖然這會阻止網站崩潰,並且在XML提要存在的情況下仍然有效,但它仍會創建分析錯誤而不是顯示錯誤文本。
我會很感激有這方面的幫助或建議,我的代碼如下:
<xsl:variable name="feed" select="'http://url.to.feed'"/>
<xsl:template match="/">
<xsl:value-of select="document($feed)"/>
<!-- start writing XSLT -->
<xsl:choose>
<xsl:when test="string-length($feed) > 0 and $feed != ''">
<xsl:choose>
<xsl:when test="document($feed)">
File found
<xsl:variable name="feedContent" select="umbraco.library:GetXmlDocumentByUrl($feed, $cacheRate)"/>
<xsl:choose>
<xsl:when test="count($feedContent/error) > 0">
<!--<xsl:when test="$feedContent != 'error'">-->
<p class="feedList">
<strong>This dynamic content is currently not available</strong><br />
The content could not be loaded. Please verify that you are on the correct page and that you have an
active internet connection.
</p>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="renderFeed">
<xsl:with-param name="feedContent" select="$feedContent"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
Can't find the file...
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<p class="feedList">
<strong>No content exists for this page</strong><br />
Please view another page.
</p>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
更新: 我試過撇了我的代碼,以簡化的問題以下,這應該使用GetXmlDocumentByUrl的非緩存實現,這樣我會確保我沒有問題存在,也輸出直線距離的值,以確保它不是我的選擇statments:
<xsl:template match="/">
<!-- start writing XSLT -->
<xsl:choose>
<xsl:when test="string-length($feed) > 0 and $feed != ''">
<xsl:variable name="vDoc" select="umbraco.library:GetXmlDocumentByUrl($feed)"/>
<xsl:value-of select="$vDoc"/>
<xsl:choose>
<xsl:when test="$vDoc">
File found
</xsl:when>
<xsl:otherwise>
Can't find the file...
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<p class="feedList">
<strong>No content exists for this page</strong><br />
Please view another page.
</p>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
有了一個明確的404頁,它返回一串「System.Net」。 WebException:遠程服務器返回錯誤:(404)未找到。在System.Net.HttpWebRequest.GetResponse()在umbraco.library.GetXmlDocumentByUrl(字符串Url)「然而,飼料,我實際上有問題,它的超時,我仔細檢查與小提琴手,似乎該網頁實際上返回200,但不是一個XML文件,我要指出,我renderFeed模板如下,所以我本來還指望它顯示否則內容,而不是暫停。
<xsl:template name="renderFeed">
<xsl:param name="feedContent" />
<xsl:choose>
<xsl:when test="count($feedContent//item) > 0">
//Render Feed content
</xsl:when>
<xsl:otherwise>
<p class="feedList">
<strong>No content exists for this page</strong><br />
Please view another page.
</p>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
我得到了測試時從一個例子,有更好的方式,我應該測試這個嗎?
請在返回404錯誤時提供GetXmlDocumentByUrl()返回的XML示例。這很可能是你的XPATH表達式(count($ feedContent/error)> 0)出錯。 – user47900 2012-01-30 18:28:10