xsl:call-template does not work in XSL

11,374

Your xsl:if, xsl:value-of and xsl:variable all need to exist inside an xsl:template, xsl:variable or xsl:param, I am not sure whether they are not.

An xsl:template must be a child of xsl:stylesheet only.

You need to remove the template definitions from inside the first <xsl:template match="/">

Define the incrementValue template seperate and put the content of the other template inside the main <xsl:template match="/">

so you have something like this:

<xsl:stylesheet version = '2.0'
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:xdt="http://www.w3.org/2005/02/xpath-datatypes"
 xmlns:mngi="www.medianewsgroup.com"
 exclude-result-prefixes="xs xdt mngi dirReader"
 xmlns:date="http://exslt.org/dates-and-times"
 xmlns:utildate="xalan://java.util.Date"
 xmlns:dirReader="xalan://com.mngi.eidos.util.DirectoryReader"
 extension-element-prefixes="date utildate dirReader">
<xsl:strip-space elements="*"/>
<xsl:output method="xml"
            indent="yes"
            encoding="utf-8"
            doctype-system="/SysConfig/Classify/Dtd/MNG/classify-story.dtd"/>
    ...

<xsl:variable name="totalPhotos" select="dirReader:totalPhotos($PhotoPath)"/>

    ...

    <xsl:template match="/">

        ...

        <xsl:if test="$totalPhotos &gt; 0">
            <photoName>
            <!--xsl:value-of select="$currentIdx"/-->
                <xsl:variable name="photoFromIndex" select="dirReader:photoNameWithIndex($currentIdx,$PhotoPath)"/>
                <xsl:value-of select="concat($PhotoPath,'',$photoFromIndex)"/>
            </photoName>
            <xsl:call-template name="incrementValue">
                <xsl:with-param name="currentIdx" select="$currentIdx"/>
            </xsl:call-template>
        </xsl:if>    
    </xsl:template>
    <xsl:template name="incrementValue">
        <xsl:param name="currentIdx"/>
        <xsl:if test="$currentIdx &lt; $totalPhotos">
            <xsl:value-of select="$currentIdx"/>
            <photoName>
                <xsl:variable name="photoFromIndex" select="dirReader:photoNameWithIndex($currentIdx,$PhotoPath)"/>
                <xsl:value-of select="concat($PhotoPath,'',$photoFromIndex)"/>
            </photoName>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

EDIT: Variables used in both templates will have to be declared globally as I have done with <xsl:variable name="totalPhotos" select="dirReader:totalPhotos($PhotoPath)"/> above so they are available to both templates because at the minute they are only scoped to the template they are in. or you can pass them as parameters as is done with <xsl:with-param name="currentIdx" select="$currentIdx"/>. If there are variables that only exist in the incrementValue template move out of the main template into that one.

WARNING: This is untested as I don't fully understand the problem due to lack of input so I am only sorting out the syntax.

Share:
11,374
Sachin Paradkar
Author by

Sachin Paradkar

Updated on August 01, 2022

Comments

  • Sachin Paradkar
    Sachin Paradkar almost 2 years

    I am trying to call the below template from my code . But I keep getting javax.xml.transform.TransformerException: ElemTemplateElement error: incrementValue.For a different template I still get javax.xml.transform.TransformerException: ElemTemplateElement error: templateName.Since the stylesheet is too long I am pasting the relevant code of the stylesheet. Can someone let me know what I am doing wrong ??

    <xsl:stylesheet version = '2.0'
         xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
         xmlns:xs="http://www.w3.org/2001/XMLSchema"
         xmlns:xdt="http://www.w3.org/2005/02/xpath-datatypes"
         xmlns:mngi="www.medianewsgroup.com"
         exclude-result-prefixes="xs xdt mngi dirReader"
         xmlns:date="http://exslt.org/dates-and-times"
         xmlns:utildate="xalan://java.util.Date"
         xmlns:dirReader="xalan://com.mngi.eidos.util.DirectoryReader"
         extension-element-prefixes="date utildate dirReader">
        <xsl:strip-space elements="*"/>
        <xsl:output method="xml"
                    indent="yes"
                    encoding="utf-8"
                    doctype-system="/SysConfig/Classify/Dtd/MNG/classify-story.dtd"/>
        <xsl:template match="/">
            <xsl:processing-instruction name="EM-dtdExt"
             >/SysConfig/Rules/MNG/MNG.dtx</xsl:processing-instruction>
            <xsl:processing-instruction name="EM-templateName"
             >/SysConfig/BaseConfiguration/MNG/Templates/MNG_story.xml</xsl:processing-instruction>
            <xsl:processing-instruction name="xml-stylesheet"
             >type="text/css" href="/SysConfig/BaseConfiguration/MNG/Css/MNG-story-nonechannel.css"</xsl:processing-instruction>
            <!-- Added By Sachin -->
            <xsl:processing-instruction name="EM-dtdExt"
             >/SysConfig/Rules/MNG/MNG.dtx</xsl:processing-instruction>
            <xsl:processing-instruction name="EM-templateName"
             >/SysConfig/BaseConfiguration/MNG/Templates/MNG_story.xml</xsl:processing-instruction>
            <xsl:processing-instruction name="xml-stylesheet"
             >type="text/css" href="/SysConfig/BaseConfiguration/MNG/Css/MNG-story-nonechannel.css"</xsl:processing-instruction>
            <xsl:variable name="UPPERCASE" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ '" />
            <xsl:variable name="lowercase" select="'abcdefghijklmnopqrstuvwxyz'" />
            <xsl:variable name="HubName" select="translate(/Article/Hub/HubName, ' ', '')" />
            <xsl:variable name="lowerhubname" select="translate($HubName, $UPPERCASE, $lowercase)" />
            <xsl:variable name="SiteRoot" select="'C:/TwinCitiesArticles'" />
            <xsl:variable name="DatePath" select="translate(substring-before(/Article/PublishingDates/WebPublish_DTTM, 'T'), '-', '/')"/>
            <xsl:variable name="PhotoDir" select="'photos/'" />
            <xsl:variable name="PhotoPath" select="concat($SiteRoot, $DatePath, '/', $lowerhubname, $PhotoDir)" />
            <TodaysDate>
                <xsl:value-of select="utildate:new()"/>
            </TodaysDate>
            <imageDir>
                <xsl:value-of select="$PhotoPath"/>
            </imageDir>
            <xsl:variable name="totalPhotos" select="dirReader:totalPhotos($PhotoPath)"/>
            <xsl:variable name="photoList" select="dirReader:readDirectory($PhotoPath)"/>
            <xsl:variable name="pName" select="dirReader:photoName($totalPhotos,$PhotoPath)"/>
            <xsl:variable name="firstPhotoName" select="dirReader:firstPhoto($totalPhotos,$PhotoPath)"/>
            <xsl:variable name="currentIdx" select="dirReader:currentIndex($firstPhotoName,$PhotoPath)"/>
            <totalPhotos>
                <xsl:value-of select="$totalPhotos" />
            </totalPhotos>
            <xsl:template name="incrementValue">
                <xsl:param name="currentIdx"/>
                <xsl:if test="$currentIdx &lt; $totalPhotos">
                    <xsl:value-of select="$currentIdx"/>
                    <photoName>
                        <xsl:variable name="photoFromIndex"
                                      select="dirReader:photoNameWithIndex($currentIdx,$PhotoPath)"/>
                        <xsl:value-of select="concat($PhotoPath,'',$photoFromIndex)"/>
                    </photoName>
                    <xsl:call-template name="incrementValue">
                        <xsl:with-param name="currentIdx" select="$currentIdx + 1"/>
                    </xsl:call-template>
                </xsl:if>
            </xsl:template>
            <xsl:if test="$totalPhotos &gt; 0">
                <photoName>
                    <!--xsl:value-of select="$currentIdx"/-->
                    <xsl:variable name="photoFromIndex" select="dirReader:photoNameWithIndex($currentIdx,$PhotoPath)"/>
                    <xsl:value-of select="concat($PhotoPath,'',$photoFromIndex)"/>
                </photoName>
    
                    <xsl:call-template name="incrementValue">
                        <xsl:with-param name="currentIdx" select="$currentIdx"/>
                    </xsl:call-template>
    
            </xsl:if>
    
  • Sachin Paradkar
    Sachin Paradkar about 13 years
    I do not get a error when I declare a new template but I only get an error while calling them.
  • rogermushroom
    rogermushroom about 13 years
    I don't know how you aren't getting an error perhaps to do with which instructions are called first but it won't be able to find incrementValue unless under xsl:stylesheet or imported from another stylesheet