tags:

views:

44

answers:

1

Hello,

I have a website coded with XML+XSLT that outputs me a full HTML website. Now, to make the site more dinamic I want to split some parts of the document: the header, footer and sidebar. I was looking at Google and I found this solution:

<xsl:param name="doc" select="document('menu.xml')"/>
<xsl:template match="/">
<html><head></head><body><xsl:for-each
select="$doc"><xsl:apply-templates/></xsl:for-each></body></html>
</xsl:template>

I was trying to apply it and I can get it working. This is the way I am using:

I changed the route to "../menu.xml" becasuse the xsl is inside a folder, this works well.

<?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:template match="/">

more valid and working code and then:

<ul class="menu_top">
    <xsl:for-each select="$menu">
        <li>
            <a>
                <xsl:attribute name="href">
                    #<xsl:value-of select="link" />
                </xsl:attribute>

                <xsl:value-of select="name"/>
            </a>
        </li>
    </xsl:for-each>
</ul>
<xsl:for-each select="$menu">
    <div class="submenu"> 
        <xsl:attribute name="id">
            <xsl:value-of select="link" />
        </xsl:attribute>

        <ul>
            <xsl:for-each select="child">
                <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>
</xsl:for-each>

Finally my menu.xml:

<?xml version="1.0" encoding="utf-8"?>
   <menu>
        <category>
            <name>First</name>
            <link>menu-1</link>

            <child>
                <name>Child 1</name>
                <link>#</link>
            </child>

            <child>
                <name>Child 2</name>
                <link>#</link>
            </child>          
        </category>
</menu>

I've more categories entries but I simplified it.

Thanks in advance!

+2  A: 

The document() function returns the root of the document you're importing. In this case, that's the menu element, not the category element. If you want to loop through the categories, use this instead:

<xsl:for-each select="$menu/menu/category">
   ...
Welbog
Thank you Welbog! This works fine. I tried to vote up but I've no enough reputation! Thanks another time! :)
Isern Palaus
@Isern Palaus: Also, there is no need for all those `for-each`. It is prefectly valid `apply-templates select="$doc"`. Check my XML/XSLT Client Side site at http://www.aranedabienesraices.com.ar
Alejandro
@Alejando i don't know how to use right the templates. :(
Isern Palaus
@Isern Palaus: Then ask a question with inputs sample and desired output.
Alejandro