XSL:FO 如何为 <p> 元素内的 <span> 元素添加颜色

XSL:FO How do I add color to the <span> elements inside of the <p> element

问题:如何使用 xsl:fo 为跨度元素添加颜色?

假设我有一个包含如下段落块的文档:

<?xml version="1.0" encoding="UTF-8"?>
<diffreport><css/><diff>
<p>One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin. He lay on his armour-like back, and if he lifted his head a little he could see his brown belly, slightly domed and divided by arches into stiff sections. The bedding was hardly able to cover it and seemed ready to slide off any moment. His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked. 
<span style="background-color:rgb(255, 255, 255); color:rgb(102, 102, 102)"><span class="diff-html-added" id="added-diff-0" previous="first-diff" changeId="added-diff-0" next="added-diff-1">Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts.</span></span>
</p>
</diff></diffreport>

我创建了一个 xsl:fo 模板文件,如下所示。

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:s="http://www.stylusstudio.com/xquery">
<xsl:template match="/">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
        <fo:layout-master-set>
            <fo:simple-page-master master-name="default-page" page-height="11in" page-width="8.5in" margin-left="0.6in" margin-right="0.6in" margin-top="0.79in" margin-bottom="0.79in">
                <fo:region-body/>
            </fo:simple-page-master>
        </fo:layout-master-set>
        <fo:page-sequence master-reference="default-page">
            <fo:flow flow-name="xsl-region-body">
                <fo:block>
                    <fo:block text-align="center">
                        <fo:block>
                            <xsl:text>Report</xsl:text>
                        </fo:block>
                    </fo:block>
                    <fo:table width="100%" border-style="outset" border-width="2pt" background-repeat="repeat">
                        <fo:table-column/>
                        <fo:table-body>
                            <fo:table-row>
                                <fo:table-cell border-style="inset" border-width="2pt" padding="2pt" background-repeat="repeat" display-align="center">
                                    <fo:block>
                                        <xsl:text>Document</xsl:text>
                                    </fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                            <fo:table-row>
                                <fo:table-cell border-style="inset" border-width="2pt" padding="2pt" background-repeat="repeat" display-align="center">
                                    <fo:block>
                                        <xsl:apply-templates/>
                                    </fo:block>
                                </fo:table-cell>
                            </fo:table-row>
                        </fo:table-body>
                    </fo:table>
                </fo:block>
            </fo:flow>
        </fo:page-sequence>
    </fo:root>
</xsl:template>
<xsl:template match="span[@class='diff-html-added']">
    <span style="color:green; background-color: #ccffcc;">
    <xsl:value-of select="."/></span>
</xsl:template>
<xsl:template match="span[@class='diff-html-removed']">
        <span style="color:red; text-decoration: line-through; background-color: #fdc6c6;">
        <xsl:value-of select="."/></span>
</xsl:template>
<xsl:template match="span[@class='diff-html-added']" mode="clean"/>
<xsl:template match="span[@class='diff-html-removed']" mode="clean"/>

该报告正在运行,但出于某种原因,我添加的用于在元素中包含红色和绿色的 xsl 模板对我不起作用,因为它没有显示。

<xsl:template match="span[@class='diff-html-added']">
    <span style="color:green; background-color: #ccffcc;">
    <xsl:value-of select="."/></span>
</xsl:template>

我需要做什么来修复我的 xsl 模板,以便 span 元素显示为绿色或红色?

您的样式表在本应输出 fo:inline 元素时尝试输出 "span" 元素。当需要输出 XSL FO 等价物时,它还尝试输出 CSS 属性样式。

添加 FO 命名空间:

<xsl:stylesheet version="1.0" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:s="http://www.stylusstudio.com/xquery">

并将您的模板更改为:

    <xsl:template match="span[@class='diff-html-added']">
    <fo:inline color="green" background-color="#ccffcc">
        <xsl:value-of select="."/></fo:inline>
</xsl:template>
<xsl:template match="span[@class='diff-html-removed']">
    <fo:inline color="red" text-decoration="line-through" background-color="#fdc6c6">
        <xsl:value-of select="."/></fo:inline>
</xsl:template>

产生这个输出: