I need an XSLT script to generate a piece of javascript.
<script type="text/javascript">
var DATE_INFO = { -XSLT FOR EACH LOOP HERE - }; ....... </script>
There is more to the script, but this is the part that's important. Whenever I add the XSLT that needs to execute in it, the entire thing slows down to a crawl and crashes. I KNOW the XSLT is correct, because if I remove all the Javascript parts from the script it prints EXACTLY what I need it to.
As perfect as you think it is, context means a lot in XSLT, so post your XSLT (or the relevant parts of it) so we can help you; the above doesn't give us any clues as to what's wrong...
Okay, well, these are the XSLT parts that are called:
<xsl:template match="/"> <div id="jsCalendarContainer"><xsl:comment /><!-- Comment here to make sure the XSLT doesn't autoclose the div! --></div> <!-- Get the parent node --> <xsl:variable name="events" select="umbraco.library:GetXmlNodeById('1359')/Event" /> <!-- loop through the events -->
<script type="text/javascript"> var DATE_INFO = { <!-- loop through the events --> <xsl:for-each select="$events"> <!-- handle the creation of the line of data --> <xsl:call-template name="createDataRow"> <xsl:with-param name="eventId" select="current()/@id" /> </xsl:call-template> <!-- add a comma if this is not the last item--> <xsl:if test="position() != last()"> , </xsl:if> </xsl:for-each> }; function getDateInfo(date, wantsClassName) { var as_number = Calendar.dateToInt(date); return DATE_INFO[as_number]; }
<xsl:template name="createDataRow"> <xsl:param name="eventId" /> <!-- Get the current event --> <xsl:variable name="event" select="umbraco.library:GetXmlNodeById($eventId)" /> <!-- test if it has recurrance --> <xsl:choose> <xsl:when test="string($event/event/pdcalendarevent/pdcrec) != '0'"> <!-- if yes, handle the recurrancy --> <xsl:call-template name="recurrance"> <xsl:with-param name="eventId" select="current()/@id" /> </xsl:call-template> </xsl:when> <xsl:otherwise> <!-- if not, just print this line --> <xsl:value-of select="Exslt.ExsltDatesAndTimes:formatdate($event/event/pdcalendarevent/pdcstart, 'yyyyMMdd')"/>:{klass: "highlight", tooltip: "<xsl:value-of select="$event/@nodeName"/>"} </xsl:otherwise> </xsl:choose> </xsl:template>
<xsl:template name="recurrance"> <!-- Store the events id --> <xsl:param name="eventId" /> <!-- prepare a parameter to keep incrementing the start date! --> <xsl:param name="newStartDate" /> <!-- get the Event itself --> <xsl:variable name="event" select="umbraco.library:GetXmlNodeById($eventId)" /> <!-- find this events "start date" --> <xsl:variable name="startDate"> <xsl:choose> <!-- if the parameter is not empty, use it as the start date --> <xsl:when test="$newStartDate != ''"> <xsl:value-of select="$newStartDate" /> </xsl:when> <!-- else use the events pdcstart value --> <xsl:otherwise> <xsl:value-of select="$event/event/pdcalendarevent/pdcstart" /> </xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:variable name="endDate" select="$event/event/pdcalendarevent/pdcend" /> <!-- print this event line --> <xsl:value-of select="Exslt.ExsltDatesAndTimes:formatdate($startDate, 'yyyyMMdd')"/>:{klass: "highlight", tooltip: "<xsl:value-of select="$event/@nodeName"/>"} <!-- make a variable to test whether or not it has to loop --> <xsl:variable name="testDate"> <xsl:choose> <!-- add a single day to the start date --> <xsl:when test="string($event/event/pdcalendarevent/pdcrec) = '1'"> <xsl:value-of select="umbraco.library:DateAdd($startDate, 'd', 1)" /> </xsl:when> <!-- add a week to the start date --> <xsl:when test="string($event/event/pdcalendarevent/pdcrec) = '2'"> <xsl:value-of select="umbraco.library:DateAdd($startDate, 'd', 7)" /> </xsl:when> <!-- add a month to the start date --> <xsl:when test="string($event/event/pdcalendarevent/pdcrec) = '3'"> <xsl:value-of select="umbraco.library:DateAdd($startDate, 'm', 1)" /> </xsl:when> <!-- add a year to the start date --> <xsl:when test="string($event/event/pdcalendarevent/pdcrec) = '4'"> <xsl:value-of select="umbraco.library:DateAdd($startDate, 'y', 1)" /> </xsl:when> </xsl:choose> </xsl:variable> <!-- if the end date is bigger or equal to the newly created date, we loop through this once more! --> <xsl:if test="umbraco.library:DateGreaterThanOrEqual($endDate,$testDate)"> ,<xsl:call-template name="recurrance"> <xsl:with-param name="eventId" select="$event/@id" /> <!-- but this time, we fill up the parameter with the new date! :D --> <xsl:with-param name="newStartDate" select="$testDate" /> </xsl:call-template> </xsl:if> </xsl:template>
Okay, I got the javascript part working. I had to break it up a bit like this:
<xsl:text>var DATE_INFO = {</xsl:text> <!--<xsl:value-of select="$dates"/> --><xsl:for-each select="$events"> <!-- handle the creation of the line of data --> <xsl:call-template name="createDataRow"> <xsl:with-param name="eventId" select="current()/@id" /> </xsl:call-template> <!-- add a comma if this is not the last item--> <xsl:if test="position() != last()"> , </xsl:if> </xsl:for-each> <xsl:text>}; </xsl:text>
That did the trick. Only problem now is the final part of the script:
<xsl:if test="umbraco.library:DateGreaterThanOrEqual($endDate,$testDate)"> ,<xsl:call-template name="recurrance"> <xsl:with-param name="eventId" select="$event/@id" /> but this time, we fill up the parameter with the new date! :D <xsl:with-param name="newStartDate" select="$testDate" /> </xsl:call-template> </xsl:if>
This part basically calls the same template once again if the condition is true to get a sort of loop. But it just seems to bog down the entire application. Any ideas on how to fix that?
It's definitely doing an infinite loop, so you'll have to debug all possible values for the $testDate variable. Start by outputting the values of $endDate, $testDate and the DateGreaterThanOrEqual() function to see whether they make sense.
Also, put an otherwise statement in there, just to see if you'd sometimes hit that...
Well, it works when I use the XSLT visualizer from the editor. It just dies whenever I use it on the actual page. Does the visualizer kill the loop if it determines it to be infinite? I also can't find anything strange by printing out the values. They give exactly what I expect them too and increment perfectly up to the point where they are supposed to end the loop.
As I said, it works in the visualizer, just not on the actual site.
generating javascript with XSLT
Hey all,
I need an XSLT script to generate a piece of javascript.
There is more to the script, but this is the part that's important. Whenever I add the XSLT that needs to execute in it, the entire thing slows down to a crawl and crashes. I KNOW the XSLT is correct, because if I remove all the Javascript parts from the script it prints EXACTLY what I need it to.
Any ideas?
Kind regards,
-Ferdy
Hi Ferdy,
As perfect as you think it is, context means a lot in XSLT, so post your XSLT (or the relevant parts of it) so we can help you; the above doesn't give us any clues as to what's wrong...
/Chriztian
Okay, well, these are the XSLT parts that are called:
Two ideas (read 'guesses').
1) Build a variable up to insert into the javascript (so you end up with: "var DATE_INFO ={ $newVar };)")
2) Wrap the <script /> contents in <![CDATA[]]>
Okay, I got the javascript part working. I had to break it up a bit like this:
That did the trick. Only problem now is the final part of the script:
This part basically calls the same template once again if the condition is true to get a sort of loop. But it just seems to bog down the entire application. Any ideas on how to fix that?
Hi Ferdy,
It's definitely doing an infinite loop, so you'll have to debug all possible values for the $testDate variable. Start by outputting the values of $endDate, $testDate and the DateGreaterThanOrEqual() function to see whether they make sense.
Also, put an otherwise statement in there, just to see if you'd sometimes hit that...
/Chriztian
Well, it works when I use the XSLT visualizer from the editor. It just dies whenever I use it on the actual page. Does the visualizer kill the loop if it determines it to be infinite? I also can't find anything strange by printing out the values. They give exactly what I expect them too and increment perfectly up to the point where they are supposed to end the loop.
As I said, it works in the visualizer, just not on the actual site.
-Ferdy
is working on a reply...