使用数据库值

时间:2015-11-03 09:54:22

标签: java xslt xpath

<xsl:template match="">
 <fo:inline  color="#ff0000">
      <xsl:apply-templates select=""/>
  </fo:inline>

这里#ff0000是静态的,但我需要使用DB Value动态更新。

数据库值:msg = <p><span style="color: #ff0000;">Test</span></p> 这里<fo:inline color="#ff0000">颜色是动态的。 P

1 个答案:

答案 0 :(得分:0)

XSLT在XML上运行,如果XML值来自数据库,则它并不真正相关。但无论如何,假设您的XML看起来像这样:

<p><span style="color: #ff0000;">Test</span></p>

你可以提取&#34;颜色&#34;从风格,使用这个表达

normalize-space(substring-after(substring-before(@style, ';'), 'color:'))

然后,您可以使用属性值模板填充输出中的color属性

<fo:inline color="{normalize-space(substring-after(substring-before(@style, ';'), 'color:'))}">

试试这个示例XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:fo="...">
    <xsl:output method="xml" encoding="UTF-8" indent="yes" />

    <xsl:template match="span">
        <fo:inline color="{normalize-space(substring-after(substring-before(@style, ';'), 'color:'))}">
            <xsl:apply-templates />
        </fo:inline>
    </xsl:template>

    <xsl:template match="p">
        <fo:block>
            <xsl:apply-templates />
        </fo:block>
    </xsl:template>
</xsl:stylesheet>

或者,如果有可能出现&#34;颜色&#34;不存在,你可以这样做

<xsl:template match="span">
    <fo:inline>
        <xsl:if test="contains(@style, 'color')">
            <xsl:attribute name="color">
                <xsl:value-of select="normalize-space(substring-after(substring-before(@style, ';'), 'color:'))" />
            </xsl:attribute>
        </xsl:if>
        <xsl:apply-templates />
    </fo:inline>
</xsl:template>