gpt4 book ai didi

java - 如何使用 xsl-fo 渲染 xslt 中 span 标记内的字体颜色和大小等样式?

转载 作者:行者123 更新时间:2023-12-02 10:21:29 25 4
gpt4 key购买 nike

我正在根据富文本编辑器的输出生成 pdf 文件,某些组件(如字体颜色、特定单词或段落的字体大小)如下

 <p>Hello Hi <strong>skansdjnsjc</strong>
<span style="color:#ce181e"><em>cddsklncjkdsv</em></span>
<span style="color:#ce181e">sdsadsad</span></p>

在我的 xslt 文件中,我做了一个模板匹配,例如

  <xsl:template match="span">     
<xsl:variable name="color">
<xsl:choose>
<xsl:when test="@color">
<xsl:value-of select="@color"/>
</xsl:when>
<xsl:otherwise>
<xsl:text>black</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
</xsl:template>

但是 pdf 文件中未呈现所需的样式。我错过了什么吗?或者有什么解决办法吗?

感谢您提前提供帮助!!!

最佳答案

在 XSLT 2.0 中,您可以像这样从样式属性中提取颜色

<xsl:variable name="extractColor" select="tokenize(tokenize(@style, ';')[normalize-space(substring-before(., ':')) = 'color'], ':')[2]" />

然后,要设置您的 color 变量(如果未提取颜色,则将其设置为 black),请执行以下操作...

<xsl:variable name="color" select="($extractColor, 'black')[1]" />

当然,如果您扩展以提取其他值,则可以创建一个函数。

尝试这个 XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:my="my">
<xsl:output method="html" indent="yes" html-version="5"/>

<xsl:template match="span">
<span>
<xsl:variable name="color" select="(my:extract(@style, 'color'), 'black')[1]" />
<xsl:value-of select="$color" />
</span>
</xsl:template>

<xsl:function name="my:extract">
<xsl:param name="text" />
<xsl:param name="name" />
<xsl:sequence select="tokenize(tokenize($text, ';')[normalize-space(substring-before(., ':')) = $name], ':')[2]" />
</xsl:function>
</xsl:stylesheet>

关于java - 如何使用 xsl-fo 渲染 xslt 中 span 标记内的字体颜色和大小等样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54321774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com