[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] Unable to pass a param variable to a template inside a for-each loop


Subject: RE: [xsl] Unable to pass a param variable to a template inside a for-each loop
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 13 Apr 2006 15:43:40 +0100

You are applying templates to "/", the root node. You don't have a template
that matches "/" in the relevant mode, so the built-in template gets
invoked. The built in template does apply-templates on its children (in the
same mode), so at this stage your template

<xsl:template match="FileService" mode="ReaperMode">

gets invoked. But (in XSLT 1.0) the built-in template doesn't pass its
parameters on.

Change apply-templates select="/" to select="/FileService" and all should be
well. 

Michael Kay
http://www.saxonica.com/

> -----Original Message-----
> From: Rick.Jones@xxxxxxxxxxxxxx [mailto:Rick.Jones@xxxxxxxxxxxxxx] 
> Sent: 13 April 2006 15:33
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Unable to pass a param variable to a template 
> inside a for-each loop
> 
> 
> 
> Hi,
> Problem:
> I am it seems unable to pass a param variable to a template 
> inside a for 
> each loop.
> 
> Objective:
> I am trying to get the param to capture the position number 
> of the node 
> that is being processed in the for loop.
> So, each time it goes around the position() is being captured 
> and inserted 
> into the output XML as a new node.
> 
> XSL Processor: Xalan 2.7
> XSL Language: 1.0
> 
> 
> Here is my XML input:
> <?xml version="1.0" encoding="UTF-8"?>
> <FileService>
>     <Route>Reaper</Route>
>     <File>
>         <Filename>C:\datatrans\trans\outbound\texty1.txt</Filename>
>         <ArchivePath>C:\datatrans\archive\outbound\</ArchivePath>
>         <LogPath>C:\datatrans\confirmations\outbound\</LogPath>
>     </File>
>     <File>
>         <Filename>C:\datatrans\trans\outbound\Texty2.txt</Filename>
>         <ArchivePath>C:\datatrans\archive\outbound\</ArchivePath>
>         <LogPath>C:\datatrans\confirmations\outbound\</LogPath>
>     </File>
>     <File>
>         <Filename>C:\datatrans\trans\outbound\Word Doco.doc</Filename>
>         <ArchivePath>C:\datatrans\archive\outbound\</ArchivePath>
>         <LogPath>C:\datatrans\confirmations\outbound\</LogPath>
>     </File>
>     <File>
>         <Filename>C:\datatrans\trans\outbound\Word.doc</Filename>
>         <ArchivePath>C:\datatrans\archive\outbound\</ArchivePath>
>         <LogPath>C:\datatrans\confirmations\outbound\</LogPath>
>     </File>
>     <Routing>SSI-F</Routing>
>     <TransferProtocol>
>         <Protocol>FTP</Protocol>
>         <IP>127.0.0.1</IP>
>         <Port>21</Port>
>         <LoginName>username</LoginName>
>         <Password>userpassword</Password>
>     </TransferProtocol>
>     <TransferQueue>
>         <QueueName>Outbound</QueueName>
>         <TransferJob>XferOut</TransferJob>
>     </TransferQueue>
> </FileService>
> 
> 
> I have a style sheet (edited for this post) like so:
> 
> 
> <!-- Loops through the document to create 
>                 <xsl:when test="FileService/Route='Reaper'">
>                         <xsl:for-each select="FileService/File">
>                                 <xsl:apply-templates select="/" 
> mode="ReaperMode">
>                                         <xsl:with-param 
> name="loopCtr" 
> select="position()"/>
>                                 </xsl:apply-templates>
>                         </xsl:for-each>
>                 </xsl:when>
> ....
> 
>         <!-- Match the the whole of the document for invoking the 
> FileReaper --> 
>         <xsl:template match="FileService" mode="ReaperMode">
>                 <xsl:param name="loopCtr"/>
>       <!-- Java Service that is called -->
>                 <jbi:invoke service="foo:FileReaper">
>                         <jbi:copyProperties/>
> 
>                         <!-- the message sent to will be the 
> result of 
> this next call -->
>                         <xsl:apply-templates select="." 
> mode="PickPosition">
>                                 <xsl:with-param name="posCtr2" 
> select="$loopCtr"/>
>                         </xsl:apply-templates>
> 
>                 </jbi:invoke>
>         </xsl:template>
> 
>         <!-- Rebuild the document by the template adding the 
> Pick Position 
> -->
>         <xsl:template match="FileService" mode="PickPosition">
>                 <xsl:param name="posCtr2"/>
>                 <FileService>
>                         <xsl:copy-of select="Route"/>
>                         <xsl:copy-of select="Position"/>
>  
>                         <!-- New node with param -->
>                         <PickPosition><xsl:value-of 
> select="$posCtr"/></PickPosition>
>  
>                         <xsl:copy-of select="File"/>
>                         <xsl:copy-of select="Routing"/>
>                         <xsl:copy-of select="TransferProtocol"/>
>                         <xsl:copy-of select="TransferQueue"/>
>                 </FileService>
>         </xsl:template>
> 
> 
> The XML is being passed to the JAVA service for each File 
> element (which 
> means multiple times which is what I require) however the 
> <PickPosition> 
> element has no value on the output.
> 
> If I force the line <xsl:param name="loopCtr"/> to read <xsl:param 
> name="loopCtr select"12345"/> then I do get a 12345 output 
> for the param 
> and element.
> 
> However I get nothing passed to the template with the for 
> each line of 
> <xsl:with-param name="loopCtr" select="position()"/>.
> Even if I force that with a dummy value like so <xsl:with-param 
> name="loopCtr" select="99999"/> I still get no value! It 
> seems unable to 
> set and pass the loopCtr value to the ReaperMode template. 
> All the rest 
> seems to work.
> 
> I must be missing something but as I understood it this is 
> the way params 
> worked :(
> 
> 
> Regards Rick
> 
> Direct Line Group Limited, registered in England 
> with number 2811437, registered office 3 Edridge 
> Road, Croydon, Surrey  CR9 1AG.  The following 
> companies are members of the Direct Line Group: 
> Direct Line Insurance plc, Direct Line Life 
> Insurance Company Limited, Direct Line Unit 
> Trusts Limited and Direct Line Financial 
> Services Limited, all of which are authorised 
> and regulated by the Financial Services Authority.
> All are members of The Royal Bank of Scotland Group.
> 
> This email is intended for the addressee only and 
> may contain confidential, proprietary or legally 
> privileged information.  If you are not the 
> intended recipient of this email you should 
> notify us immediately and delete it.  You should 
> not copy, print, distribute, disclose or use any 
> part of it.  We reserve the right to monitor and 
> record all electronic communications through our 
> networks.  We cannot accept any liability for 
> viruses transmitted via this email once it has 
> left our network.


Current Thread
Keywords