【问题标题】:Format italic/bold with XSLT to HTML使用 XSLT 将斜体/粗体格式化为 HTML
【发布时间】:2014-07-09 20:26:44
【问题描述】:

我有一个这样的 XML 文档:

<bibliography>
    <element1>
        <text>
            Some text and <italic>italic Text</italic> and <bold>bold text</bold>
        </text>
    </element1>
    <element2>
        <text>
            Some text and <italic>italic Text</italic> and <bold>bold text</bold>
        </text>
    </element2>
</bibliography>

此 XSL 有效,但无法格式化 &lt;italic&gt;&lt;bold&gt; 标记。

<xsl:template match="/">
        <html>
            <head>
                <title>Bibliographie</title>
                <style type="text/css">
                .entry {
                    font-family: Georgia
                }
            </style>
            </head>
            <body>
                <xsl:apply-templates/>
        </body>
    </html>
</xsl:template>

<xsl:template match="/bibliography/*">
    <p>
        <div class="entry{@type}">
    [<xsl:number count="*"/>]
    <xsl:apply-templates/>
        </div>
    </p>
</xsl:template>

我必须添加什么才能让它格式化 &lt;italic&gt;&lt;bold&gt; 标记以适合 HTML? 我尝试使用 XSL-FO,但似乎无法将对象导出为 HTML,只能导出为 PDF。

【问题讨论】:

    标签: html xml xslt


    【解决方案1】:

    您问过一个关于输出 xsl-fo 的类似问题。 HTML 的原理是相同的,只是输出 HTML 标签而不是 xsl-fo 的标签。

    XSLT 不起作用的主要问题是因为您没有匹配 bolditalic

    的模板

    试试这个 XSLT

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="html" indent="yes"/>
    
        <xsl:template match="bibliography">
            <html>
               <head>
                   <title>Bibliographie</title>
                   <style type="text/css">
                   .entry {
                       font-family: Georgia
                   }
               </style>
               </head>
                <body>
                    <xsl:apply-templates />
                </body>
            </html>
        </xsl:template>
    
        <xsl:template match="bibliography/*">
           <div class="entry{@type}">
              [<xsl:number count="*"/>]
              <xsl:apply-templates/>
           </div>
        </xsl:template>
    
        <xsl:template match="bibliography/*/*" priority="0">
           <p>
               <xsl:apply-templates/>
           </p>
        </xsl:template>
    
        <xsl:template match="text">
            <xsl:apply-templates/>
        </xsl:template>
    
        <xsl:template match="bold">
            <span style="font-weight:bold;">
                <xsl:apply-templates/>
            </span>  
        </xsl:template>
    
        <xsl:template match="italic">
            <span style="font-style:italic;">
                <xsl:apply-templates />
            </span>  
        </xsl:template>
    </xsl:stylesheet>
    

    不在其中一个模板上使用“优先级”

     <xsl:template match="bibliography/*/*" priority="0">
    

    这充当了一种“包罗万象”的模板,用于匹配您没有特定模板的元素。例如,需要优先级以确保它不会在匹配“斜体”和“粗体”的模板之前应用。也就是说,如果您想要以特定方式格式化其他元素,例如“作者”,只需为它们添加特定模板。

    【讨论】:

    • 不知何故这对我不起作用。这是我的 XML 文件:link 和我的 XSLT:link 似乎它生成的 HTML 是正确的,但格式很糟糕。这是我的浏览器 (firefox) 中的输出:link
    • 我忘了说有几个元素可以包含&lt;italic&lt;bold&gt; 标签。非常遗憾。我需要更改什么,以使用&lt;italic&lt;bold&gt; 标签给出包含以下所有元素的段落中的每个元素?
    • 由于使用了xsl:apply-templates,它仍然应该选择italicbold。但是,我已经对我的问题进行了编辑,以允许输出内容更加灵活。我还注意到我已将其设置为输出方法“xml”而不是“html”。
    猜你喜欢
    • 2016-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-25
    • 1970-01-01
    • 2011-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多