如何在XSL样式表中的另一个标记内的标记中创建不同的字体?

时间:2016-07-26 17:16:08

标签: html xml xslt

如何在XSL中为这样的

应用不同的字体
<text> sometext <citation> somecitation </citation> sometext </text>

因此,引文内容应采用与文本不同的字体。

所以这是我的XML

的一部分

<text>她翻过床,在她的房间里发现了看不见的短吻鳄。 <citation>这里发生了什么?她要求<citation></text>

我写了一段代码<xsl:template match="text"> <p> <xsl:value-of select="text()"/> <q> <xsl:value-of select="citation/text()"/> </q> </p> (q代表CSS中的斜体) 我想得到什么: 她翻过床,在她的房间里发现了看不见的鳄鱼。 &#34;这里发生了什么?&#34; 她要求。

我现在得到的东西:她翻过床,在她的房间里发现了看不见的短吻鳄。她要求。 &#34;这里发生了什么?&#34;

如何才能获得正确的结果?

谢谢!

1 个答案:

答案 0 :(得分:1)

首先,浏览器应用的默认样式应该足够 - 只要您使用正确的HTML标记。

给出以下输入:

XML

<text>She flipped her bed over and found invisible alligators all over her room. <citation> What's going on here? </citation> she demanded. </text>

以下样式表:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/">
    <html>
        <body>
            <xsl:apply-templates/>
        </body>
    </html>
</xsl:template>

<xsl:template match="text">
    <p>
        <xsl:apply-templates/>
    </p>
</xsl:template>

<xsl:template match="citation">
    <cite>
        <xsl:apply-templates/>
    </cite>
</xsl:template>

</xsl:stylesheet>

将返回:

<html>
   <body>
      <p>She flipped her bed over and found invisible alligators all over her room. <cite> What's going on here? </cite> she demanded. 
      </p>
   </body>
</html>

大多数浏览器将呈现为:

enter image description here

如果您不喜欢或不信任浏览器默认值,您可以指定自己的样式,例如:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/">
    <html>
        <body>
            <xsl:apply-templates/>
        </body>
    </html>
</xsl:template>

<xsl:template match="text">
    <p>
        <xsl:apply-templates/>
    </p>
</xsl:template>

<xsl:template match="citation">
    <span style="font-style: italic;">
        <xsl:apply-templates/>
    </span>
</xsl:template>

</xsl:stylesheet>

或者:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/">
    <html>
        <head>
            <style>
                q {
                    font-style: italic;;
                }
            </style>
        </head>
        <body>
            <xsl:apply-templates/>
        </body>
    </html>
</xsl:template>

<xsl:template match="text">
    <p>
        <xsl:apply-templates/>
    </p>
</xsl:template>

<xsl:template match="citation">
    <q>
        <xsl:apply-templates/>
    </q>
</xsl:template>

</xsl:stylesheet>
相关问题