tags:

views:

88

answers:

3

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"&gt;
    <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>

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>
+1  A: 

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

svick
hi @svick, I've updated the question with a more specific code about what I'm doing. I tried to do what you said but I get the error mentioned on the question.Thank you in advance!
Isern Palaus
A: 

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.

svick
I also tried `<xsl:include href="head.xsl" />` that contains the code from _NOTE1_ from question, and I can't get it working to. I get this error:XSLTProcessor::importStylesheet() [xsltprocessor.importstylesheet]: compilation error: file /home/xsl/index.xsl line 9 element include in /home/index.php on line 9XSLTProcessor::importStylesheet() [xsltprocessor.importstylesheet]: element include only allowed as child of stylesheet in /home/index.php on line 9
Isern Palaus
Sorry for 'spamming'... but I see that it needs to be direct child of `<xsl:stylesheet />`... so dificult to fit it :(
Isern Palaus
+1  A: 

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&lt;/link&gt;
        <name>Link1</name>
    </category>
    <category>
        <link>http://www.example.com/link2&lt;/link&gt;
        <name>Link2</name>
    </category>
    <category>
        <link>http://www.example.com/link3&lt;/link&gt;
        <name>Link3</name>
    </category>
</menu>

sidebar.xml

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

and footer.xml

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

Output the same result that provided stylesheet.

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

Alejandro
And then, with the templates, I can put it them into a separated .xsl file and load it with `<xsl:include href="sidebar.xsl" />`. Thank you!
Isern Palaus
@Isern Palaus: You're wellcome! Also, I would not recommend to split this stylesheet because the same template is apply for elements from diferents source. If you'll split the patterns in order to group them by the source, then you'd end up repeating the templates or calling a template in main stylesheet modelu for example.
Alejandro