Xslt 在字符串长度后强制换行
我想在使用AH Formatter生成的PDF中的字符串长度为14个字符后强制换行。这是我的xsl代码,没有任何断行的尝试:Xslt 在字符串长度后强制换行,xslt,xsl-fo,antenna-house,Xslt,Xsl Fo,Antenna House,我想在使用AH Formatter生成的PDF中的字符串长度为14个字符后强制换行。这是我的xsl代码,没有任何断行的尝试: <xsl:attribute-set name="big" use-attribute-sets="bold"> <xsl:attribute name="font-size">38pt</xsl:attribute> <xsl:attribute name="line-height">28.84pt</xsl
<xsl:attribute-set name="big" use-attribute-sets="bold">
<xsl:attribute name="font-size">38pt</xsl:attribute>
<xsl:attribute name="line-height">28.84pt</xsl:attribute>
<xsl:attribute name="text-align">center</xsl:attribute>
<xsl:attribute name="letter-spacing">1mm</xsl:attribute>
</xsl:attribute-set>
<xsl:attribute-set name="small" use-attribute-sets="bold">
<xsl:attribute name="font-size">27pt</xsl:attribute>
<xsl:attribute name="line-height">27pt</xsl:attribute>
<xsl:attribute name="text-align">center</xsl:attribute>
<xsl:attribute name="letter-spacing">1mm</xsl:attribute>
</xsl:attribute-set>
<xsl:choose>
<xsl:when test="string-length($count_cover)>=14">
<fo:block xsl:use-attribute-sets="small">
<xsl:apply-templates/>
</fo:block>
</xsl:when>
<xsl:otherwise>
<fo:block xsl:use-attribute-sets="big">
<xsl:apply-templates/>
</fo:block>
</xsl:otherwise>
</xsl:choose>
38磅
28.84磅
居中
1毫米
27磅
27磅
居中
1毫米
可以用XSL-FO强制换行吗?在FO中不能强制换行,但可以将字符串拆分为单独的FO块
<xsl:choose>
<xsl:when test="string-length($count_cover) >= 14">
<fo:block><xsl:value-of select="substring($count_cover, 1, 13)"/></fo:block>
<fo:block><xsl:value-of select="substring($count_cover, 14)"/></fo:block>
</when>
<xsl:otherwise>
<fo:block>
<xsl:value-of select="$count_cover"/>
</fo:block>
</xsl:otherwise>
</xsl:choose>
在FO中不能强制换行,但可以将字符串拆分为单独的FO块
<xsl:choose>
<xsl:when test="string-length($count_cover) >= 14">
<fo:block><xsl:value-of select="substring($count_cover, 1, 13)"/></fo:block>
<fo:block><xsl:value-of select="substring($count_cover, 14)"/></fo:block>
</when>
<xsl:otherwise>
<fo:block>
<xsl:value-of select="$count_cover"/>
</fo:block>
</xsl:otherwise>
</xsl:choose>
如果标题可以转换为字符串,则可以插入
作为换行符
<xsl:variable name="cover_title" as="xs:string" select="'Very Long Cover Title! Very Long Cover Title! Very Long Cover Title! '"/>
<xsl:variable name="count_cover" as="xs:integer" select="string-length($cover_title)"/>
<xsl:variable name="lf_position" as="xs:integer" select="14"/>
<xsl:template match="/">
<xsl:choose>
<xsl:when test="$count_cover gt $lf_position">
<fo:block xsl:use-attribute-sets="small">
<xsl:analyze-string select="$cover_title" regex=".{{1}}">
<xsl:matching-substring>
<xsl:value-of select="."/>
<xsl:if test="position() eq $lf_position">
<fo:block/>
</xsl:if>
</xsl:matching-substring>
<xsl:non-matching-substring/>
</xsl:analyze-string>
</fo:block>
</xsl:when>
<xsl:otherwise>
<fo:block xsl:use-attribute-sets="big">
<xsl:value-of select="$cover_title"/>
</fo:block>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
结果是:
<fo:block font-weight="bold" font-size="27pt" line-height="27pt" text-align="center" letter-spacing="1mm">Very Long Cove<fo:block/>r Title! Very Long Cover Title! Very Long Cover Title! </fo:block>
很长的封面标题!很长的封面标题!很长的封面标题!
但是,此方法忽略单词边界和断字控制。如果您打算制作书的封面标题,最好使用fo:block容器引入AH格式化程序扩展
很长的封面标题!很长的封面标题!很长的封面标题!
如果标题可以转换为字符串,则可以插入
作为换行符
<xsl:variable name="cover_title" as="xs:string" select="'Very Long Cover Title! Very Long Cover Title! Very Long Cover Title! '"/>
<xsl:variable name="count_cover" as="xs:integer" select="string-length($cover_title)"/>
<xsl:variable name="lf_position" as="xs:integer" select="14"/>
<xsl:template match="/">
<xsl:choose>
<xsl:when test="$count_cover gt $lf_position">
<fo:block xsl:use-attribute-sets="small">
<xsl:analyze-string select="$cover_title" regex=".{{1}}">
<xsl:matching-substring>
<xsl:value-of select="."/>
<xsl:if test="position() eq $lf_position">
<fo:block/>
</xsl:if>
</xsl:matching-substring>
<xsl:non-matching-substring/>
</xsl:analyze-string>
</fo:block>
</xsl:when>
<xsl:otherwise>
<fo:block xsl:use-attribute-sets="big">
<xsl:value-of select="$cover_title"/>
</fo:block>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
结果是:
<fo:block font-weight="bold" font-size="27pt" line-height="27pt" text-align="center" letter-spacing="1mm">Very Long Cove<fo:block/>r Title! Very Long Cover Title! Very Long Cover Title! </fo:block>
很长的封面标题!很长的封面标题!很长的封面标题!
但是,此方法忽略了单词边界和断字控制。如果您打算制作书籍封面标题,最好使用fo:block容器引入AH格式化程序扩展
很长的封面标题!很长的封面标题!很长的封面标题!
linefeed treatment=“preserve”
并插入
代码>而不是fo:block
,作为对注释的回答。您可以使用
代码>,在每14个字符之后,让AH格式化程序在零宽度空间中断:
内部的replace()
在每14个非空格字符后插入字符,外部的replace()
在第15个字符是空格字符时修复该字符
如果您使用的是比例宽度字体,则某些14个字符的序列(不包括,例如,14个恒定宽度的行号)将比其他字符的宽度大或小,因此您可能需要插入代码>在多个字符之间切换,以便AH格式化程序可以在中断前尽最大努力填充行
axf:word break=“break all”
在单词内部启用换行。看linefeed treatment=“preserve”
并插入
代码>而不是fo:block
,作为对注释的回答。您可以使用
代码>,在每14个字符之后,让AH格式化程序在零宽度空间中断:
内部的replace()
在每14个非空格字符后插入字符,外部的replace()
在第15个字符是空格字符时修复该字符
如果您使用的是比例宽度字体,则某些14个字符的序列(不包括,例如,14个恒定宽度的行号)将比其他字符的宽度大或小,因此您可能需要插入代码>在多个字符之间切换,以便AH格式化程序可以在中断前尽最大努力填充行
axf:word break=“break all”
在单词内部启用换行。看