繁体   English   中英

结合 <xsl:param> 和 <xsl:include>

[英]Combining <xsl:param> and <xsl:include>

我试图通过将网站分成多个部分来使网站更具特色。 我的第一页有一个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>

顺便说一句,我想用XLS文件拆分该XLST部分。 我尝试使用<xsl:include>但是无法使用参数$ menu使用它。

我已经用拆分结束拆分标记了,在这里我需要拆分文档

我已经尝试用@svick的第一个答复来解决,但是用我为PHP完成的XSLTPRocessor类标记将其拆分为:

警告:XSLTProcessor :: importStylesheet()[xsltprocessor.importstylesheet]:仅允许将元素导入作为样式表的子级

所以,拆分我正在做的事情然后包含它是有问题的。

我该如何解决?

提前致谢!

注1 head.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>地方发布一些代码(这不起作用)。

所以,拆分我正在做的事情然后包含它是有问题的。 我该如何解决?

是的,出了点问题。 您想分割砖块模板...

首先,您需要拆分一些东西,因此此样式表:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dummy="dummy"
exclude-result-prefixes="dummy">
    <xsl:param name="menu" select="document('menu.xml')"/>
    <xsl:param name="sidebar" select="document('sidebar.xml')"/>
    <xsl:param name="footer" select="document('footer.xml')"/>
    <dummy:attSet>
        <footer class="footer"/>
        <menu class="menu" id="tabs"/>
        <sidebar class="body_dcha"/>
    </dummy:attSet>
    <xsl:template match="/page">
        <html lang="es">
            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
                <title>
                    <xsl:value-of select="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">
                        <xsl:apply-templates select="$menu"/>
                    </div>
                    <div class="body">
                        <xsl:apply-templates select="news/contents"/>
                        <xsl:apply-templates select="$sidebar"/>
                        <div class="clear"></div>
                    </div>
                    <xsl:apply-templates select="$footer"/>
                </div>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="page/news/contents">
        <div class="body_izqda">
            <xsl:apply-templates/>
        </div>
    </xsl:template>
    <xsl:template match="contents/entry/title">
        <h2>
            <xsl:value-of select="."/>
        </h2>
    </xsl:template>
    <xsl:template match="contents/entry/text">
        <p>
            <xsl:value-of select="."/>
        </p>
    </xsl:template>
    <xsl:template match="menu/category|sidebar/results/category|footer/entry">
        <li>
            <a href="{link}">
                <xsl:value-of select="name"/>
            </a>
        </li>
    </xsl:template>
    <xsl:template match="/footer|/menu|/sidebar">
        <div>
            <xsl:copy-of select="document('')/*/dummy:*/*[name()=name(current())]/@*"/>
            <ul>
                <xsl:apply-templates/>
            </ul>
        </div>
    </xsl:template>
</xsl:stylesheet>

使用此输入:

<page>
    <title>Some Page</title>
    <news>
        <contents>
            <entry>
                <title>Title1</title>
                <text>Text1</text>
            </entry>
            <entry>
                <title>Title2</title>
                <text>Text2</text>
            </entry>
            <entry>
                <title>Title3</title>
                <text>Text3</text>
            </entry>
            <entry>
                <title>Title4</title>
                <text>Text4</text>
            </entry>
        </contents>
    </news>
</page>

并且此文件:

menu.xml文件

<menu>
    <category>
        <link>http://www.example.com/link1</link>
        <name>Link1</name>
    </category>
    <category>
        <link>http://www.example.com/link2</link>
        <name>Link2</name>
    </category>
    <category>
        <link>http://www.example.com/link3</link>
        <name>Link3</name>
    </category>
</menu>

sidebar.xml

<sidebar>
    <results>
        <category>
            <link>http://www.example.com/link4</link>
            <name>Link4</name>
        </category>
        <category>
            <link>http://www.example.com/link5</link>
            <name>Link5</name>
        </category>
        <category>
            <link>http://www.example.com/link6</link>
            <name>Link6</name>
        </category>
    </results>
</sidebar>

footer.xml

<footer>
    <entry>
        <link>http://www.example.com/link7</link>
        <name>Link7</name>
    </entry>
    <entry>
        <link>http://www.example.com/link8</link>
        <name>Link8</name>
    </entry>
    <entry>
        <link>http://www.example.com/link9</link>
        <name>Link9</name>
    </entry>
</footer>

输出与样式表相同的结果。

因此,现在您可以将样式表分为几个模块。

我懂了。 问题是每个<xsl:include> d文件必须是有效的XSLT,这意味着它必须是有效的XML。 而且,有效的XML中不能包含未关闭的标签,但是例如,您需要在head.xsl中未打开的<html> 因此,我认为您无法在XSLT中做到这一点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM