Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can you call a template within a template in xsl

Tags:

xml

xslt

can you call a template within a template? for example:

If I was wanting 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>
like image 816
gary A.K.A. G4 Avatar asked Dec 23 '22 01:12

gary A.K.A. G4


2 Answers

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.

like image 77
Jim Garrison Avatar answered Jan 06 '23 00:01

Jim Garrison


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 :)

like image 33
Dimitre Novatchev Avatar answered Jan 05 '23 23:01

Dimitre Novatchev