include header and footer in html created with xsl

2019-08-02 03:57发布

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 ?

标签: html xslt
2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-08-02 04:45

Is the XSL generating more than one HTML file? If so, you can definitely include a header and footer in each HTML file.

If you’re only generating one HTML document, there isn’t really the concept of a “page”, unless you mean the series of screens a user sees as they scroll down. In that case, you’d need to use CSS to make the header and footer stay visible.

Or is this HTML being used to generate a PDF? Please explain further.

查看更多
小情绪 Triste *
3楼-- · 2019-08-02 04:51

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>

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>

which imports another 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>

would result in:

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

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 :)

查看更多
登录 后发表回答