2011-11-08 17 views
2

私はシンプルなフォームのxmlファイルを持っており、カテゴリーフィールドでグループ化されるリストにxslt(1.0)でコンテンツを表示したいと思っています。これのアイデア?以下のことを試してみてくださいおかげxsltグループ化リスト

は、私は、XSLTと非常に慣れていないよ...と私は任意のヘルプが

<collection> 

    <book> 
      <title>Sushi for dummies</title> 
      <author>Judi Strada</author> 
      <category>Cooking</category> 
      <year>2001</year> 
      <isbn>95641022</isbn> 
    </book> 

    <book> 
      <title>Sixties Design</title> 
      <author>Philippe Garner</author> 
      <category>Design</category> 
      <year>2007</year> 
      <isbn>64781365</isbn> 
    </book> 

    <book> 
      <title>Final Jeopardy</title> 
      <author>Stephen Baker</author> 
      <category>Computer Science</category> 
      <year>2011</year> 
      <isbn>8316363546</isbn> 
    </book> 

    <book> 
      <title>Spoon river anthology</title> 
      <author>Edgar Lee Masters</author> 
      <category>Poetry</category> 
      <year>1973</year> 
      <isbn>21565648362</isbn> 
    </book> 

    <book> 
      <title>The dark is rising</title> 
      <author>Susan Cooper</author> 
      <category>Philosophy</category> 
      <year>1973</year> 
      <isbn>47884564151</isbn> 
    </book> 

      <book> 
      <title>The graphic alphabet</title> 
      <author>David Pelletier</author> 
      <category>Design</category> 
      <year>1996</year> 
      <isbn>1322456655</isbn> 
    </book> 

    <book> 
      <title>Pattern Recognition and Machine Learning</title> 
      <author>Christopher M. Bishop</author> 
      <category>Computer Science</category> 
      <year>2006</year> 
      <isbn>45456531073</isbn> 
    </book> 

</collection> 

答えて

1

を歓迎しているので、このについての明確な何かを見つけることができませんでした。

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

    <!-- create an index of book nodes group by category --> 
    <xsl:key name="bookindex" match="book" use="category"/> 

    <xsl:template match="collection"> 
    <collections> 
     <!-- select book nodes which are the first node in their relevant index --> 
     <!-- this basically selects the node which has the same id as the first node 
      returned from the index --> 
     <xsl:apply-templates 
      select="book[generate-id() = 
         generate-id(key('bookindex', category)[1])]" 
      mode="group"/> 
    </collections> 
    </xsl:template> 

    <!-- handle the first node as the group pivot --> 
    <xsl:template match="book" mode="group"> 
    <group> 
     <category><xsl:value-of select="category"/></category> 
     <!-- select all book nodes which have the same category as myself --> 
     <xsl:apply-templates select="../book[category=current()/category]"/> 
    </group> 
    </xsl:template> 

    <!-- now handle each node in the book list --> 
    <xsl:template match="book"> 
      <title><xsl:value-of select="title"/></title> 
    </xsl:template> 

</xsl:stylesheet> 
+0

[Muenchian grouping](http://www.jenitennison.com/xslt/grouping/muenchian.xml) – Saxoier

0

これは "Muenchian grouping" と呼ばれている:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 
<xsl:key name="kBooksByCat" match="book" use="category"/> 

<xsl:template match="node()|@*"> 
    <xsl:copy> 
    <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match= 
"book 
    [generate-id() 
    = 
    generate-id(key('kBooksByCat', category)[1]) 
    ] 
"> 
    <category name="{category}"> 
    <xsl:copy-of select="key('kBooksByCat', category)"/> 
    </category> 
</xsl:template> 
</xsl:stylesheet> 

提供されるXML文書に適用された場合:

<collection> 
    <book> 
     <title>Sushi for dummies</title> 
     <author>Judi Strada</author> 
     <category>Cooking</category> 
     <year>2001</year> 
     <isbn>95641022</isbn> 
    </book> 
    <book> 
     <title>Sixties Design</title> 
     <author>Philippe Garner</author> 
     <category>Design</category> 
     <year>2007</year> 
     <isbn>64781365</isbn> 
    </book> 
    <book> 
     <title>Final Jeopardy</title> 
     <author>Stephen Baker</author> 
     <category>Computer Science</category> 
     <year>2011</year> 
     <isbn>8316363546</isbn> 
    </book> 
    <book> 
     <title>Spoon river anthology</title> 
     <author>Edgar Lee Masters</author> 
     <category>Poetry</category> 
     <year>1973</year> 
     <isbn>21565648362</isbn> 
    </book> 
    <book> 
     <title>The dark is rising</title> 
     <author>Susan Cooper</author> 
     <category>Philosophy</category> 
     <year>1973</year> 
     <isbn>47884564151</isbn> 
    </book> 
    <book> 
     <title>The graphic alphabet</title> 
     <author>David Pelletier</author> 
     <category>Design</category> 
     <year>1996</year> 
     <isbn>1322456655</isbn> 
    </book> 
    <book> 
     <title>Pattern Recognition and Machine Learning</title> 
     <author>Christopher M. Bishop</author> 
     <category>Computer Science</category> 
     <year>2006</year> 
     <isbn>45456531073</isbn> 
    </book> 
</collection> 

指名手配、正しくグループ化された結果が生成される

<collection> 
    <category name="Cooking"> 
     <book> 
     <title>Sushi for dummies</title> 
     <author>Judi Strada</author> 
     <category>Cooking</category> 
     <year>2001</year> 
     <isbn>95641022</isbn> 
     </book> 
    </category> 
    <category name="Design"> 
     <book> 
     <title>Sixties Design</title> 
     <author>Philippe Garner</author> 
     <category>Design</category> 
     <year>2007</year> 
     <isbn>64781365</isbn> 
     </book> 
     <book> 
     <title>The graphic alphabet</title> 
     <author>David Pelletier</author> 
     <category>Design</category> 
     <year>1996</year> 
     <isbn>1322456655</isbn> 
     </book> 
    </category> 
    <category name="Computer Science"> 
     <book> 
     <title>Final Jeopardy</title> 
     <author>Stephen Baker</author> 
     <category>Computer Science</category> 
     <year>2011</year> 
     <isbn>8316363546</isbn> 
     </book> 
     <book> 
     <title>Pattern Recognition and Machine Learning</title> 
     <author>Christopher M. Bishop</author> 
     <category>Computer Science</category> 
     <year>2006</year> 
     <isbn>45456531073</isbn> 
     </book> 
    </category> 
    <category name="Poetry"> 
     <book> 
     <title>Spoon river anthology</title> 
     <author>Edgar Lee Masters</author> 
     <category>Poetry</category> 
     <year>1973</year> 
     <isbn>21565648362</isbn> 
     </book> 
    </category> 
    <category name="Philosophy"> 
     <book> 
     <title>The dark is rising</title> 
     <author>Susan Cooper</author> 
     <category>Philosophy</category> 
     <year>1973</year> 
     <isbn>47884564151</isbn> 
     </book> 
    </category> 
    <book> 
     <title>The graphic alphabet</title> 
     <author>David Pelletier</author> 
     <category>Design</category> 
     <year>1996</year> 
     <isbn>1322456655</isbn> 
    </book> 
    <book> 
     <title>Pattern Recognition and Machine Learning</title> 
     <author>Christopher M. Bishop</author> 
     <category>Computer Science</category> 
     <year>2006</year> 
     <isbn>45456531073</isbn> 
    </book> 
</collection> 
関連する問題