xslt - 如何在不扩大行高的情况下在段落的第一行生成首字母?

标签 xslt pdf xsl-fo apache-fop

我想要一个段落的第一行的首字母和所有其他段落的文本缩进。我正在使用以下代码:

<xsl:template match="paragraph">
    <xsl:choose>
        <!-- no text-indent, first letter bigger -->
        <xsl:when test="fn:position() = 1">
            <fo:block font-size="10pt" font-height="12pt">
                <fo:inline font-size="18pt"><xsl:value-of 
                  select="substring(.,1,1)"/></fo:inline>
                <xsl:value-of select="substring(.,2)"/>
            </fo:block>
        </xsl:when>
        <xsl:otherwise>
            <!-- text-indent --> 
            <fo:block line-height="12pt"  
                text-indent="10pt">
                <xsl:value-of select="."/></fo:block>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

这有效,但第一行的行高被放大,并且行之间有太多空间。 enter image description here

行高属性或 <fo:initial-property-set>不工作。
非常感谢。

编辑:我正在使用 fop

编辑:FOP 不支持 <fo:float><fo:initial-property-set> .我使用 <fo:list> 尝试了另一个代码:
        <xsl:when test="fn:position() = 1">
            <fo:block font-family="Times" font-size="10pt" line-height="12pt"> 
                <fo:list-block>
                    <fo:list-item>
                        <fo:list-item-label>
                            <fo:block font-size="18pt"><xsl:value-of select="substring(.,1,1)"/></fo:block>
                        </fo:list-item-label>
                        <fo:list-item-body>
                            <fo:block><xsl:value-of select="substring(.,2)"/></fo:block>
                        </fo:list-item-body>
                    </fo:list-item>
                </fo:list-block>
            </fo:block>
        </xsl:when>

结果如下所示:

enter image description here

所以我使用了图形空间 &#8199;<fo:list-item-body> 的 value-of 选择模式中:
        <xsl:when test="fn:position() = 1">
            <fo:block font-family="Times" font-size="10pt" line-height="12pt"> 
                <fo:list-block>
                    <fo:list-item>
                        <fo:list-item-label>
                            <fo:block font-size="18pt"><xsl:value-of select="substring(.,1,1)"/></fo:block>
                        </fo:list-item-label>
                        <fo:list-item-body>
                            <fo:block><xsl:value-of select="('&#8199;', '&#8199;', substring(.,2))"/></fo:block>
                        </fo:list-item-body>
                    </fo:list-item>
                </fo:list-block>
            </fo:block>
        </xsl:when> 

这不仅是陈词滥调,而且效果也不是很好:

enter image description here

enter image description here

有没有人有办法解决吗?

最佳答案

这个 xsl 片段,对问题中的第一个片段进行了最低限度的修改,使用 Apache FOP 产生了所需的结果:

<xsl:template match="paragraph">
    <xsl:choose>
        <!-- no text-indent, first letter bigger -->
        <xsl:when test="not(preceding-sibling::paragraph)">
            <fo:block line-height="12pt" 
                line-stacking-strategy="font-height">
                <fo:inline font-size="18pt"><xsl:value-of 
                  select="substring(.,1,1)"/></fo:inline>
                <xsl:value-of select="substring(.,2)"/>
            </fo:block>
        </xsl:when>
        <xsl:otherwise>
            <!-- text-indent --> 
            <fo:block line-height="12pt"  
                text-indent="10pt">
                <xsl:value-of select="."/></fo:block>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

要点:
  • 所有 fo:blocks 都被赋予相同的 行高
  • 第一个有 line-stacking-strategy="font-height" ,因此格式化程序使用 nominal-requested-line-rectangle 构建行,而不管内联子项
  • 的尺寸
  • 测试条件 position() = 1 ,正如在问题中所做的那样,如果输入文件缩进(因为 child #1 可能是仅由空格组成的文本节点,“第一个”段落元素)将是 child #2),所以我检查 not(preceding-sibling::paragraph) 而不是
  • 第一段首字母溢出结果行区域;如果需要,可以在 fo:block 上设置一些 前空格 以避免这种情况
  • 关于xslt - 如何在不扩大行高的情况下在段落的第一行生成首字母?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21501333/

    相关文章:

    java - XSLT:找不到 Java 类异常

    java - 获取 PDF Java Web 中的当前页面

    c# - 文件上传到 Windows Azure Blob

    xml - 使用 XSLT 生成 XSLT 的一些有用结构是什么?

    html - 如何对齐 block 中的两个行内元素

    xml - 如何在没有源 xml 文件根节点的情况下将一个 xml 文件包含在另一个 xml 中?

    xpath - XSLT 根据条件更改特定元素的属性值

    html - 如果我使用 XSLT,我会得到 HTML 页面吗?

    email - 将事件工作表作为 PDF 发送到单元格中列出的电子邮件

    xml - XSL-FO 外部图形未显示