2011-03-11 129 views
2

我想从我的代码中调用下面的模板。但我不断收到javax.xml.transform.TransformerException:ElemTemplateElement错误:incrementValue.For一个不同的模板,我仍然得到javax.xml.transform.TransformerException:ElemTemplateElement错误:templateName.Since样式表太长,我粘贴的相关代码样式表。有人能让我知道我在做什么错吗?xsl:呼叫模板在XSL中不起作用

<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> 
+0

你可以粘贴整个样式表吗? – zode64 2011-03-11 22:14:24

+0

由于XSL太长,我在上面的文章中通过编辑粘贴了我的样式表的相关部分。我没有导入任何外部样式表。我的照片目录中有3张照片。我已经声明了一个模板,该模板会遍历该目录中的照片数量,并且需要我的输出,如 C:/photos/twincities/photo1.jpg C:/photos/twincities/photo2.jpg C:/photos/twincities/photo3.jpg 2011-03-11 22:39:30

+0

@Sachin Paradkar:'ElemTemplateElement错误:incrementValue'因为你有一个'XSL:内容模板中template'元素另一个。 – 2011-03-11 22:46:08

回答

2

你的xsl:如果XSL:价值的和xsl:变量都需要在xsl存在:模板,XSL:变量或XSL:PARAM,我不知道他们是否都没有。

xsl:模板只能是xsl:stylesheet的子模板。

您需要从第一<xsl:template match="/">

内删除模板定义定义incrementValue模板单独并把其他模板的内容主要<xsl:template match="/">

里面,所以你有这样的事情:

<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模板中存在的变量移出主模板才会移入该模板。

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

+0

我没有得到一个错误,当我声明一个新的模板,但我只是在调用它们时出错。 – 2011-03-11 23:00:25

+0

我不知道你是怎么没有得到一个错误,可能与首先调用哪些指令有关,但它将无法找到incrementValue,除非在xsl:stylesheet下或从另一个样式表导入 – zode64 2011-03-11 23:14:47