我使用 XSLT 2.0 版,我想包含一个头部模板 head.xsl
在另一个文件中 home.xsl
.
主页.xsl :
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css"/>
<title/>
</head>
<body>
<div id="main">
<div id="content">
<xsl:call-template name="home-in"/>
</div>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
头.xsl:
xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css"/>
<title/>
</head>
<body>
<div id="container">
<div id="header">
<div id="menu">
<ul>
<li><a href="#" class="active">Home</a></li>
<li><a href="#">about</a></li>
</ul>
</div>
</div>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
如何包含我的
head.xsl
文件到 home.xsl
?
最佳答案
您可以使用 <xsl:include>
包含另一个文件
您的 home.xsl
文件看起来像这样
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="html" indent="yes"/>
<xsl:include href="head.xsl"/>
<xsl:template match="/">
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css"/>
<title/>
</head>
<body>
<div id="main">
<div id="content">
<xsl:call-template name="home-in"/>
</div>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
更多详情
XSLT 处理器将简单地用
href
中命名的样式表的内容替换指令。属性。请注意,包含的样式表模板将与包含的样式表具有相同的默认优先级和导入优先级。不要混淆
xsl:include
与 xsl:import
,除了导入样式表中的指令可以被导入样式表和任何包含的样式表中的指令覆盖之外,它是相似的。换句话说,导入样式表中元素的导入优先级始终小于导入样式表的导入优先级。由于使用
xsl:include
与复制文件中的代码相同,请确保在使用命名模板时没有重复的模板名称。这里贴来自
w3 documentation
Including a stylesheet multiple times can cause errors because of duplicate definitions. Such multiple inclusions are less obvious when they are indirect. For example, if stylesheet B includes stylesheet A, stylesheet C includes stylesheet A, and stylesheet D includes both stylesheet B and stylesheet C, then A will be included indirectly by D twice. If all of B, C and D are used as independent stylesheets, then the error can be avoided by separating everything in B other than the inclusion of A into a separate stylesheet B' and changing B to contain just inclusions of B' and A, similarly for C, and then changing D to include A, B', C'.
请注意,
xsl:include
和 xsl:import
元素只允许作为顶级元素。
关于xslt - 如何将一个 XSL 文件包含到另一个文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28888360/