首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >xsl:call-template在XSL中不工作

xsl:call-template在XSL中不工作
EN

Stack Overflow用户
提问于 2011-03-12 05:57:51
回答 1查看 11.3K关注 0票数 2

我正在尝试从我的代码中调用下面的模板。但是我一直收到javax.xml.transform.TransformerException: ElemTemplateElement错误: incrementValue.For一个不同的模板,我仍然收到javax.xml.transform.TransformerException: ElemTemplateElement错误: templateName.Since样式表太长,我正在粘贴样式表的相关代码。谁能让我知道我做错了什么??

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-03-12 06:35:04

您的xsl:if、xsl:value-of和xsl:variable都需要存在于xsl:template、xsl:variable或xsl:param中,我不确定它们是否不存在。

xsl:template只能是xsl:stylesheet的子级。

您需要从第一个<xsl:template match="/">中删除模板定义

定义单独的incrementValue模板,并将另一个模板的内容放在主<xsl:template match="/">

所以你会得到类似这样的东西:

代码语言:javascript
复制
<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>

编辑:两个模板中使用的变量必须全局声明,就像我在上面对<xsl:variable name="totalPhotos" select="dirReader:totalPhotos($PhotoPath)"/>所做的那样,这样它们对两个模板都是可用的,因为目前它们的作用域仅限于它们所在的模板。或者,您可以将它们作为参数进行传递,就像使用<xsl:with-param name="currentIdx" select="$currentIdx"/>一样。如果存在只存在于incrementValue模板中的变量,则将其从主模板移到该模板中。

警告:这是未经测试的,因为我没有完全理解这个问题,因为缺乏输入,所以我只整理了语法。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5278838

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档