Search code examples
xsltxslt-1.0xsl-fo

How to set the border style in one place and reference it throughout XSL


I would like to have one var/attribute that I set the border style and reference it so if i want to change the border from 1pt to 2pt I don't need to change it in various locations.

For example at the moment I do this;

<fo:table border="1pt solid black"  table-layout="fixed"
  width="100%" display-align="center">
 <fo:table-column column-width="10%" border-right="1pt solid black"/>
 <fo:table-column column-width="10%" border-right="1pt solid black"/>
 <fo:table-column column-width="23%" border-right="1pt solid black"/>
 <fo:table-column column-width="8%" border-right="1pt solid black"/>
 <fo:table-column column-width="11%" border-right="1pt solid black"/>
 <fo:table-column column-width="8%" border-right="1pt solid black"/>
 <fo:table-column column-width="20%" border-right="1pt solid black"/>
 <fo:table-header>...

I would prefer something like;

<xsl:variable
    name="border"
    select="1pt solid black">
</xsl:variable>

<fo:table border="$border"  table-layout="fixed"
  width="100%" display-align="center">
 <fo:table-column column-width="10%" border-right="$border"/>
 <fo:table-column column-width="10%" border-right="$border"/>
 <fo:table-column column-width="23%" border-right="$border"/>
 <fo:table-column column-width="8%" border-right="$border"/>
 <fo:table-column column-width="11%" border-right="$border"/>
 <fo:table-column column-width="8%" border-right="$border"/>
 <fo:table-column column-width="20%" border-right="$border"/>
 <fo:table-header>...

So my question really is this possible and if so whats the correct syntax?

Any help would be great,

Thanks in advance!


Solution

  • Define the variable like so (using apostrophes, to indicate an literal string, not a xpath expression)

    <xsl:variable name="border" select="'1pt solid black'" />
    

    Then, use Attribute Value Templates to use it in an attribute

    <fo:table-column column-width="20%" border-right="{$border}"/>
    

    Alternatively, you could achieve this with Attribute Sets. Define an attribute set like so

    <xsl:attribute-set name="border">
        <xsl:attribute name="border-right" select="'12pt solid black'" />
    </xsl:attribute-set>
    

    Then use it as follows

    <fo:table-column column-width="20%" xsl:use-attribute-sets="border"/>
    

    With attribute sets, you could have multiple attributes in the set.