I’m trying to read in an XML feed using an XSLT Macro in Umbraco and have it display the content nicely formatted. My Macro works fine when the feed is available, but if the feed returns a 404 I cannot manage to get the XSLT to gracefully handle it.
I’m getting the XML using umbraco.library:GetXmlDocumentByUrl()
I’m finding that it is creating a parse error, and that sometimes it just crashes the site rather than return the error text I have specified.
I have also tried wrapping the GetXmlDocumentByUrl() in a document() test to see if I can use that to handle the error a little better. I have found that whilst this stops the site from crashing, and does work if the XML feed exists, it still creates a parse error rather than displaying my error text.
I’d appreciate any help or advice on this, my code is below:
<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>
Update:
I’ve tried skimming down my code to simplify the issue to the following, this is supposed to use the non-cache implementation of GetXmlDocumentByUrl so that I would ensure that I don’t have an issue there and also output the value straight away to ensure it’s not my choose 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>
With a definite 404 page it returns a string of “System.Net.WebException: The remote server returned an error: (404) Not Found. at System.Net.HttpWebRequest.GetResponse() at umbraco.library.GetXmlDocumentByUrl(String Url)” However with the feed that I’m actually having problems with its timing out, I double checked with fiddler and it seems that the page is actually returning a 200, but not an XML document, I should mention that in my renderFeed template is as follows, so I would have still expected it to display the otherwise content rather than timeout.
<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>
I got the when tests from an example, is there a better way I should be testing for this?
The only problem I can see is if your
XmlDocumentis loading aContentTypeoftext/htmlrather thantext/xml. I wrote a simple function, based on the original one in Umbraco, to allow you to alter theWebRequesttimeout plus, it checks theContentType.