FlowDocument到HTML的转换-如何在XSLT中选择前景色?

FlowDocument到HTML的转换-如何在XSLT中选择前景色?,html,xml,xslt,Html,Xml,Xslt,我已将XSLT从改编并扩展到: <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:msxsl="urn:schema

我已将XSLT从改编并扩展到:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    exclude-result-prefixes="msxsl x">

  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

  <xsl:template match="x:Section[not(parent::x:Section)]">
    <div>
      <xsl:apply-templates select="node()"/>
    </div>
  </xsl:template>

  <xsl:template match="x:Section">
    <xsl:apply-templates select="node()"/>
  </xsl:template>

  <xsl:template match="x:Paragraph">
    <p>
      <xsl:apply-templates select="node()"/>
    </p>
  </xsl:template>

  <xsl:template match="x:Run">
    <xsl:variable name="style">
      <xsl:if test="@FontStyle='Italic'">
        <xsl:text>font-style:italic;</xsl:text>
      </xsl:if>
      <xsl:if test="@FontWeight='Bold'">
        <xsl:text>font-weight:bold;</xsl:text>
      </xsl:if>
      <xsl:if test="contains(@TextDecorations, 'Underline')">
        <xsl:text>text-decoration:underline;</xsl:text>
      </xsl:if>
      <xsl:if test="@FontSize != ''">
        <xsl:text>font-size:</xsl:text>
        <xsl:value-of select="@FontSize" />
        <xsl:text>pt;</xsl:text>
      </xsl:if>
      <xsl:if test="@FontFamily != ''">
        <xsl:text>font-family:</xsl:text>
        <xsl:value-of select="@FontFamily" />
        <xsl:text>;</xsl:text>
      </xsl:if>
      <xsl:if test="@Foreground-Color != ''">
        <xsl:text>color:</xsl:text>
        <xsl:value-of select="@Foreground-Color"/>
        <xsl:text>;</xsl:text>
      </xsl:if>
    </xsl:variable>
    <span>
      <xsl:if test="normalize-space($style) != ''">
        <xsl:attribute name="style">
          <xsl:value-of select="normalize-space($style)"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:value-of select="text()"/>
    </span>
  </xsl:template>
</xsl:stylesheet>


字体:斜体; 字体大小:粗体; 文字装饰:下划线; 字体大小: pt; 字体系列: ; 颜色: ;
除了这一部分之外,此XSLT工作得非常好:

  <xsl:if test="@Foreground-Color != ''">
    <xsl:text>color:</xsl:text>
    <xsl:value-of select="@Foreground-Color"/>
    <xsl:text>;</xsl:text>
  </xsl:if>

颜色:
;
除了前景色,我试过彩色和FontColor,但似乎都不起作用


提取文本前景色的“test=”和“select=”之后的正确关键字是什么?

当您使用CSS内联样式创建HTML时,与其说是XSLT问题,不如说是CSS问题。CSS中正确的属性名实际上是
color
,因此该部分是正确的。但是,我不知道您的输入格式使用了哪些颜色值,以及它们是否等于CSS理解的颜色名称。因此,向我们展示一个输入示例,并检查CSS颜色属性值spec:

至于属性,基于属性可能只是

  <xsl:if test="@Foreground != ''">
    <xsl:text>color:</xsl:text>
    <xsl:value-of select="@Foreground"/>
    <xsl:text>;</xsl:text>
  </xsl:if>

颜色:
;

我还没有检查可能的颜色值及其与CSS值的匹配情况。

谢谢。它选择了#ARGB格式的颜色,这显然是浏览器无法识别的,因此我必须先做一个子字符串来去除a值:出于其他原因,我必须在位置4而不是2处开始子字符串,但除此之外,它还能工作。