XSLTドロップダウンツリー

私たちのサイトに何か階層的なものがあるとします。 たとえば、製品カテゴリ。 カテゴリにはサブカテゴリなどがあります。 この階層をドロップダウンリストに表示します。



たとえば、構造は次のXMLで表現されます。

< categories >

< category id ="1" title ="" >

< categories >

< category id ="2" title ="Intel" >

< categories >

< category id ="3" title ="Intel Core LGA775" >

< categories />

</ category >

< category id ="4" title ="Intel Core i7 LGA1366" >

< categories />

</ category >

</ categories >

</ category >

< category id ="6" title ="AMD" >

< categories >

< category id ="6" title ="AMD Athlon AM2" >

< categories />

</ category >

< category id ="7" title ="AMD Athlon II AM3" >

< categories />

</ category >

</ categories >

</ category >

</ categories >

</ category >

< category id ="8" title =" " >

< categories >

< category id ="9" title ="SATA" >

< categories >

< category id ="10" title ="Seagate" >

< categories />

</ category >

< category id ="11" title ="Western Digital" >

< categories />

</ category >

</ categories >

</ category >

< category id ="12" title ="IDE" >

< categories >

< category id ="13" title ="Seagate" >

< categories />

</ category >

< category id ="14" title ="Western Digital" >

< categories />

</ category >

</ categories >

</ category >

</ categories >

</ category >

</ categories >




* This source code was highlighted with Source Code Highlighter .




注: 実際には、属性に文字列を保存するのはよくありません;それらを別々のノードにして、CDATAでラップする必要があります。 ただし、ここでのタイトルは、スペースを節約するための属性です。 したがって、悪い例を挙げないでください。

そのため、このXMLからドロップダウンリストを作成する必要があります。 これを行う最も簡単な方法は、次の変換を記述することです。

< xsl:stylesheet version ="1.0" xmlns:xsl ="http://www.w3.org/1999/XSL/Transform" >

< xsl:output method ="html" indent ="yes" />



< xsl:template match ="categories" >

< select >

< option value ="0" > </ option >

< xsl:apply-templates select ="//category" />

</ select >

</ xsl:template >



< xsl:template match ="category" >

< option value ="{@id}" >

< xsl:value-of select ="@title" />

</ option >

</ xsl:template >

</ xsl:stylesheet >



* This source code was highlighted with Source Code Highlighter .






その結果、私たちはそのような画像を取得します

glyいリスト

一般的には機能しますが、カテゴリツリーをツリーのように見せたいです。 また、ここでselect



いるので、適切な場所にスペースを配置することによってのみこのツリーを取得できます。 もちろんoptgroup



がありますが、この場合、カテゴリは任意に大きなネストを持つことができるため、私たちを救うことはできません。

解決策



ここで、そのような自転車は与えられた問題を解決します:

  < xsl:stylesheet version = "1.0" xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" > 
      

< xsl:出力 メソッド = "html" インデント = "yes" />



< xsl:テンプレート 一致 = "/" >

< 選択 >

< オプション = "0" >ルートカテゴリ</ オプション >

< xsl:call-template name = "cat_tree" >

< xsl:with-param name = "space" select = "'&#160;&#160;'"
/>

</ xsl:call-template >

</ 選択 >

</ xsl:テンプレート >



< xsl:テンプレート = "cat_tree" match = "categories" >

< xsl:param name = "space" />

< xsl:for-each select = "categories / category" >

< オプション = "{@ id}" >

< xsl:select-of select = "$ space" />

< xsl:value-of select = "@ title" />

</ オプション >

< xsl:if test = "./ Categories" >

< xsl:call-template name = "cat_tree" >

< xsl:with-param name = "space" select = "concat($ space、
  '&#160;&#160;' 
) " />

</ xsl:call-template >

</ xsl:if >

</ xsl:for-each >

</ xsl:テンプレート >

</ xsl:スタイルシート >



*このソースコードは、 ソースコードハイライターで強調表示されました。


その結果、次の結果が得られます。

美しい木




All Articles