Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 10th, 2009, 10:52 AM
Authorized User
 
Join Date: Apr 2008
Posts: 85
Thanks: 10
Thanked 0 Times in 0 Posts
Default Problem with template

Hi All,

how can i use template inside a template. Like below example.

Code:
 
<xsl:template name="Interest">
 
<xsl:template match="Apply">
 
 ... some code here
 
</xsl:template>
Thanks

Nelly
 
Old November 10th, 2009, 11:06 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You can't, as xsl:template is a top level declaration.

If you explain what you are trying to accomplish (i.e. show us your input XML, and a sample output XML) then we can probably tell you how to do what you want.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old November 10th, 2009, 11:25 AM
Authorized User
 
Join Date: Oct 2009
Posts: 15
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Hi sam... thanks for your reply.

What i am trying to do is i have created a name template in that template i am receiving parameters from call template. In name template i want to compare values of attribute in xml document with parameter values.

below is the code:

Code:
<xsl:template match="Apply">

  <xsl:call-template name="Activity">
  
   <xsl:with-param name="ID">              
                <xsl:apply-templates select="./user/@ID"/>
   </xsl:with-param>

  </xsl:call-template>        

</xsl:template>



 <xsl:template name="Activity">
        <xsl:param name="ID"/>


        <xsl:apply-templates select="document('employee.xml')"/>

         <xsl:template match="employee">
            <xsl:if test="@ID=$ID">

		 <xsl:value-of select="profile/title"></xsl:value-of>

            </xsl:if>         
 	 </xsl:template>
        


 </xsl:template>
I want to compare the value and read the data from XMl document.

Thanks
 
Old November 10th, 2009, 11:38 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I have no idea - I said to show us your input XML, and what you want your output XML to look like. I have no idea what they look like so I can only imagine how the above is incorrect.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old November 10th, 2009, 11:44 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

I think you might want
Code:
 <xsl:template name="Activity">
        <xsl:param name="ID"/>
        <xsl:apply-templates select="document('employee.xml')//employee[@ID = $ID]"/>       
 </xsl:template>
 
 <xsl:template match="employee">
         <xsl:value-of select="profile/title"></xsl:value-of>  
 </xsl:template>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
The Following 2 Users Say Thank You to Martin Honnen For This Useful Post:
Hari7 (November 10th, 2009), nelly78 (November 10th, 2009)
 
Old November 10th, 2009, 12:15 PM
Authorized User
 
Join Date: Apr 2008
Posts: 85
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Hi Marin... Thank you very much.

You are absolutly right, i wanted the same think and its working perfectly for my scenario.

Thanks Again.
 
Old November 10th, 2009, 12:29 PM
Authorized User
 
Join Date: Apr 2008
Posts: 85
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Hi Martin,

Just a quick question in below code, how can i access 'name' parameter in the last template.

Code:
 
<xsl:template match="Apply">
  <xsl:call-template name="Activity">
  
   <xsl:with-param name="ID">              
                <xsl:apply-templates select="./dept/@name"/>
   </xsl:with-param>
  </xsl:call-template>        
</xsl:template>
 
 <xsl:template name="Activity">
        <xsl:param name="ID"/>
        <xsl:param name="Name"/>

        <xsl:apply-templates select="document('employee.xml')"/>                

 </xsl:template>

<xsl:template match="department">
 <xsl:if test="@name=$name">           
            <xsl:apply-templates/>            
        </xsl:if>  
 </xsl:template>
'name' parameter is coming from call template.

Thanks

Nelly
 
Old November 10th, 2009, 12:53 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Both xsl:call-template and xsl:apply-templates allow you to pass on parameters thus you can pass parameters on. If you use XSLT 2.0 you can use tunnel parameters to ensure the parameters are passed on even if you don't explicitly do that e.g.
Code:
 <xsl:template name="Activity">
        <xsl:param name="ID"/>
        <xsl:param name="Name"/>

        <xsl:apply-templates select="document('employee.xml')">
          <xsl:with-param name="Name" select="$Name" tunnel="yes"/>
        </xsl:apply-templates>

 </xsl:template>

<xsl:template match="department">
 <xsl:param name="Name" tunnel="yes"/>
 <xsl:if test="@name = $Name">           
            <xsl:apply-templates/>            
        </xsl:if>  
 </xsl:template>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old November 11th, 2009, 11:07 AM
Authorized User
 
Join Date: Apr 2008
Posts: 85
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Hi Matin than for your reply..

There is a little problem in parameter value:

In the below code i can see the value in '$Name'

Code:
<xsl:template name="Activity">
        <xsl:param name="ID"/>
        <xsl:param name="Name"/>

        <xsl:apply-templates select="document('employee.xml')">
          <xsl:with-param name="Name" select="$Name" tunnel="yes"/>
        </xsl:apply-templates>

 </xsl:template>
But here value '$Name' is showing empty:

Code:
<xsl:template match="department">
 <xsl:param name="Name" tunnel="yes"/>
 <xsl:if test="@name = $Name">           
            <xsl:apply-templates/>            
        </xsl:if>  
 </xsl:template>
where i am doing wrong?

Thanks
Nelly
 
Old November 11th, 2009, 11:23 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Are you using XSLT 2.0 with an XSLT 2.0 processor like Saxon 9 or AltovaXML tools? Tunnel parameters are a new feature in XSLT 2.0.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with template Hari7 XSLT 3 November 3rd, 2009 01:26 PM
Problem with XSLT template nelly78 XML 3 October 29th, 2009 02:14 PM
Problem in Recursive applying template ismomo XSLT 3 December 29th, 2007 11:35 PM
xsl:template problem mickhughes XSLT 1 August 16th, 2007 08:40 AM
Datagrid template column problem lore6673 ASP.NET 1.x and 2.0 Application Design 1 December 3rd, 2003 11:05 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.