私は次のような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>