Dmitriy Ryabin
Dmitriy Ryabin

Reputation: 468

xsl global variable access in templates

I use xsl stylesheet to create PDF from xml. There is a large number of templates used to display various sections.

Inside many templates, I display various headers with background color

<fo:block ... background-color="#2D338E" ...>..</fo:block>

Now, I want the color to be contingent on some condition. So, there are now 2 values for the color - #2D338E and #2D458E (just for an example) I create a variable

 <xsl:variable name="color"><!-- I define conditions here -->
 </xsl:variable>

The problem I am having is that in order to use it in all the templates, I have to define a color parameter in all of them, and then pass that created variable as the value of that parameter. I can't "see" the variable "color" from within a template definition, if I don't pipe it via argument. Can this be fixed? Can I define that variable once, somewhere, and then access it? It is too long to go through each template adding parameter. And even longer to pass it, since some templates can be called more than once.

Upvotes: 0

Views: 496

Answers (1)

Michael Kay
Michael Kay

Reputation: 163468

It's not clear to me from your description whether the colour will be the same for all instances within a transformation. If it is, you can define it as a global variable. However, if the colour in some sections differs from the colour in other sections, this won't work.

XSLT 2.0 has the solution, namely tunnel parameters. If you set a parameter using xsl:with-param[@tunnel='yes'], then it becomes accessible to indirectly-called templates as well as directly-called ones, which avoids the problem you describe of declaring the parameter in every template. You haven't said in your post whether you are using XSLT 2.0, or if moving to XSLT 2.0 is an option.

Upvotes: 1

Related Questions