xslt - 如何将一个 XSL 文件包含到另一个文件中?

标签 xslt xslt-2.0

我使用 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:includexsl: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:includexsl:import元素只允许作为顶级元素。

关于xslt - 如何将一个 XSL 文件包含到另一个文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28888360/

相关文章:

xml - XSLT 将 2 个 XML 文件作为输入并生成输出 XML 文件

xslt - 如何获取 XSLT 中字符串的最后一个字符以便将其添加到 HTML 输出中?

javascript - 在 XSLT 中使用变量应用模板

xslt - XSL : Ignoring/stripping namespaces in secondary documents

javascript - 加载 xsl 时出错 "invalid xpath expression"

xml - 使用 XSLT 2.0 读取文本文件时如何添加父元素?

java - 将不同的 xml 转换为另一个 xml

xml - 使用 xslt 将节点从 XML 插入到另一个 XML 文件

用于元素集合的 XSLT for-each 循环

xslt - 如何在 XSLT 的输出中包含通过 xpath 参数选择的节点的祖先分支