Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dan 1288 posts 3921 karma points c-trib
    Mar 10, 2011 @ 12:41
    Dan
    0

    Problem with template/loop

    Hi,

    I have a macro which iterates through 6 items and outputs some links and images for each one.  The code is as follows:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    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" xmlns:umbraco.contour="urn:umbraco.contour" xmlns:tagsLib="urn:tagsLib" xmlns:BlogLibrary="urn:BlogLibrary"
    exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets umbraco.contour tagsLib BlogLibrary ">


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

    <xsl:param name="currentPage"/>

    <xsl:variable name="maxImageListImages" select="6" />

    <xsl:template match="/">
    <ul id="products">
    <xsl:call-template name="outputImageListImage">
    <xsl:with-param name="node" select="$currentPage" />
    </xsl:call-template>
    </ul>
    </xsl:template>

    <xsl:template name="outputImageListImage">
    <xsl:param name="node" select="." />
    <xsl:param name="index" select="1" />
    <xsl:variable name="mediaID" select="$node/data[@alias = concat('box', $index, 'Image1')]" />
    <xsl:variable name="linkID" select="$node/data[@alias = concat('box', $index, 'URL')]" />
    <xsl:variable name="src" select="umbraco.library:GetMedia($mediaID, false())/data[@alias = 'umbracoFile']" />

    <xsl:if test="number($linkID) and normalize-space($src)">
    <xsl:variable name="url" select="umbraco.library:NiceUrl($linkID)" />
    <li>
    <a href="{url}" title="Click for more information" class="quick-link">
    <img src="{$src}" alt="{$node/@nodeName}" />
    </a>
    <h2>
    <a href="{url}" title="Click for more information" class="quick-link">
    <xsl:value-of select="$node/data[@alias = concat('box', $index, 'Title')]" />
    </a>
    </h2>
    </li>
    </xsl:if>
    <xsl:if test="$index &lt;= $maxImageListImages">
    <xsl:call-template name="outputImageListImage">
    <xsl:with-param name="index" select="$index + 1" />
    </xsl:call-template>
    </xsl:if>
    </xsl:template>
    </xsl:stylesheet>

    However, when I save this I get the old error:

    Error occured

    System.OverflowException: Value was either too large or too small for an Int32.
    at System.Convert.ToInt32(Double value)
    at System.Double.System.IConvertible.ToInt32(IFormatProvider provider)
    at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
    at System.Xml.Xsl.Runtime.XmlQueryRuntime.ChangeTypeXsltArgument(XmlQueryType xmlType, Object value, Type destinationType)
    at System.Xml.Xsl.Runtime.XmlQueryContext.InvokeXsltLateBoundFunction(String name, String namespaceUri, IList`1[] args)
    at (XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator {urn:schemas-microsoft-com:xslt-debug}current, IList`1 node, Double index)
    at (XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime, XPathNavigator {urn:schemas-microsoft-com:xslt-debug}current)
    at Root(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)
    at Execute(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime)
    at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlSequenceWriter results)
    at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter writer, Boolean closeWriter)
    at System.Xml.Xsl.XmlILCommand.Execute(IXPathNavigable contextDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter results)
    at System.Xml.Xsl.XmlILCommand.Execute(IXPathNavigable contextDocument, XmlResolver dataSources, XsltArgumentList argumentList, TextWriter results)
    at System.Xml.Xsl.XslCompiledTransform.Transform(IXPathNavigable input, XsltArgumentList arguments, TextWriter results)
    at umbraco.presentation.webservices.codeEditorSave.SaveXslt(String fileName, String oldName, String fileContents, Boolean ignoreDebugging)

     

    Can anyone suggest what how to get this to run without the error?

  • Lachlann 344 posts 626 karma points
    Mar 10, 2011 @ 12:51
    Lachlann
    0

    Hi Dan,

     

    Just had a quick look thorugh your code and Im not entirley sure about

     <xsl:variable name="linkID" select="$node/data[@alias = concat('box', $index, 'URL')]" />

    the variable linkID is then passed to NiceURL which will be expecting a node ID to create a URL with. could this be causeing your issue?

    Also this code

     <a href="{url}" title="Click for more information" class="quick-link">

    should probably have a $ before the url variable.

    Hope this helps

     

    L

    
    
                
  • Lachlann 344 posts 626 karma points
    Mar 10, 2011 @ 12:53
    Lachlann
    0

    Oh no wait my bad, I see what you are doing.

    have you tried putting /@id on the end of

     <xsl:variable name="linkID" select="$node/data[@alias = concat('box', $index, 'URL')]" />

     

    L

  • Dan 1288 posts 3921 karma points c-trib
    Mar 10, 2011 @ 12:56
    Dan
    0

    Thanks Lachlann - good spot on the {url}s - sorted those.  However, the variable linkID should be returning a node ID - to be double sure I hard coded a node id into the line where the niceURL is called, and still same error.

  • Lachlann 344 posts 626 karma points
    Mar 10, 2011 @ 12:57
    Lachlann
    1

    Hmm have you looked at hard coding a media id for the GetMedia funciton, that is hte other one i know tends to throw the int32 errors

  • Lachlann 344 posts 626 karma points
    Mar 10, 2011 @ 12:59
    Lachlann
    0

    Also have you tried turning off error reporting and saving it anyway, I find that sometime even though it throws an error when you save it runs fine when its actually on the page.

     

    L

  • Dan 1288 posts 3921 karma points c-trib
    Mar 10, 2011 @ 14:52
    Dan
    0

    I'm getting somewhere, but there appears to be a weird issue whereby the data on the node is only returned in the first iteration of the loop - even though the $index variable is incrementing okay.  I've stripped out most of my HTML and hard-coded one of the field aliases just to demonstrate the issue:

    <xsl:param name="currentPage"/>

    <xsl:variable name="maxImageListImages" select="5" />

    <xsl:template match="/">
    <ul id="products">
    <xsl:call-template name="outputImageListImage">
    <xsl:with-param name="node" select="$currentPage" />
    </xsl:call-template>
    </ul>
    </xsl:template>

    <xsl:template name="outputImageListImage">
    <xsl:param name="node" select="." />
    <xsl:param name="index" select="1" />
    <xsl:variable name="boxTitle" select="$node/data[@alias = concat('box', $index, 'Title')]" />
    <xsl:variable name="mediaID" select="$node/data[@alias = concat('box', $index, 'Image1')]" />
    <xsl:variable name="linkID" select="$node/data[@alias = concat('box', $index, 'URL')]" />
    <xsl:variable name="url" select="$linkID" />

    **<xsl:value-of select="$index"/> / <xsl:value-of select="$node/data[@alias = 'box1Title']"/>**

    <xsl:if test="$index &lt;= $maxImageListImages">
    <xsl:call-template name="outputImageListImage">
    <xsl:with-param name="index" select="$index + 1" />
    </xsl:call-template>
    </xsl:if>
    </xsl:template>
    </xsl:stylesheet>

    I've put asterisks around the output, and here's what's returned:

    **1 / First product**
    **2 / **
    **3 / **
    **4 / **
    **5 / **
    **6 / **

    Really, what I'm expecting here, whilst the 'box1Title' is hard-coded is:

    **1 / First product**
    **2 / First product**
    **3 / First product**
    **4 / First product**
    **5 / First product**
    **6 / First product**

    When that's working I can then hook up boxXTitle to use the $index value to return the appropriate title for that item in the loop.

    Can you see why 'box1Title' is only returned on the first loop, and is blank on the other iterations?  I think this is the crux of my issue.

  • Dan 1288 posts 3921 karma points c-trib
    Mar 10, 2011 @ 16:44
    Dan
    0

    Got around it by just referencing $currentPage instead of $node, and that's sorted it.

    Thanks for your help Lachlann - the check on the mediaID value kind of pointed me in the right direction.

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Mar 10, 2011 @ 17:18
    Chriztian Steinmeier
    1

    Hi Dan,

    Just to help you understand why you didn't get the expected output...

    When you first call the outputImageListImage template, you do so from within the root template (match="/") - you pass in $currentPage to the "node" parameter, which gets you the first output because $currentPage has that property (data[@alias = 'box1Title']) - when you call the template again (from within itself) you don't pass an argument to the $node parameter, so it'll take the default which you've specified (select=".").

    Now that dot means to use the "current context node" which will actually fall back to the "/" from when you called it the first time (because it hasn't been changed since) and you'll probably be a little surprised if you try to examine that one, 'coz it'll hold only a <macro> element (possibly with some childnodes if the macro has parameters). 

    To fix the template at that stage, you could send the existing $node parameter on to the next iteration: 

    <xsl:if test="$index &lt;= $maxImageListImages">
        <xsl:call-template name="outputImageListImage">                         
            <xsl:with-param name="node" select="$node" />
            <xsl:with-param name="index" select="$index + 1" />                             
        </xsl:call-template>
    </xsl:if>

    Hope that helps you understand why it failed...

    /Chriztian

  • Dan 1288 posts 3921 karma points c-trib
    Mar 10, 2011 @ 17:21
    Dan
    0

    Ace, thanks Chriztian!

Please Sign in or register to post replies

Write your reply to:

Draft