如何将页眉和页脚XSLT文件添加到另一个XSLT文件以将其转换为HTML? [英] How can I add header and footer XSLT files to another XSLT file to convert it into HTML?

查看:167
本文介绍了如何将页眉和页脚XSLT文件添加到另一个XSLT文件以将其转换为HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个 XSLT 程序,它将 XML 转换为 HTML code>格式。我想添加页眉和页脚到 XSLT 。这意味着我正在考虑在内容中添加 header.xslt footer.xslt .xslt



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

 <?xml version =1.0encoding =UTF-8?> 
<?xml-stylesheet href =layout.xslttype =text / xsl?>
< customerList>
< customer id =101>
< name> Jon< / name>
< email> Jon.sood@iqbsys.com< / email>
<年龄> 23< / age>
<性>男性< /性别>
< / customer>
< / customerList>

这是我的主要 xslt strong> layout.xslt

 <?xml version =1.0encoding =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>标头内容< / h1>
< h2> =======开始内容==========< / h2>
< h2>客户详细信息是:< / h2>
< table border =1>
< tr bgcolor =#9acd32>
< th>名称< / th>
< th>电子邮件< / th>
< 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> =======结束内容==========< / h2>
< h1>页脚内容< / h1>
< / body>
< / html>
< / xsl:template>
< / xsl:stylesheet>

输出结果如下:



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



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



感谢:-)

解决方案


将这两个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>

<! - 这个: - >

< h2> =======开始内容==========< / h2>
< h2>客户详细信息是:< / h2>
<! - ... - >
< / body>
< / html>
< / xsl:template>
< / xsl:stylesheet>

其次,您需要将外部文件中定义的模板合并到样式表定义在你的主文件中。这就是XSL import include 元素的用途,但由于您已经在使用 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>

请注意,此样式表中不需要模板;导入的样式表正在被用来提供所有需要的。您可以像这样编写标题样式表,例如:
$ b header.xslt

 < xsl:stylesheet version =1.0
xmlns:xsl =http://www.w3.org/1999/XSL/Transform>

<! - 请注意,此模式与content.xslt中的apply-templates
选择的模式相匹配: - >
< xsl:template match =/mode =header>
< h1>标头内容< / h1>
< / xsl:template>

< / xsl:stylesheet>


I am coding a XSLT program, which will convert XML to HTML format. I want to add header and footer to XSLT. It mean that I am thinking to add header.xslt and footer.xslt in my content.xslt.

Here is my code: XML file 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>

Here is my main xslt file ie 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>

Here is my content.xslt file:

 <?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>

The output of this is :

I want to display the content of Header and footer from two different XSLT files ie footer.xslt and header.xslt.

How can I include these two xslt files to content.xslt ? I have tried import but not getting the correct results.

Thanks :-)

解决方案

How can I include these two xslt files to content.xslt ? I have tried import but not getting the correct results.

You have two main issues to tackle.

In the first place, you need the transformations defined in the separate header and footer stylesheets to be presented inside the main transformation. You don't show any mechanism for doing that, but it's not hard: you need to insert apply-templates elements at the appropriate places in the main template. It may be useful to use non-default modes for the header and footer templates. For example,

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>

In the second place, you need to incorporate the template(s) defined in your external files into the stylesheet defined in your main file. This is what the XSL import and include elements are for, but since you're already using import I guess I don't need to go into detail. Thus, it looks like this might fit into your scheme:

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>

Note that no template is needed in this stylesheet; the imported stylesheets are being relied upon to provide all needed. You might then write the header stylesheet like this, for example:

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>

这篇关于如何将页眉和页脚XSLT文件添加到另一个XSLT文件以将其转换为HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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