如何为所有字段在xslt中设置一次颜色

时间:2015-12-18 19:43:54

标签: variables xslt colors

我可以设置变量或其他东西让我们说“红色”一次,当我想要一些字体为红色时我只是调用那个变量吗?这样我就可以轻松编辑所有指定文本的颜色,以备将来使用。我是xslt的新手,感谢任何帮助。谢谢。

编辑:添加一些我想要的代码。

<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<!--Probably declare the variable here-->
<!--Like <variable=outputcolor value="red" -->
    <fo:root>
        <fo:layout-master-set>
            <fo:simple-page-master master-name="A4-portrait"
                page-height="29.7cm" page-width="21.0cm" margin="2cm">
                <fo:region-body />
            </fo:simple-page-master>
        </fo:layout-master-set>
        <fo:page-sequence master-reference="A4-portrait">
            <fo:flow flow-name="xsl-region-body" font-family="Helvetica"
                font-size="6pt">
                <fo:block font-size="8pt" text-indent="5pt">
                    <fo:inline font-weight="bold"><xsl:text>Application Summary</xsl:text></fo:inline>
                        <!--Here i would like to make red a variable that i could possibly changed -->
                        <!--like fo:inline color="{outputcolor}"-->
                    <fo:inline color="red">
                        <xsl:value-of select="businessInfo/appSum" />
                    </fo:inline>
                </fo:block>
            </fo:flow>
        </fo:page-sequence>
    </fo:root>
</xsl:template>

2 个答案:

答案 0 :(得分:1)

您可以将named attribute set定义为xsl:stylesheet的直接子项,其中包含您要重复使用的属性:

<xsl:attribute-set name="colouredText">
  <xsl:attribute name="color">red</xsl:attribute>
  <!-- you can set other attributes too: font-weight, font-style, ... -->
</xsl:attribute-set>

然后在使用xsl:copyxsl:element或使用文字结果元素创建元素时使用它:

<xsl:copy use-attribute-sets="colouredText">
    ...
</xsl:copy>

<xsl:element name="fo:inline" use-attribute-sets="colouredText">
    ...
</xsl:element>

<fo:inline xsl:use-attribute-sets="colouredText">
    ...
</fo:inline>

如果输出要求发生变化(“除了红色,重要信息也必须是粗体” / “忘记颜色,只需将它们设为斜体” / “尝试使用Comic Sans”)您只需要调整属性集内的属性定义,而无需修改应用这些“样式”的模板。

xsl:use-attribute-sets属性的值是以空格分隔的列表属性集名称:

<xsl:attribute-set name="spacedText">
  <xsl:attribute name="space-before">12pt</xsl:attribute>
  <xsl:attribute name="space-after">12pt</xsl:attribute>
</xsl:attribute-set>

...

<fo:block xsl:use-attribute-sets="colouredText spacedText">
    This block is both coloured and spaced!
</fo:block>

属性集又可以引用其他集合:

<xsl:attribute-set name="colouredTitle" use-attribute-sets="colouredText">
    <xsl:attribute name="font-size">16pt</xsl:attribute>
    <xsl:attribute name="text-align">center</xsl:attribute>
</xsl:attribute-set>

XSL 1.0规范(或the corresponding section of the XSL 2.0 specifications)的链接部分提供了有关如何扩展和合并属性集的更多信息。

答案 1 :(得分:1)

使用attribute-set是值得探索的选项。另一个选择是做你开始做的事情:

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

<xsl:template match="/">
    <!--Probably declare the variable here-->
    <xsl:variable name="outputcolor" select="'red'" />
    <fo:root>
        <fo:layout-master-set>
            <fo:simple-page-master master-name="A4-portrait"
                page-height="29.7cm" page-width="21.0cm" margin="2cm">
                <fo:region-body />
            </fo:simple-page-master>
        </fo:layout-master-set>
        <fo:page-sequence master-reference="A4-portrait">
            <fo:flow flow-name="xsl-region-body" font-family="Helvetica"
                font-size="6pt">
                <fo:block font-size="8pt" text-indent="5pt">
                    <fo:inline font-weight="bold"><xsl:text>Application Summary</xsl:text></fo:inline>
                    <!--Here i would like to make red a variable that i could possibly changed -->
                    <fo:inline color="{$outputcolor}">
                        <xsl:value-of select="businessInfo/appSum" />
                    </fo:inline>
                </fo:block>
            </fo:flow>
        </fo:page-sequence>
    </fo:root>
</xsl:template>

</xsl:stylesheet>

请注意,在模板顶层声明变量仅将其范围限制为该模板。您可以声明全局变量(在样式表的顶层,在任何模板之外),以使它们在样式表中的任何位置可用。