在使用 xslt 转换创建的 html 中包含页眉和页脚 [英] include header and footer in html created with xslt transformation

查看:28
本文介绍了在使用 xslt 转换创建的 html 中包含页眉和页脚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用在 xml 内容上运行的 XSLT 转换来创建 HTML.xml 内容是动态的,因此生成的 HTML 可以分布在多个页面上.我想在 HTML 的每个输出页面中包含一个页眉和页脚.有没有办法做这个 XSLT 转换或 HTML 技巧?

I'm creating an HTML using XSLT transformation run on xml content. The xml content is dynamic hence the resultant HTML can spread over more than one page. I would like to include a header and footer in each output page of HTML. Is there a way to do this XSLT transformation or HTML tricks ?

推荐答案

假设我有一个这样的 XML 结构:

Let's say I had an XML structure like this:

<?xml version="1.0" encoding="UTF-8"?>
<documentElement>
    <header/>
    <body>
        <!-- omitted for brevity -->
    </body>
</documentElement>

和一个样式表来转换上述 XML:

and a stylesheet to transform the above XML:

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

    <xsl:import href="imported.xsl"/>

    <xsl:output
        method="html"
        encoding="UTF-8"
        omit-xml-declaration="yes"
        doctype-system="about:blank"
        indent="no"
        media-type="text/html"
    />

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

    <xsl:template match="documentElement">
        <HTML dir="ltr">
            <xsl:apply-templates/>
        </HTML>
    </xsl:template>

    <xsl:template match="body">
        <BODY>
            <!-- content of other element -->
            <xsl:apply-templates select="footer"/>
        </BODY>
    </xsl:template>

</xsl:stylesheet>

导入另一个样式表:

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

    <!-- imported.xsl -->

    <xsl:output
        method="html"
        encoding="UTF-8"
        indent="no"
        media-type="text/html"
    />

    <xsl:template match="header">
        <!-- content of header -->
        <HEAD>
            <META charset="UTF-8"/>
        </HEAD>
    </xsl:template>

    <xsl:template name="footer">
        <FOOTER>
            <!-- content of footer -->
        </FOOTER>
    </xsl:template>

</xsl:stylesheet>

会导致:

<!DOCTYPE HTML>
<HTML dir="ltr">
    <HEAD>
        <META charset="UTF-8">
    </HEAD>
    <BODY>
        <!-- content of other element -->
        <FOOTER>
            <!-- content of footer -->
        </FOOTER>
    </BODY>
</HTML>

注意文档有

元素但没有
元素以及样式表如何转换他们不同;例如,<xsl:template ma​​tch="header"/>与 <xsl:template name="footer"/>!

Notice the document has the <header/> element but not the <footer/> element and how the stylesheet(s) transform(s) them differently; e.g, <xsl:template match="header"/> versus <xsl:template name="footer"/>!

我不知道这是否能消除您的疑虑;如果您需要深入解释,请随时告诉我:)

I don't know if this clears your doubt; in case you need in-depth explanation, do not hesitate to let me know :)

这篇关于在使用 xslt 转换创建的 html 中包含页眉和页脚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆