Abhijeet
Abhijeet

Reputation: 417

how to write function in xslt1.0 after downgrade from xslt 2.0

I had the xslt2.0 which i downgrade to the 1.0 and using xalan, but am getting the following exception.

Recoverable error: line 9: Unsupported XSL element 'function'.

the part of xslt is as below.

    <xsl:function name="nav:adjustDate">
            <xsl:param name="dateStr" />
            <xsl:param name="age" />
            <xsl:variable name="minutes">
                    <xsl:choose>
                            <xsl:when test="$age = 1">
                                    <xsl:value-of select="0" />
                            </xsl:when>
                            <xsl:when test="$age = 2">
                                    <xsl:value-of select="-10" />
                            </xsl:when>
                            <xsl:when test="$age = 3">
                                    <xsl:value-of select="-20" />
                            </xsl:when>
                            <xsl:when test="$age = 4">
                                    <xsl:value-of select="-30" />
                            </xsl:when>
                            <xsl:when test="$age = 5">
                                    <xsl:value-of select="-40" />
                            </xsl:when>
                            <xsl:when test="$age = 6">
                                    <xsl:value-of select="-50" />
                            </xsl:when>
                            <xsl:otherwise>
                                    <xsl:value-of select="-60" />
                            </xsl:otherwise>
                    </xsl:choose>
            </xsl:variable>
            <xsl:variable name="dateFormatterStr">
                    <xsl:text>yyyy-MM-dd'T'HH:mm:ss.SSSZ</xsl:text>
            </xsl:variable>
            <!--  output date format should match the input date format of the job file -->
            <xsl:variable name="outDateFormatterStr">
                    <xsl:text>yyyy-MM-dd'T'HH:mm:ssZ</xsl:text>
            </xsl:variable>
    <xsl:variable name="bo" select="bool:new('FALSE')" />
            <xsl:variable name="dateFormatter" select="dateFormat:new($dateFormatterStr)" />
            <xsl:variable name="outDateFormatter" select="dateFormat:new($outDateFormatterStr)" />
    <xsl:value-of select="dateFormat:setLenient($dateFormatter,$bo)" />
            <!-- Have to remove the colon in the timezone offset(eg. +05:00) otherwise date formatter wont work correctly -->
            <xsl:variable name="testDate"
                    select="dateFormat:parse($dateFormatter,concat(substring($dateStr,1,string-length($dateStr)-3),'00'))" />
            <xsl:variable name="cal" select="gregorianCal:new()" />
            <xsl:value-of select="gregorianCal:setTime($cal,$testDate)" />
            <!-- xslt version 2 does not accept contants 12 represents the value for java.util.Calendar.MINUTE
                 Follow section of code will subtract the number of minutes-->
            <xsl:value-of select="gregorianCal:add($cal,12,$minutes)" />
            <xsl:variable name="outputDate" select="gregorianCal:getTime($cal)" />
            <xsl:sequence select="dateFormat:format($outDateFormatter,$outputDate)" />
    </xsl:function>

also want to know the substitute for xsl:sequence in xslt1.0

Can somebody guide me on this ? how to proceed ? am quite new for xslt.

Upvotes: 1

Views: 633

Answers (1)

C. M. Sperberg-McQueen
C. M. Sperberg-McQueen

Reputation: 25034

If you're quite new to XSLT, then back-porting a stylesheet from XSLT 2.0 to XSLT 1.0 is likely to be a very painful experience. Good luck.

In XSLT 1.0, named templates can often be used where in XSLT 2.0 one would use a function. You will need to change the outer xsl:function instruction to xsl:template and then you will need to change all callers to set a variable by calling the named template instead of by invoking the function. And finally you will need to translate the body of the function into XSLT 1.0. Judging by the large number of variables initialized by calls to other user-declared functions, this will be a very slow process.

If your goal (or the goal of those who assigned you this task) is to provide an opportunity for a long and intensive line-by-line study of the stylesheet, this is probably a good way. Otherwise, if I were you, I'd suggest reconsidering the decision to downgrade to 1.0.

Upvotes: 1

Related Questions