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">
<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!
The
document()function returns the root of the document you’re importing. In this case, that’s themenuelement, not thecategoryelement. If you want to loop through the categories, use this instead: