【问题标题】:How can I add header and footer XSLT files to another XSLT file to convert it into HTML?如何将页眉和页脚 XSLT 文件添加到另一个 XSLT 文件以将其转换为 HTML?
【发布时间】:2017-09-21 05:17:01
【问题描述】:

我正在编写一个XSLT 程序,它将XML 转换为HTML 格式。我想在XSLT 中添加页眉和页脚。这意味着我正在考虑在我的content.xslt 中添加header.xsltfooter.xslt

这是我的代码: XML 文件 dataDetail.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="layout.xslt" type="text/xsl"?>
 <customerList>
    <customer id="101">
        <name>Jon</name>
        <email>Jon.sood@iqbsys.com</email>
        <age>23</age>
        <gender>male</gender>
    </customer>
</customerList>

这是我的主要 xslt 文件,即 layout.xslt

    <?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:import href="content.xslt" />
    <xsl:template match="/">
        <xsl:apply-imports />
    </xsl:template>
</xsl:stylesheet>

这是我的 content.xslt 文件:

 <?xml version="1.0"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <html>
            <body>
                <h1>Header content</h1>
                <h2>=======start content==========</h2>
                <h2>The customer details are: </h2>
                <table border="1">
                    <tr bgcolor="#9acd32">
                        <th>Name</th>
                        <th>Email</th>
                        <th>Gender</th>
                    </tr>
                    <xsl:for-each select="customerList/customer">
                        <tr>
                            <td>
                                <xsl:value-of select="name" />
                            </td>
                            <td>
                                <xsl:value-of select="email" />
                            </td>
                            <td>
                                <xsl:value-of select="gender" />
                            </td>
                        </tr>
                    </xsl:for-each>
                </table>

                <h2>=======End content==========</h2>
                <h1>Footer content</h1>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

这个输出是:

我想显示来自两个不同XSLT 文件的页眉和页脚的内容,即footer.xsltheader.xslt

如何将这两个 xslt 文件包含到 content.xslt 中?我已尝试导入,但没有得到正确的结果。

谢谢:-)

【问题讨论】:

    标签: html xml xslt xslt-1.0


    【解决方案1】:

    如何将这两个 xslt 文件包含到 content.xslt 中?我尝试过导入,但没有得到正确的结果。

    您有两个主要问题需要解决。

    首先,您需要将在单独的页眉和页脚样式表中定义的转换呈现在主转换中。您没有显示任何这样做的机制,但这并不难:您需要在主模板的适当位置插入 apply-templates 元素。对页眉和页脚模板使用非默认模式可能很有用。例如,

    content.xslt

    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:template match="/">
            <html>
                <body>
    
                    <!-- THIS: -->
                    <xsl:apply-templates select="." mode="header" />
    
                    <h2>=======start content==========</h2>
                    <h2>The customer details are: </h2>
                    <!-- ... -->
                </body>
            </html>
        </xsl:template>
    </xsl:stylesheet>
    

    其次,您需要将外部文件中定义的模板合并到主文件中定义的样式表中。这就是 XSL importinclude 元素的用途,但既然您已经在使用 import 我想我不需要详细说明。因此,看起来这可能适合您的方案:

    layout.xslt

    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:import href="content.xslt" />
        <xsl:import href="header.xslt" />
        <xsl:import href="footer.xslt" />
    </xsl:stylesheet>
    

    请注意,此样式表中不需要模板;依赖导入的样式表来提供所有需要。然后,您可以像这样编写标题样式表,例如:

    header.xslt

    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
        <!-- note that this mode matches the one selected by the apply-templates
             in content.xslt: -->
        <xsl:template match="/" mode="header">
            <h1>Header Content</h1>
        </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

    • 这个解决方案没有任何问题,而且非常干净易懂。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-22
    • 1970-01-01
    相关资源
    最近更新 更多