altandogan
altandogan

Reputation: 1285

Calling template in xslt files

i have created one template whic is like this:

<xsl:template name="loop">
  <xsl:param name="yeni"></xsl:param>
          <xsl:choose>
            <xsl:when test="$yeni !=''">
            <span style="color:#ff0000">
              <br/>
              <xsl:value-of select="$yeni"/>
              </span>
                  <xsl:call-template name="loop">

                    <xsl:with-param name="yeni" select="substring($yeni,2)"/>
                  </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:text>I am out of the loop</xsl:text>
                </xsl:otherwise>
              </xsl:choose>
</xsl:template>  

i can call it somewhere and it works.But i want to call it from this code xslt design breaks down. Why i can't call in <xsl:template match="//n1:Invoice/cac:InvoiceLine"> this template

<xsl:template match="//n1:Invoice/cac:InvoiceLine">
<tr>
<td id="lineTableTd" align="right">
<xsl:template match="/">
                          <xsl:call-template name="loop">
                            <xsl:with-param name="yeni">"hello"</xsl:with-param>
                          </xsl:call-template>
                        </xsl:template>
</td>
</tr>

Upvotes: 0

Views: 410

Answers (1)

Tim C
Tim C

Reputation: 70648

If you look at your current code...

<xsl:template match="//n1:Invoice/cac:InvoiceLine">
   <tr>
      <td id="lineTableTd" align="right">
         <xsl:template match="/">
             <xsl:call-template name="loop">
                 <xsl:with-param name="yeni">"hello"</xsl:with-param>
              </xsl:call-template>
         </xsl:template>
     </td>
  </tr>
</xsl:template>

The problem you have an xsl:template nest within another xsl:template which is not allowed. It should really look like this

<xsl:template match="n1:Invoice/cac:InvoiceLine">
   <tr>
      <td id="lineTableTd" align="right">
         <xsl:call-template name="loop">
             <xsl:with-param name="yeni">"hello"</xsl:with-param>
         </xsl:call-template>
     </td>
  </tr>
</xsl:template>

(Note, you don't also need the // at the start of the template match either)

This would give you the following output though

<span style="color:#ff0000"><br/>"hello"</span>
<span style="color:#ff0000"><br/>hello"</span>
<span style="color:#ff0000"><br/>ello"</span>
<span style="color:#ff0000"><br/>llo"</span>
<span style="color:#ff0000"><br/>lo"</span>
<span style="color:#ff0000"><br/>o"</span>
<span style="color:#ff0000"><br/>"</span>
I am out of the loop

Possibly you need to change <xsl:value-of select="$yeni"/> in the "loop" template to just <xsl:value-of select="substring($yeni, 1, 1)"/>. It is also possible you don't actually need the quotations marks around "hello" in this instance

 <xsl:with-param name="yeni">hello</xsl:with-param>

You would only need quotation marks if you did it this way (to stop it looking for an element named "hello").

<xsl:with-param name="yeni" select='"hello"' />

As an aside, should you wish to split some text by a specific string, you can use a similar method. You can use the "contains" function to check whether a string contains another string. If so, you would use "substring-before" to output the first bit, and then recursively call the "loop" template using "substring-after".

Upvotes: 2

Related Questions