xml - 组合 <xsl :param> and <xsl:include>

标签 xml xslt xhtml

我试图通过将网站分成几部分来使网站更加动态。我的第一页有一个 XML 文件,现在已转换为 4 个文件:index.xml、menu.xml、sidebar.xml 和 footer.xml。

(已更新)

我在index.xsl 文件中正确包含了XML。现在我需要包含他们将使用的 .xls。实际上,我将所有内容都放在同一个文件中并且工作正常,因此 XML 包含问题已解决。

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name="menu" select="document('../menu.xml')"/>
    <xsl:param name="sidebar" select="document('../sidebar.xml')"/>
    <xsl:param name="footer" select="document('../footer.xml')"/>

    <xsl:template match="/">
        <!-- Split header.xsl -->
        <html lang="es">
            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
                <title><xsl:value-of select="page/title" /></title>

                <link rel="stylesheet" type="text/css" href="css/main.css" /> 
                <script type="text/javascript" src="js/custom.js"></script> 
            </head>

            <body>
                <div class="content">
                    <div class="header">
                        <div id="tabs" class="menu">
                            <ul>
                                <xsl:for-each select="$menu/menu/category">
                                    <li><a><xsl:attribute name="href"><xsl:value-of select="link" /></xsl:attribute><xsl:value-of select="name"/></a></li>
                                </xsl:for-each>
                            </ul>
                        </div>
                    </div>                   

                    <div class="body">

                    <!-- End Split header.xsl -->

                        <div class="body_izqda">
                            <xsl:for-each select="page/news/contents/entry">
                                <h2><xsl:value-of select="title" /></h2>
                                <p><xsl:value-of select="text" /></p>
                            </xsl:for-each>
                        </div>

                        <!-- Split sidebar.xml -->
                        <div class="body_dcha">
                            <ul>
                                <xsl:for-each select="$sidebar/sidebar/results/category">
                                    <li>
                                        <a><xsl:attribute name="href"><xsl:value-of select="link" /></xsl:attribute><xsl:value-of select="name" /></a>
                                    </li>
                                </xsl:for-each>
                            </ul>
                        </div>
                        <!-- End Split sidebar.xml -->

                        <!-- Split footer.xml -->
                        <div class="clear">
                        </div>
                    </div>

                    <div class="footer">
                        <ul>
                            <xsl:for-each select="$footer/footer/entry">
                                <li><a><xsl:attribute name="href"><xsl:value-of select="link" /></xsl:attribute><xsl:value-of select="title" /></a></li>
                            </xsl:for-each>
                        </ul>
                    </div>
                </div>
            </body>
        </html>
        <!-- End Split footer.xml -->
    </xsl:template>
</xsl:stylesheet>

顺便说一句,我想将 XLST 部分与 XLS 文件分开。我尝试使用 <xsl:include>但我无法让它与参数 $menu 一起工作。

我在需要分割文档的地方标记了SplitEnd Split

我已经尝试用@svick 的第一个回复来解决,但是将它与我​​为 PHP 完成的 XSLTPRocessor 类所做的标记分开给了我:

Warning: XSLTProcessor::importStylesheet() [xsltprocessor.importstylesheet]: element import only allowed as child of stylesheet

所以,我的做法是先拆分然后再包含它,这是有问题的。

如何解决?

提前致谢!

注1 头.xsl:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

            <title><xsl:value-of select="page/title" /></title>

            <link rel="stylesheet" type="text/css" href="css/reset-min.css" />
            <link rel="stylesheet" type="text/css" href="css/main.css" />
            <link rel="stylesheet" type="text/css" href="css/jquery-ui.css" />
        </head>
    </xsl:template>
</xsl:stylesheet>

最佳答案

组合<xsl:import><xsl:param>似乎对我来说效果很好:

main.xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:param name="title" select="'Page title'" />
  <xsl:include href="head.xsl"/>
</xsl:stylesheet>

head.xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="head">
    <title>
      <xsl:value-of select="$title"/>
    </title>
  </xsl:template>
</xsl:stylesheet>

在包含 <head /> 的文件上应用 main.xsl产生<title>Page title</title>正如预期的那样。如果这对您没有帮助,您应该在实际使用 <xsl:import> 的地方发布一些代码和<xsl:param> (这不起作用)。

关于xml - 组合 <xsl :param> and <xsl:include>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3381750/

相关文章:

spring - 为什么我的 JSF + Spring Web 应用程序输出 JSF 源代码而不是解释的 HTML 页面?

python - 使用 python 获取 youtube 数据

android - 重复位图在 View 的 state_pressed 上渲染不佳

c# - 将斜杠添加到自关闭标签

xslt - 如何获取 XSLT 中的元素名称

xslt - 如何获取XSLT文件中的当前文件夹路径?

html - "<html xmlns="http ://www. w3.org/1999/xhtml">"是做什么的?

java - 通用集合和 XStream

xml - 如何使用带有属性值的 xslt 作为选择器?

html - 字段集验证。字段集位于表单内部,并在外部有输入