【问题标题】:XSL-FO make text different color in single elementXSL-FO 使单个元素中的文本颜色不同
【发布时间】:2014-11-05 16:25:30
【问题描述】:

我整个上午都在寻找这个问题的答案,但没有运气。也许我只是没有正确搜索,我不知道。我正在使用以下标签集在 PDF 中显示 XML 标记:

<verbatimText>&lt;para>&lt;quantity>
&lt;quantityGroup quantityUnitOfMeasure="ft.lbf">&lt;quantityValue>10&lt;/quantityValue>
&lt;quantityTolerance quantityToleranceType="plusorminus" quantityUnitOfMeasure="ft.lbf">  2&lt;/quantityTolerance>&lt;/quantityGroup>
&lt;/quantity>.&lt;/para></verbatimText>

在 PDF 中将其显示为全 BOLD 单色块没有问题。我想要尝试并完成的是使 XML 语法着色,很像文本编辑器中的语法突出显示,其中元素名称为蓝色,属性为红色,元素内容为黑色。

我认为有一种方法可以格式化文本的子字符串,但是我很难找到一个好的起点。

【问题讨论】:

  • 你能使用 XSLT 2.0 吗?
  • 不,我正在通过 FOP 运行 XSL 转换,最后我知道它不支持 2.0。
  • 有什么理由不能将 xslt 转换与 fo 处理分开吗?
  • 简短回答,可能不是。我的 XSLT 现在生成一个 FO 文档。如果我要使用两种不同的变换来获得最终的 FO,您的建议是什么?那么我是否不需要编写第二个 XSL 文档来完成我的转换?如果我看得太短视了,请原谅我。
  • 您不必拥有第二个 XSLT。我的建议是使用像 Saxon 这样的 XSLT 2.0 处理器将您的 XML 转换为 XSL-FO(使用您的 XSLT)。然后用 FOP 处理 XSL-FO 文件。我总是将我的 XSLT (XML->XSL-FO) 与 XSL-FO 文件的处理分开。

标签: xslt xsl-fo


【解决方案1】:

这是一个示例,说明如何使用 XSLT 2.0 (xsl:analyze-string) 添加颜色。

它可以进行一些调整,但说明了我的想法......

XML 输入

<verbatimText>&lt;para>&lt;quantity>
&lt;quantityGroup quantityUnitOfMeasure="ft.lbf">&lt;quantityValue>10&lt;/quantityValue>
&lt;quantityTolerance quantityToleranceType="plusorminus" quantityUnitOfMeasure="ft.lbf">  2&lt;/quantityTolerance>&lt;/quantityGroup>
&lt;/quantity>.&lt;/para></verbatimText>

XSLT 2.0

<xsl:stylesheet version="2.0" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/">
        <fo:root>
            <fo:layout-master-set>
                <fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in">
                    <fo:region-body margin="1in" margin-top="1.5in" margin-bottom="1.5in"/>
                </fo:simple-page-master>
            </fo:layout-master-set>
            <fo:page-sequence master-reference="my-page">
                <fo:flow flow-name="xsl-region-body"> 
                    <xsl:apply-templates/>
                </fo:flow>
            </fo:page-sequence>
        </fo:root>
    </xsl:template>

    <xsl:template match="verbatimText">
        <fo:block>
            <xsl:analyze-string select="." regex="(&lt;/?)([^\s>]+)(>?)">
                <xsl:matching-substring>
                    <fo:inline color="#0000FF"><xsl:value-of select="concat(regex-group(1),'&#xFEFF;',regex-group(2),
                        if (regex-group(3)) then '&#xFEFF;>' else '')"/></fo:inline>
                </xsl:matching-substring>
                <xsl:non-matching-substring>
                    <xsl:analyze-string select="." regex="(\s)([^=&lt;]+=['&quot;])([^'&quot;]+)(['&quot;])(>?)">
                        <xsl:matching-substring>
                            <xsl:value-of select="regex-group(1)"/>
                            <fo:inline color="#FF0000"><xsl:value-of select="regex-group(2)"/></fo:inline>
                            <xsl:value-of select="regex-group(3)"/>
                            <fo:inline color="#FF0000"><xsl:value-of select="concat(regex-group(4),
                                if (regex-group(5)) then '&#xFEFF;>' else '')"/></fo:inline>
                        </xsl:matching-substring>
                        <xsl:non-matching-substring>
                            <xsl:value-of select="."/>
                        </xsl:non-matching-substring>
                    </xsl:analyze-string>
                </xsl:non-matching-substring>
            </xsl:analyze-string>                            
        </fo:block>
    </xsl:template>

</xsl:stylesheet>

XSL-FO(使用 Saxon-HE 9.5)

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
   <fo:layout-master-set>
      <fo:simple-page-master master-name="my-page" page-width="8.5in" page-height="11in">
         <fo:region-body margin="1in" margin-top="1.5in" margin-bottom="1.5in"/>
      </fo:simple-page-master>
   </fo:layout-master-set>
   <fo:page-sequence master-reference="my-page">
      <fo:flow flow-name="xsl-region-body">
         <fo:block>
            <fo:inline color="#0000FF">&lt;para&gt;</fo:inline>
            <fo:inline color="#0000FF">&lt;quantity&gt;</fo:inline>
            <fo:inline color="#0000FF">&lt;quantityGroup</fo:inline> 
            <fo:inline color="#FF0000">quantityUnitOfMeasure="</fo:inline>ft.lbf<fo:inline color="#FF0000">"&gt;</fo:inline>
            <fo:inline color="#0000FF">&lt;quantityValue&gt;</fo:inline>10<fo:inline color="#0000FF">&lt;/quantityValue&gt;</fo:inline>
            <fo:inline color="#0000FF">&lt;quantityTolerance</fo:inline> 
            <fo:inline color="#FF0000">quantityToleranceType="</fo:inline>plusorminus<fo:inline color="#FF0000">"</fo:inline> 
            <fo:inline color="#FF0000">quantityUnitOfMeasure="</fo:inline>ft.lbf<fo:inline color="#FF0000">"&gt;</fo:inline>  2<fo:inline color="#0000FF">&lt;/quantityTolerance&gt;</fo:inline>
            <fo:inline color="#0000FF">&lt;/quantityGroup&gt;</fo:inline>
            <fo:inline color="#0000FF">&lt;/quantity&gt;</fo:inline>.<fo:inline color="#0000FF">&lt;/para&gt;</fo:inline>
         </fo:block>
      </fo:flow>
   </fo:page-sequence>
</fo:root>

PDF 输出(使用 FOP 1.1)

【讨论】:

  • 这太棒了。我需要复习一下我的正则表达式,才能完全理解你使用的过滤器(有点惭愧地说我不知道​​)。我唯一改变的是属性后的结束“>”被涂成红色并变成蓝色。
  • @twfurst - 我没有发现那些 > 是红色的。很高兴我的例子有所帮助。
猜你喜欢
  • 1970-01-01
  • 2010-11-08
  • 2014-05-12
  • 1970-01-01
  • 2017-06-19
  • 1970-01-01
  • 2020-09-13
  • 1970-01-01
  • 2018-12-28
相关资源
最近更新 更多