0

I am transforming my XML document into PDF document thru Apache-FOP project, So far so good. Except, I am having which is not easy to configure later on. My code snippet is like this,

<fo:table-cell>
    <fo:block margin-top="5pt" text-align="left"><xsl:value-of select="" /></fo:block>
</fo:table-cell>
<fo:table-cell>
    <fo:block margin-top="5pt" text-align="center"><xsl:value-of select="" /></fo:block>
</fo:table-cell>
<fo:table-cell>
    <fo:block margin-top="5pt" text-align="center"><xsl:value-of select="" /></fo:block>
</fo:table-cell>
<fo:table-cell>
    <fo:block margin-top="5pt" text-align="center"><xsl:value-of select="" /></fo:block>
</fo:table-cell>
<fo:table-cell>
    <fo:block margin-top="5pt" text-align="center"><xsl:value-of select="" /></fo:block>
</fo:table-cell>
<fo:table-cell>
     <fo:block margin-top="5pt" text-align="center"><xsl:value-of select="" /></fo:block>
</fo:table-cell>

As you can see in above code, I have margin-top="5pt" text-align="center" quite often. I have tried to find a way so that I can write this values only once and later I can just change one variable which then affects everyone.


Research finding :

What I found so far is, that I can use a parameter in XSLT and define a variable. later, I can use, The parameter value can be 5pt. and then I use it like this.

<xsl:attribute name="margin-top">$var</xsl:attribute>

But this isn't a good solution as it make my code completely unreadable(well not completely, but you know what I mean). Is there anything like CSS in XSLT?

Community
  • 1
  • 1
TeaCupApp
  • 11,316
  • 18
  • 70
  • 150

1 Answers1

4

Using xsl:attribute can be quite a long-winded way of doing it. Better to make use of "Attribute Value Templates" here, which allow you to specify your code in-line

<fo:block margin-top="{$var}" text-align="{$var2}">

The curly braces indicate the value is an expression to be evaluated, rather than literally output.

Carrying on from this, I think xsl:attribute-set could be your friend here. This would allow you to create a set of attributes that can be applied to any element later on. To start with, you would define your attribute set, like so: (Note, this should go under the xsl:stylesheet element)

<xsl:attribute-set name="block">
  <xsl:attribute name="margin-top">5pt</xsl:attribute>
  <xsl:attribute name="text-align">left</xsl:attribute>
</xsl:attribute-set> 

Then, to use it, you can just use the xsl:use-attribute-sets attribute

<fo:block xsl:use-attribute-sets="block">

This should output the following:

<fo:block margin-top="5pt" text-align="left">

Furthermore, you could still "parameterise" your attribute set, should you wish to set the values from your calling program. For example, the following should also work

<xsl:param name="margin-top" select="'6pt'" />
<xsl:param name="text-align" select="'left'" />

<xsl:attribute-set name="block">
  <xsl:attribute name="margin-top"><xsl:value-of select="$margin-top" /></xsl:attribute>
  <xsl:attribute name="text-align"><xsl:value-of select="$text-align" /></xsl:attribute>
</xsl:attribute-set> 
Tim C
  • 70,053
  • 14
  • 74
  • 93