Thavudu
Thavudu

Reputation: 235

XSLT Apply template inside an another template based on Attribute

I have a below XML, i have to Loop through Test_resuts and Component. when a Component's TYPE matches with purticular name then i have to apply different template to that component. I have created an XSL to with different template for each Component TYPE eg:NORMALTEXTBOX. But the templates are not getting aplied to the Component Block... could anyone please help me here?

XML

<Report>
<Test_Results>
   <Test_Result TestID="1" TestName="sampleTest" >
       <Component Type="NORMALTEXTBOX">
         <ComponentName>Component1</ComponentName>
         <ComponentId></ComponentId>
         <Method></Method>
         <Results></Results>
       </Component>
       <Component Type="NUMERICTEXTBOX">
         <ComponentName>Component2</ComponentName>
         <ComponentId></ComponentId>
         <Method></Method>
         <Results></Results>
       </Component>
   </Test_Result>

   <Test_Result TestID="1" TestName="">
       <Component Type="NORMALTEXTBOX">
         <ComponentName></ComponentName>
         <ComponentId></ComponentId>
         <Method></Method>
         <Results></Results>
       </Component>
   </Test_Result>
 </Test_Results>
</Report>

XSL

            <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="html" omit-xml-declaration="yes"/>
        <xsl:template match="/">
          <html>
          <body>
            <xsl:apply-templates select="Report/Test_Results"/>
          </body>
          </html>
        </xsl:template>

        <xsl:template match="Test_Results">
           <xsl:for-each select="Test_Result">
             <xsl:for-each select="Component">
               <xsl:choose>
                  <xsl:when test="@Type = 'NORMALTEXTBOX'">
                     <xsl:apply-templates select="Component" mode="normaltext"/>
                  </xsl:when>

                  <xsl:when test="@Type = 'NUMERICTEXTBOX'">
                     <xsl:apply-templates select="Component" mode="numerictext"/>
                  </xsl:when>
               </xsl:choose>
             </xsl:for-each>
          </xsl:for-each>
        </xsl:template>

        <xsl:template match="Component" mode="normaltext">
            <h1> different styles to be applied, only some values of component will be taken</h1>
        </xsl:template>

        <xsl:template match="Component" mode="numerictext">
            <h1>different styles to be applied, only some values of component will be taken</h1>
        </xsl:template>

    </xsl:stylesheet>

Upvotes: 1

Views: 5003

Answers (2)

Thavudu
Thavudu

Reputation: 235

I have firgured it out... instead of apply-templates, i have tried the below and it apply the template for that purticular component.

 <xsl:call-template name="NORMALTEXTBOX">

Upvotes: 0

Linga Murthy C S
Linga Murthy C S

Reputation: 5432

Since your context node in xsl:choose is Component, xsl:apply-template's select expression should be current():

<xsl:choose>
<xsl:when test="@Type = 'NORMALTEXTBOX'">
    <xsl:apply-templates select="." mode="normaltext"/>
</xsl:when>

<xsl:when test="@Type = 'NUMERICTEXTBOX'">
    <xsl:apply-templates select="." mode="numerictext"/>
</xsl:when>
</xsl:choose>

But, a better way to do this would be to just include the @Type condition in template match as below:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" omit-xml-declaration="yes"/>
<xsl:template match="/">
    <html>
        <body>
            <xsl:apply-templates select="Report/Test_Results"/>
        </body>
    </html>
</xsl:template>
<xsl:template match="Test_Results">
    <xsl:for-each select="Test_Result">
        <xsl:apply-templates select="Component"/>
    </xsl:for-each>
</xsl:template>
<xsl:template match="Component[@Type = 'NORMALTEXTBOX']">
    <h1> different styles to be applied, only some values of component will be taken</h1>
</xsl:template>
<xsl:template match="Component[@Type = 'NUMERICTEXTBOX']">
    <h1>different styles to be applied, only some values of component will be taken</h1>
</xsl:template>
</xsl:stylesheet>

Upvotes: 2

Related Questions