如何将多个xsl文件合并为一个

时间:2017-06-19 10:41:53

标签: xml html5 xslt

我正在尝试将头文件和导航文件添加到另一个xsl文件中。 header.xslnav.xsl,但不起作用。

主体文件

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

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

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

    <!-- <xsl:template match="/"> <xsl:apply-templates /> </xsl:template> <xsl:template 
        match="/response"> <HTML dir="ltr"> <xsl:apply-templates /> </HTML> </xsl:template> -->
    <xsl:template match="body">
<body>
<xsl:import href="nav.xsl" />
        <!-- content of other element -->
        <xsl:apply-templates select="footer" />
        </body>
    </xsl:template>
</xsl:stylesheet>

header.xsl

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

    <!-- header.xsl -->

    <xsl:output method="html" encoding="UTF-8" indent="no"
        media-type="text/html" doctype-system="about:legacy-compat" />

    <xsl:template match="header">
        <!-- content of header -->
        <HEAD>
            <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
            <meta http-equiv="X-UA-Compatible" content="IE=edge" />
            <meta name="viewport" content="width=device-width, initial-scale=1" />
            <meta name="description" content="..." />
            <meta name="author" content="..." />
            <meta name="robots" content="NOINDEX, NOFOLLOW" />
            <meta http-equiv="pragma" content="no-cache" />
            <meta http-equiv="expires" content="-1" />

            <!-- Bootstrap -->
            <link href="/assets/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet" />
            <link href="/assets/bootstrap/dist/css/bootstrap-theme.min.css"
                rel="stylesheet" />
            <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
            <script src="/assets/jquery/dist/jquery.js"></script>
            <!-- Include all compiled plugins (below), or include individual files 
                as needed -->
            <script src="/assets/bootstrap/dist/js/bootstrap.min.js"></script>

            <link rel="stylesheet" type="text/css" href="/stylesheets/simple-sidebar.css" />
            <link rel="stylesheet" type="text/css" href="/stylesheets/ccp.css" />
            <link rel="icon" type="image/png" href="/images/ccp.ico" />
            <!--[if IE]><link rel="shortcut icon" href="images/ccp.ico"/><![endif] --><!-- Internet Explorer -->
            <script language="javascript" SRC="/javascripts/ccp.js"></script>
            <title>
                <xsl:value-of select="/response/header/title" />
                - title
            </title>
        </HEAD>

    </xsl:template>

</xsl:stylesheet>

nav.xsl

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

    <!-- header.xsl -->

    <xsl:output method="html" encoding="UTF-8" indent="no"
        media-type="text/html" doctype-system="about:legacy-compat" />

    <xsl:template match="/">

        <div class="container-fluid ">
            <div class="logo">

            </div>

            <div class="row">
                <div class="header">
                    <div class="row-fluid">
                        <div class="col-xs-5 col-sm-7 col-md-9">
                            <h2 style="padding-left: 15px !important;">
                                <xsl:value-of select="/response/header/title" />
                            </h2>
                        </div>
                        <div class="col-xs-4 col-sm-3 col-md-2 vcenter" style="text-align: right;font-size: 8pt;">

                            <xsl:value-of select="/response/header/user" />

                            |
                            <a>
                                <xsl:attribute name="href">/logout.php?csrf=<xsl:value-of
                                    select="/response/header/token" /></xsl:attribute>
                                Logout
                                <img src="/images/page-layout/logout-button.gif" alt="Icon: Logout"
                                    title="Logout" />
                            </a>
                        </div>
                    </div>
                </div>


                <div class="row-fluid" style="clear: both;position: relative;top: -50px;margin-left: 95px;">
                    <div class="col-sm-8">
                        <xsl:value-of select="/response/header/description" />

                    </div>
                    <div class="col-sm-4 text-right">
                        <a href="/landing.php">

                            Connectivity Compliance Portal (CCP)
                            <span style="font-size:7pt">v3.0</span>

                        </a>
                    </div>
                </div>
            </div>
        </div>

        <!-- Sidebar -->
        <div id="sidebar-wrapper">

            <ul class="sidebar-nav">
                <xsl:for-each select="/response/header/nav/container">
                    <xsl:if test="title = admin">
                        <li class="sidebar-brand">
                            <a>
                                <xsl:attribute name="href"><xsl:value-of
                                    select="link" /></xsl:attribute>
                                <xsl:value-of select="title" />
                            </a>
                        </li>
                    </xsl:if>
                    <xsl:for-each select="sub_modules">

                        <li class="menu">
                            <a href="#">
                                <xsl:value-of select="title" />
                            </a>
                        </li>

                        <xsl:for-each select="menuitem">
                            <li>
                                <a>
                                    <xsl:attribute name="href"><xsl:value-of
                                        select="link" /></xsl:attribute>
                                    <xsl:value-of select="title" />
                                </a>
                            </li>
                        </xsl:for-each>

                    </xsl:for-each>
                </xsl:for-each>
            </ul>
        </div>
        <!-- /#sidebar-wrapper -->

    </xsl:template>

</xsl:stylesheet>

我看不出我在这里做错了什么。输出应为html5。

在firefox / IE / chrome中出错,firefox显示Error loading stylesheet: Parsing an XSLT stylesheet failed.

更新

我也尝试了以下内容,但也不起作用

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

    <xsl:include href="header.xsl" />
    <xsl:include href="nav.xsl" />

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

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

        <xsl:call-template name="header"/>

        <xsl:call-template name="nav"/>
        <!-- Page Content -->

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

    </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

您的初始XSLT失败,因为xsl:import必须是xsl:stylesheet的孩子。请注意,导入样式表实际上并不会导致该样式表中的任何模板在该点应用。

在第一种情况下,您需要做的是将nav.xsl的导入与导入header.xsl

放在同一位置
<xsl:import href="header.xsl" />
<xsl:import href="nav.xsl" />

您还需要修改nav.xsl以匹配body而不是/,从而查看您希望通过它实现的目标......

<强> nav.xsl

<xsl:template match="body">
    <div class="container-fluid ">
      ...
    </div>
</xsl:template>

然后,在您的主要XSLT中,您可以使用xsl:apply-imports之前已将xsl:import用于&#34; nav.xsl&#34;

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

这是因为导入的模板的优先级低于主XSLT中的模板,因此主模板首先匹配。

第二次尝试时,您使用的是xsl:call-templates。只有在包含样式表中指定了模板时,这才有效。 xsl:call-templates定位模板名称,而不是实际文件的名称。

所以,&#34; header.xsl&#34;您可以使用命名模板替换<xsl:template match="header">

 <xsl:template name="header">

而且,对于nav.xsl,您将拥有<xsl:template name="nav">