Smartest way to switch attribute sets in XSLT

Viewed 36

I'm currently doing something like this, and it feels like really bad coding style. The structure of the <fo:table-row> and <fo:table-cell> elements is exactly the same, only the xsl:use-attribute-sets is different. What is the smartest way to switch the attribute sets? The XSL version is no limitation.

<xsl:template name="myTemplate">
    <xsl:param name="number-of-parts" as="xs:integer"/>
    <xsl:choose>
        <xsl:when test="$number-of-parts &lt;= 16">
            <fo:table-row xsl:use-attribute-sets="__toc__mini__table__row__empty">
                <fo:table-cell xsl:use-attribute-sets="__toc__mini__table__row__empty__cell">
                    <fo:block/>
                </fo:table-cell>
                <fo:table-cell xsl:use-attribute-sets="__toc__mini__table__row__empty__cell">
                    <fo:block/>
                </fo:table-cell>
                <fo:table-cell xsl:use-attribute-sets="__toc__mini__table__row__empty__cell">
                    <fo:block/>
                </fo:table-cell>
            </fo:table-row>
        </xsl:when>
        <!-- If more than 16 languages -->
        <xsl:otherwise>
            <fo:table-row xsl:use-attribute-sets="__toc__mini__table__row__empty__small">
                <fo:table-cell xsl:use-attribute-sets="__toc__mini__table__row__empty__cell__small">
                    <fo:block/>
                </fo:table-cell>
                <fo:table-cell xsl:use-attribute-sets="__toc__mini__table__row__empty__cell__small">
                    <fo:block/>
                </fo:table-cell>
                <fo:table-cell xsl:use-attribute-sets="__toc__mini__table__row__empty__cell__small">
                    <fo:block/>
                </fo:table-cell>
            </fo:table-row>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>
2 Answers

The attribute-set mechanism is very static, as you have discovered. If you have a dynamic requirement, I think attribute sets are best avoided (actually, I hardly ever use them myself).

Do something like

<fo:table-cell>
    <xsl:sequence select="f:my-attribute-sets('small')">
    <fo:block/>
</fo:table-cell>

and generate the attributes from your f:my-attribute-sets function.

Something like this? (Untested because I don't have your input to test with.)

<xsl:template name="myTemplate">
    <xsl:param name="number-of-parts" as="xs:integer"/>
    <xsl:variable name="table_row_variable">
        <xsl:choose>
            <xsl:when test="$number-of-parts &lt;= 16">__toc__mini__table__row__empty</xsl:when>
            <xsl:otherwise>__toc__mini__table__row__empty__cell__small</xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <fo:table-row xsl:use-attribute-sets="{$table_row_variable}">
        <fo:table-cell xsl:use-attribute-sets="{$table_row_variable}">
            <fo:block/>
        </fo:table-cell>
        <fo:table-cell xsl:use-attribute-sets="{$table_row_variable}">
            <fo:block/>
        </fo:table-cell>
        <fo:table-cell xsl:use-attribute-sets="{$table_row_variable}">
            <fo:block/>
        </fo:table-cell>
    </fo:table-row>
</xsl:template>
Related