can you call a template within a template in xsl
can you call a template within a template? for example:
If I was wanting开发者_如何学C to use
<xsl:choose>
<xsl:when test="//*[local-name()='RetrieveCCTransRq']">
<xsl:call-template name="SOAPOutput"/>
</xsl:when>
</xsl:choose>
<xsl:template name="SOAPOutput">
<SOAP-ENV:Envelope>
<SOAP-ENV:Body>
<OutputPayload>
<TotalTransactions>
<xsl:value-of select="count(//Transaction)"/>
</TotalTransactions>
<Transactions>
<xsl:apply-templates/>
</Transactions>
</OutputPayload>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
<xsl:template match="Transaction">
<xsl:choose>
<xsl:when test="contains(Type,'Debit')">
<Debit>
<xsl:apply-templates select="Date"/>
<xsl:apply-templates select="PostDate"/>
<xsl:apply-templates select="Description"/>
<xsl:apply-templates select="Amount"/>
</Debit>
</xsl:when>
<xsl:otherwise>
<Credit>
<xsl:apply-templates select="Date"/>
<xsl:apply-templates select="PostDate"/>
<xsl:apply-templates select="Description"/>
<xsl:apply-templates select="Amount"/>
</Credit>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="Date">
<Date>
<xsl:value-of select="."/>
</Date>
</xsl:template>
<xsl:template match="PostDate">
<PostDate>
<xsl:value-of select="."/>
</PostDate>
</xsl:template>
<xsl:template match="Description">
<Description>
<xsl:value-of select="."/>
</Description>
</xsl:template>
<xsl:template match="Amount">
<Amount>
<xsl:value-of select="."/>
</Amount>
</xsl:template>
</xsl:template>
You can CALL a template from another template, you can't nest template DEFINITIONS as you have done. Move all the inner template definitions to top-level and try again.
An <xsl:template>
instruction can only be defined at the global level (must be a child of the <xsl:stylesheet>
instruction).
Another recommendation is to avoid conditional tests of a node type. Instead of this:
<xsl:choose> <xsl:when test="//*[local-name()='RetrieveCCTransRq']"> <xsl:call-template name="SOAPOutput"/> </xsl:when> </xsl:choose>
it is recommended to use this:
<xsl:template match="RetrieveCCTransRq">
<!-- Place the body of the named template here -->
</xsl:template>
In this way you don't have to write the six lines of code quoted above, in which you could easily commit any kind of error. Also, you have converted a named template into a matching one, gaining more flexibility and reusability and you have eliminated a piece of procedural (pull-style) processing. Be lazy and clever -- let the XSLT processor do the node-type checking for you :)
精彩评论