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

时间:2016-08-02 01:41:48

标签: xml xslt xsl-fo

问题:如何使用xsl:fo?

为span元素添加颜色

假设我有一个带有段落块的文档,如下所示:

<?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元素显示为绿色或红色?

1 个答案:

答案 0 :(得分:3)

您的样式表正在尝试输出&#34; span&#34;当它应该输出一个fo:inline元素时。当它需要输出XSL FO等价物时,它也试图输出CSS属性样式。

添加FO名称空间:

from next_meetup
               ^
SyntaxError: invalid syntax

并将模板更改为:

from next_meetup import get_next_meetup
from next_meetup import next_meetup

产生此输出:

enter image description here

相关问题