开发者

Combining <xsl:param> and <xsl:include>

I'm trying to make a website more dinamically with splitting it in parts. I had a XML file for the first page that now has been converted to 4 files: index.xml, menu.xml, sidebar.xml and footer.xml.

(Updated)

I include correctly the XML's on the index.xsl file. Now I need to include the .xls that they will use. Actually I've it all in the same file and works fine, so XML include are solved.

<?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">
    开发者_StackOverflow社区                        <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>

By the way, I want to split that XLST parts with XLS files. I tried with the <xsl:include> but I can't get it working with the param $menu.

I've marqued with Split and End Split where I need to split the document

I already tried to solve with the first reply by @svick, but splitting it with the marks I've done the XSLTPRocessor class for PHP gives me:

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

So, something is wrong with splitting in the way I'm doing and then including it.

How can I solve it?

Thanks in advance!

NOTE1 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>


Combining <xsl:import> and <xsl:param> seems to work fine for me:

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>

Applying main.xsl on a file containing <head /> produces <title>Page title</title> as expected. If this doesn't help you, you should post some code where you actually use <xsl:import> and <xsl:param> (and that doesn't work).


So, something is wrong with splitting in the way I'm doing and then including it. How can I solve it?

Yes, something is wrong. You want to split a brick template...

First, you need to have something to split, so this stylesheet:

<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>

With this input:

<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>

And this documents:

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>

and 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>

Output the same result that provided stylesheet.

So, now you can split the stylesheet in several modules.


I see. The problem is that each <xsl:include>d file has to be valid XSLT, which means it has to be valid XML. And you can't have unclosed tags in valid XML, but you need for example unclosed <html> in head.xsl. So, I don't think you can do this in XSLT.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜