XSLT导航菜单 [英] XSLT Navigation Menu

查看:90
本文介绍了XSLT导航菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对XSL转换(XSLT-1)有疑问.我已经尝试了好几天了,但是没有使它起作用.

I have a question on XSL-Transformation (XSLT-1). I am trying it since days, but I didn't get it to work.

我想使用XML和XSLT-1构建标准的网站树导航.它仅应显示当前项目路径上的项目.如果当前项具有子项,则应打开下一个节点.

I want to build a standard website tree navigation with XML and XSLT-1. It should only show the items on the path to the current item. if the current item has child-items, the next node should be opened.

我的XML源是这样的:

My XML source is like this:

<cat>
    <item id="0" name="1">
        <item id="1" name="1.1"></item>
        <item id="2" name="1.2"></item>
        <item id="3" name="1.3"></item>
        <item id="4" name="1.4">
            <item id="5" name="1.4.1">
                <item id="6" name="1.4.1.1"></item>
                <item id="7" name="1.4.1.2"></item>
                <item id="8" name="1.4.1.3"></item>
            </item>
            <item id="9" name="1.4.2"></item>
            <item id="10" name="1.4.3"></item>
        </item>
        <item id="11" name="1.5"></item>
        <item id="12" name="1.6"></item>
    </item>
    <item id="13" name="2">
        <item id="14" name="2.1"></item>
        <item id="15" name="2.2"></item>
    </item>
    <item id="16" name="3"></item>
</cat>

现在,我想调用一个$ pageid = 7通过URL发送的页面,它应该显示如下输出:

Now I want to call a page with an $pageid=7 send by URL, and it should show some output like this:

<ul>
    <li>1
        <ul>
            <li>1.1</li>
            <li>1.2</li>
            <li>1.3</li>
            <li>1.4
                <ul>
                    <li>1.4.1
                        <ul>
                            <li>1.4.1.1</li>
                            <li class="activeitem">1.4.1.2</li>
                            <li>1.4.1.3</li>
                        </ul>
                    </li>
                    <li>1.4.2</li>
                    <li>1.4.3</li>
                </ul>
            </li>
            <li>1.5</li>
            <li>1.6</li>
        </ul>
    </li>
    <li>2</li>
    <li>3</li>
</ul>

如果我通过URL发送一个$ pageid = 4的页面,它应该显示如下输出:

If I call a page with an $pageid=4 send by URL, and it should show some output like this:

<ul>
    <li>1
        <ul>
            <li>1.1</li>
            <li>1.2</li>
            <li>1.3</li>
            <li class="activeitem">1.4
                <ul>
                    <li>1.4.1</li>
                    <li>1.4.2</li>
                    <li>1.4.3</li>
                </ul>
            </li>
            <li>1.5</li>
            <li>1.6</li>
        </ul>
    </li>
    <li>2</li>
    <li>3</li>
</ul>

希望我的例子对所有人都可以理解.

Hopefully my example is understandable for all.

我已经有了一个站点地图和一个导航导航,但这个过程似乎非常棘手,经验丰富.

I already got a sitemap and a breadcrumb navigation working, but this one seems to be very tricky and experienced.

我试图修改使用XML和XSLT的树导航的示例 -但是效果不佳.它仅显示项目的路径,而不显示其余的路径.

I tried to modify the examples from Tree navigation with XML and XSLT - but it didn't worked well. It only shows the path to the item, but not the rest.

对XSLT-1有经验的人可以帮助我吗?

Can somebody experienced with XSLT-1 help me with it?

或者也许有人已经可以在此处发布可行的解决方案了?

Or maybe somebody has already a working solution to post here?

这将是非常友善的.谢谢大家的关注.

It would be very kind. Thank you all for your attention.

推荐答案

这里的挑战是找到在新列表中导航的正确测试.对于给定的列表项,我认为您正在搜索这种测试(不是很明显):

The challenge here is to find the correct test for navigating inside a new list. For a given list item, I think you are searching for this kind of test (not really obvious):

 "item and .//@id=$pageid"

检查此转换.

[XSLT 1.0]

[XSLT 1.0]

 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output indent="yes"/>
    <xsl:param name="pageid" select="4"/>

    <xsl:template match="/">
        <xsl:call-template name="Navigate"/>
    </xsl:template>

    <xsl:template name="Navigate">
        <ul>
            <xsl:apply-templates select="/cat/item" mode="Navigate"/>
        </ul>
    </xsl:template>

    <xsl:template match="item" mode="Navigate">
        <li>
            <xsl:apply-templates select="@id[.=$pageid]" mode="Navigate"/>
            <xsl:value-of select="@name"/>
            <xsl:if test="item and .//@id=$pageid">
                <ul>
                    <xsl:apply-templates select="item" mode="Navigate"/>
                </ul>
            </xsl:if>
        </li>
    </xsl:template>

    <xsl:template match="@id" mode="Navigate">
        <xsl:attribute name="class">
            <xsl:value-of select="'activeitem'"/>
        </xsl:attribute>
    </xsl:template>

</xsl:stylesheet>


$ pageid = 4的结果:


result for $pageid = 4:

<ul>
   <li>1<ul>
         <li>1.1</li>
         <li>1.2</li>
         <li>1.3</li>
         <li class="activeitem">1.4<ul>
               <li>1.4.1</li>
               <li>1.4.2</li>
               <li>1.4.3</li>
            </ul>
         </li>
         <li>1.5</li>
         <li>1.6</li>
      </ul>
   </li>
   <li>2</li>
   <li>3</li>
</ul>

$ pageid = 7的结果:

result for $pageid=7:

<ul>
   <li>1<ul>
         <li>1.1</li>
         <li>1.2</li>
         <li>1.3</li>
         <li>1.4<ul>
               <li>1.4.1<ul>
                     <li>1.4.1.1</li>
                     <li class="activeitem">1.4.1.2</li>
                     <li>1.4.1.3</li>
                  </ul>
               </li>
               <li>1.4.2</li>
               <li>1.4.3</li>
            </ul>
         </li>
         <li>1.5</li>
         <li>1.6</li>
      </ul>
   </li>
   <li>2</li>
   <li>3</li>
</ul>

$ pageid = 13的结果:

result for $pageid=13:

<ul>
   <li>1</li>
   <li class="activeitem">2<ul>
         <li>2.1</li>
         <li>2.2</li>
      </ul>
   </li>
   <li>3</li>
</ul>

这篇关于XSLT导航菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆