我使用的是XSL-FO和FOP.95,每当我用XSL-FO编写代码时,我都必须使用此语句来生成一个空格:
<fo:block>
<xsl:choose>
<xsl:when test="normalize-space(Seller_Name)!=''">
<xsl:value-of select="normalize-space(Seller_Name)"/>
</xsl:when>
<xsl:otherwise><xsl:text> </xsl:text></xsl:otherwise>
</xsl:choose>
</fo:block>
我不想使用这些选择时的条件来生成一个空的空间来保存块崩溃。有什么函数或属性可以在这里使用吗?我试过换行处理和空白塌陷,但都没用。请给点建议。
如果你对上面的内容感到满意,为什么不将其模板化呢?这将使调用减少到三行:
<xsl:template name="blockwithblank">
<xsl:param name="field"/>
<fo:block>
<xsl:choose>
<xsl:when test="normalize-space($field)!=''">
<xsl:value-of select="$field"/>
</xsl:when>
<xsl:otherwise><xsl:text> </xsl:text></xsl:otherwise>
</xsl:choose>
</fo:block>
</xsl:template>
以上是整个样式表中的一次调用,那么每个调用只有三行:
<xsl:call-template name="blockwithblank">
<xsl:with-param name="field" select="Seller_Name"/>
</xsl:call-template>
我不确定你每次打电话能缩短三行以上。
使用两个模板:一个用于常规情况,另一个优先级更高,用于空情况:
<xsl:template match="Seller_Name">
<fo:block>
<xsl:value-of select="normalize-space()"/>
</fo:block>
</xsl:template>
<xsl:template match="Seller_Name[normalize-space() = '']" priority="5">
<fo:block> </fo:block>
</xsl:template>
XSLT需要在适当的位置使用xsl:apply-templates,但这会缩短当前模板的长度。
如果你经常用多个元素来做这件事,你可以在每个模板中匹配多个元素,这样可以节省很多重复。