XSLT菜单生成问题
这是我调用来生成菜单的模板,它有点破坏。使用 Xalan 时,我遇到堆大小错误,所以我猜测其中的某些内容严重损坏且不干净。
我为网站树的当前部分预设了模板,为其提供从网站根部开始的路径、语言、当前深度 (pi) 和过滤器。 >
这应该做的是选择当前级别上的所有内容,然后继续进行下一次迭代,这应该像再次调用时更新“nextLevel”一样简单,用于下一级菜单...
帮助?
编辑:据我所知,只要我不进行递归,它就可以工作。我得到了由我的 depht 参数指定的第一个级别,仅此而已。
该网站的组织方式如下:
<?xml version="1.0"?>
<site xsi:noNamespaceSchemaLocation="onepagev2.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<page pagename="Alpha">
<language langname="en" path="./en/Alpha" dephtCount="3">
<title>Alpha Anglo</title>
<content>Oh noes.</content>
</language>
<language langname="fr" path="./fr/Alpha" dephtCount="3">
<title>Alpha Franco</title>
<content>Oh non.</content>
</language>
</page>
<page pagename="Beta">
<language langname="en" path="./en/Beta" dephtCount="3">
<title>Beta Anglo</title>
<content>Content B is content B is content B!</content>
</language>
<language langname="fr" path="./fr/Beta" dephtCount="3">
<title>Beta Franco</title>
<content>Contenu B est contenu B est contenu B!</content>
</language>
</page>
<page pagename="Beta2">
<language langname="en" path="./en/Beta/Beta2" dephtCount="4">
<title>Beta Anglo</title>
<content>Content B is content B is content B!</content>
</language>
<language langname="fr" path="./fr/Beta/Beta2" dephtCount="4">
<title>Beta Franco</title>
<content>Contenu B est contenu B est contenu B!</content>
</language>
</page>
</site>
模板:
<xsl:template name="menubuildr">
<xsl:param name="p-path"/>
<xsl:param name="p-lang"/>
<xsl:param name="p-i" select="2"/>
<xsl:param name="p-filter"/>
<xsl:param name="p-max"/>
<xsl:if test="$p-i < $p-max">
<ul>
<xsl:variable name="nextLevel">
<xsl:for-each select="str:split(string($p-path),'/')">
<xsl:if test="position()<$p-i + 1">
<xsl:value-of select="."/>
<xsl:if test="position()<($p-i)">
<xsl:text>/</xsl:text>
</xsl:if>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:for-each
select="/site/page/language[
starts-with(@path,$p-filter)
and @dephtCount = ($p-i)
and $p-lang = @langname]">
<li>
<xsl:value-of select="@path"/>
<xsl:text>:</xsl:text>
<xsl:value-of select="@dephtCount"/>
<xsl:call-template name="menubuildr">
<xsl:with-param name="p-lang" select="$p-lang" />
<xsl:with-param name="p-filter" select="$nextLevel"/>
<xsl:with-param name="p-i" select="$p-i + 1"/>
<xsl:with-param name="p-path" select="$p-path"/>
<xsl:with-param name="p-max" select="$p-max"/>
</xsl:call-template>
</li>
</xsl:for-each>
</ul>
</xsl:if>
</xsl:template>
示例调用:
<div class="menu">Menu
<xsl:call-template name="menubuildr">
<xsl:with-param name="p-lang" select="@langname" />
<xsl:with-param name="p-filter">
<xsl:text>./</xsl:text>
<xsl:value-of select="@langname"/>
</xsl:with-param>
<xsl:with-param name="p-i" select="2"/>
<xsl:with-param name="p-path" select="@chemin"/>
<xsl:with-param name="p-max" select="count(str:split(string(@chemin),'/'))"/>
</xsl:call-template>
<hr/>
</div>
Here is a template I call to generate a menu, and it kinda breaks. Using Xalan, I get heap size error, so my guess would be that something in it is horribly broken and unholy.
I preset the template for the current portion of the website tree, feeding it the path from the root of the site, the language, the current depht (p-i), and a filter.
>
What this is supposed to do is to select everything on the current level, and then carry on with the next iteration which should be as simple as updating the "nextLevel" when called again, for the next level of menu...
Help?
Edit: As far as I know, it works as long as I don't go into recursion. I get the first level specified by my depht parameter, and nothing more.
The site is organized like this:
<?xml version="1.0"?>
<site xsi:noNamespaceSchemaLocation="onepagev2.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<page pagename="Alpha">
<language langname="en" path="./en/Alpha" dephtCount="3">
<title>Alpha Anglo</title>
<content>Oh noes.</content>
</language>
<language langname="fr" path="./fr/Alpha" dephtCount="3">
<title>Alpha Franco</title>
<content>Oh non.</content>
</language>
</page>
<page pagename="Beta">
<language langname="en" path="./en/Beta" dephtCount="3">
<title>Beta Anglo</title>
<content>Content B is content B is content B!</content>
</language>
<language langname="fr" path="./fr/Beta" dephtCount="3">
<title>Beta Franco</title>
<content>Contenu B est contenu B est contenu B!</content>
</language>
</page>
<page pagename="Beta2">
<language langname="en" path="./en/Beta/Beta2" dephtCount="4">
<title>Beta Anglo</title>
<content>Content B is content B is content B!</content>
</language>
<language langname="fr" path="./fr/Beta/Beta2" dephtCount="4">
<title>Beta Franco</title>
<content>Contenu B est contenu B est contenu B!</content>
</language>
</page>
</site>
The template:
<xsl:template name="menubuildr">
<xsl:param name="p-path"/>
<xsl:param name="p-lang"/>
<xsl:param name="p-i" select="2"/>
<xsl:param name="p-filter"/>
<xsl:param name="p-max"/>
<xsl:if test="$p-i < $p-max">
<ul>
<xsl:variable name="nextLevel">
<xsl:for-each select="str:split(string($p-path),'/')">
<xsl:if test="position()<$p-i + 1">
<xsl:value-of select="."/>
<xsl:if test="position()<($p-i)">
<xsl:text>/</xsl:text>
</xsl:if>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:for-each
select="/site/page/language[
starts-with(@path,$p-filter)
and @dephtCount = ($p-i)
and $p-lang = @langname]">
<li>
<xsl:value-of select="@path"/>
<xsl:text>:</xsl:text>
<xsl:value-of select="@dephtCount"/>
<xsl:call-template name="menubuildr">
<xsl:with-param name="p-lang" select="$p-lang" />
<xsl:with-param name="p-filter" select="$nextLevel"/>
<xsl:with-param name="p-i" select="$p-i + 1"/>
<xsl:with-param name="p-path" select="$p-path"/>
<xsl:with-param name="p-max" select="$p-max"/>
</xsl:call-template>
</li>
</xsl:for-each>
</ul>
</xsl:if>
</xsl:template>
Sample call:
<div class="menu">Menu
<xsl:call-template name="menubuildr">
<xsl:with-param name="p-lang" select="@langname" />
<xsl:with-param name="p-filter">
<xsl:text>./</xsl:text>
<xsl:value-of select="@langname"/>
</xsl:with-param>
<xsl:with-param name="p-i" select="2"/>
<xsl:with-param name="p-path" select="@chemin"/>
<xsl:with-param name="p-max" select="count(str:split(string(@chemin),'/'))"/>
</xsl:call-template>
<hr/>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为此测试
中有错误。相信我,这永远是真的。 :)
我猜你想要这个
I think there is an error in this test
<xsl:if test="position()<$p-i + 1">
.Believe me, it's always be true. :)
I guess you want this
<xsl:if test="position()<($p-i + 1)">