【问题标题】:Combining <xsl:param> and <xsl:include>结合 <xsl:param> 和 <xsl:include>
【发布时间】:2011-03-23 20:15:33
【问题描述】:

我正在尝试通过将网站分成几部分来使网站更具动态性。我有一个用于第一页的 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 部分。我尝试使用 &lt;xsl:include&gt;,但无法使用参数 $menu。

我用 SplitEnd Split 标记了我需要拆分文档的地方

我已经尝试通过@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>

【问题讨论】:

  • 你能澄清一下吗?你到底想怎么分割?
  • @svick 你好。我有一个将网站从 输出到 的 XSL。我想将 拆分为 。这其中的内容,将在一个单独的 XLS 上为每一个。我不知道这是否更清楚。对不起,英语不是我的主要语言。如果您需要更多解释,请告诉我。提前致谢!

标签: xml xslt xhtml


【解决方案1】:

结合 &lt;xsl:import&gt;&lt;xsl:param&gt; 似乎对我来说很好:

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>

对包含 &lt;head /&gt; 的文件应用 main.xsl 会按预期生成 &lt;title&gt;Page title&lt;/title&gt;。如果这对您没有帮助,您应该在实际使用 &lt;xsl:import&gt;&lt;xsl:param&gt; 的地方发布一些代码(这不起作用)。

【讨论】:

  • 嗨@svick,我已经用更具体的代码更新了这个问题,说明我在做什么。我试着按照你说的去做,但我得到了问题中提到的错误。提前谢谢!
【解决方案2】:

所以,拆分有问题 以我正在做的方式然后 包括它。我该如何解决?

是的,出了点问题。您想拆分 brick 模板...

首先,你需要有一些东西可以拆分,所以这个样式表:

<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 文件中,并使用&lt;xsl:include href="sidebar.xsl" /&gt; 加载它。谢谢!
  • @Isern Palaus:不客气!另外,我不建议拆分此样式表,因为相同的模板适用于来自不同来源的元素。如果您将模式拆分以便按源对它们进行分组,那么您最终会重复模板或在主样式表模型中调用模板。
【解决方案3】:

我明白了。问题是每个&lt;xsl:include&gt;d 文件都必须是有效的XSLT,这意味着它必须是有效的XML。并且您不能在有效的 XML 中有未关闭的标签,但您需要例如在 head.xsl 中未关闭的&lt;html&gt;。所以,我认为你不能在 XSLT 中做到这一点。

【讨论】:

  • 我还尝试了&lt;xsl:include href="head.xsl" /&gt;,其中包含来自问题的 NOTE1 的代码,但我无法让它工作。我收到此错误:XSLTProcessor::importStylesheet() [xsltprocessor.importstylesheet]: 编译错误:文件 /home/xsl/index.xsl 第 9 行元素包含在第 9 行的 /home/index.php XSLTProcessor::importStylesheet() [ xsltprocessor.importstylesheet]: 元素仅允许在第 9 行的 /home/index.php 中作为样式表的子元素包含
  • 对不起'垃圾邮件'......但我看到它需要是&lt;xsl:stylesheet /&gt;的直接孩子......所以很难适应它:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-30
相关资源
最近更新 更多