2

私は次のようなXMLを持っています:

<menu>
    <node id="home" url="url1.php">
          <label>Homepage</label>
          <node id="user" url="url2.php"><label>User profile</label></node>
          <node id="help" url="url3.php"><label>Help page</label></node>
     ...
    </node>
</menu>

メニューの生成に使用されるXMLにはnode、最初の「ホーム」の下の任意のレベルにネストされたタグがありますnode$id現在アクティブなメニュー項目を提供するPHPで呼び出されるパラメーターを渡します。

<label>ローカリゼーション用のラベルがたくさんあるため、これは別のタグにあり、属性としてではありません。実際のxmlは次のようになり<label lang='en'>...</label><label lang='it'>...</label>ます)

アイデアは、さまざまなXSLを使用して、メインメニュー、ブレッドクラム、セクションタイトル(トップメニュー)を生成することです。メインメニューの場合、私はこれを行うことができました:

<xsl:template match="menu">
     <xsl:apply-templates select="node" />
</xsl:template>

<xsl:template match="//node">
    <ul>
        <li>
            <a>
               <xsl:if test="@id=$id">
                  <xsl:attribute name='class'>active</xsl:attribute>
               </xsl:if>
               <xsl:attribute name='href'>
                  <xsl:value-of select="@url" />
               </xsl:attribute>
               <xsl:value-of select="label"/>
            </a>
            <xsl:if test="count(child::*)>0">
               <xsl:apply-templates select="node" />
            </xsl:if>
        </li>
    </ul>
    </xsl:template>
</xsl:stylesheet>

そしてそれは動作します。しかし、私はパンくずリストで立ち往生しています。@id=$id自宅から現在のページまでブレッドクラムを構築するために、特定のノードとその祖先のみを分離するにはどうすればよいですか?

結果のhtmlは、ノード広告の場合、3番目のネストされたレベルになります。

<ul>
    <li><a href="url1.php">Home</a></li>
    <li><a href="urla.php">Some child of home</a></li>
    <li><a href="urlb.php">Some grandchild of home</a></li>
    <li><a class='active' href="urlc.php">Current page which is child of the above</a></li>
</url>
4

1 に答える 1

1

The breadcrumb you can do like this, by selecting the active node and then walking the ancestors, like so:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" version="1.0" indent="yes"/>

    <xsl:template match="/">
        <!--Obviously set your PHP variable here-->
        <xsl:variable name="id">bananas</xsl:variable>
        <!--Find this node somewhere in the tree-->
        <xsl:apply-templates select=".//node[@id=$id]"/>
    </xsl:template>

    <xsl:template match="node">
        <!--Walk the ancestors-->
        <xsl:for-each select="ancestor-or-self::node">
        <!--Snappy path separator here-->
            <xsl:text> --> </xsl:text>
            <a>
                <xsl:attribute name="href">
                    <xsl:value-of select="@url" />
                </xsl:attribute>
                <xsl:value-of select="label/text()"/>
            </a>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>    

Sample Menu Xml:

<menu>
    <node id="home" url="url1.php">
          <label>Homepage</label>
          <node id="user" url="url2.php"><label>User profile</label></node>
          <node id="help" url="url3.php"><label>Help page</label></node>
    </node>
    <node id="products" url="urlN1.php">
        <label>Products</label>
        <node id="food" url="urlN2.php">
            <label>Food</label>
            <node id="fruit" url="urlN3.php">
                <label>Fruit</label>
                <node id="bananas" url="urlN4.php">
                    <label>Bananas</label>
                </node>
            </node>
            <node id="clothes" url="urlN3.php">
                <label>Clothes</label>
                <node id="shirts" url="urlN4.php">
                    <label>Shirts</label>
                </node>
            </node>
        </node>
    </node>
</menu>

Edit Update - You've updated the Q to display the breadcrumb as a list - here's an updated template just in case :)

<xsl:template match="node">
    <ul>
        <!--Walk the ancestors-->
        <xsl:for-each select="ancestor-or-self::node">
            <li>
                <a>
                    <xsl:attribute name="href">
                        <xsl:value-of select="@url" />
                    </xsl:attribute>
                    <xsl:value-of select="label/text()"/>
                </a>
            </li>
        </xsl:for-each>
    </ul>
</xsl:template>
于 2012-09-14T08:31:09.053 に答える