2017-06-19 147 views
0

我想將一個頭文件和導航文件添加到另一個xsl文件中。 header.xslnav.xsl但它不起作用。如何將多個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 /鉻它的錯誤,火狐顯示Error loading stylesheet: Parsing an XSLT stylesheet failed.

UPDATE

我曾嘗試以下爲好,但不也是工作

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

您最初的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爲「nav.xsl」

<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以模板名稱爲目標,而不是實際文件的名稱。

所以, 「header.xsl」 你要做一個命名模板,而不是

<xsl:template name="header"> 

更換<xsl:template match="header">而且,對於nav.xsl你會<xsl:template name="nav">

相關問題