Re: Setting runtime variables, multiple passes on same file, etc.

Nathan Nadeau <[email protected]> Wed, 26 May 2010 11:22:29 -0400
Newsgroups gmane.text.xml.xalan.java.user
Organization Gleim Publications
Message-ID <[email protected]>

L Duperval wrote:
> 1) How can I set a runtime variable depending on the value of SOMECODE? I tried: 
>
> <xsl:template match="DATAFILE">
>
> <xsl:if test="@name='CODE1'">
>   <xsl:message>
>     <xsl:value-of select="@NAME" />
>   </xsl:message>
>   <xsl:variable name="filename" select="concat($outputDir,'/en/can.xml')" />
> </xsl:if>
> <xsl:if test="@name='CODE2'">
>   <xsl:message>
>     <xsl:value-of select="@NAME" />
>   </xsl:message>
>   <xsl:variable name="filename" select="concat($outputDir,'/en/int.xml')" />
> </xsl:if>
>
> <redirect:write select="$filename" indent="yes">
>  .
>  .
>  .
> </redirect:write>
> </xsl:template match="DATAFILE">
>
> When I run it though, I get an error saying the "filename" variable is
> undefined, however the message prints on stdout.
>   
A variable's scope in XSLT is only the containing block/element. 
Therefore if you define a variable in an <xsl:if>, it is only in scope 
with that <xsl:if> block, which is why it's telling you that $filename 
is undefined when you try to reference it outside of the <xsl:if>. If 
you need to compute a variable based on some conditions, you would put 
the conditions within the <xsl:variable> definition block.
> 2) I created a second XSL file where the only difference from above is that the
> "/en/" in the filename is changed to "/fr/". Is there a way for me to combine
> that in the same XSL file?
>   
Your code could be put into a template with a parameter representing the 
language, and use the parameter to build your output directory, then 
call the template as many times as needed with the appropriate language 
parameter. Basic idea is to use a variable for this value, rather than 
rewriting the same code twice with only the path being different.
> 3) I have another pair of XSL documents which act in a similar manner, but where
> the difference is that I rename attributes based on language. In one case, it
> deletes all **_fr attributes and removes the "_en" suffix from the others. The
> second files deletes all **_en attributes and removes the "_fr" from the other
> attributes.
>
> In an ideal world, I would like to create a single XSL which would generate all
> my output in one fell swoop, instead of making me parse the same data multiple
> times and sending it through different XSLs. But maybe I'm dreaming in
>   
You can certainly set this up in a single XSLT using <xsl:apply 
templates> with appropriate @select and/or @mode values. You would then 
write templates that match what you want to process, pull out only the 
things you want, rename whatever you want, and redirect to the desired 
output file. Then it's just a question of applying those templates in 
the desired order.
> Technicolor...
>
> Thanks!
>
> L
>
>   

-- 
Nathan Nadeau
[email protected]
Software Development
Gleim Publications, Inc.
http://www.gleim.com