在XSLT中切换属性集的最聪明的方法



我目前正在做这样的事情,感觉真的很糟糕的编码风格。<fo:table-row><fo:table-cell>的结构完全相同,只有xsl:use-attribute-sets不同。切换属性集最聪明的方法是什么?XSL版本没有限制。

<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>

正如您所发现的,属性集机制是非常静态的。如果您有动态需求,我认为最好避免使用属性集(实际上,我自己很少使用它们)。

做一些像

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

并从f:my-attribute-sets函数生成属性。

像这样?(未经测试,因为我没有你的输入来测试。)

<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>

最新更新