2010-02-04 62 views
3

我有以下代碼:XSLT分組和亞組

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

    <xsl:key name="categories" match="Category" use="." /> 
    <xsl:key name="clients" match="Category" use ="Category/Client" /> 

    <xsl:template match="/"> 
    <ul id="red" class="treeview-red"> 
     <xsl:for-each select="/Promotions/Promotion/Category[ 
     generate-id(.) = generate-id(key('categories', .)[1]) 
     ]"> 
     <xsl:variable name="cname" select="." /> 

     <li> 
      <span> 
      <xsl:value-of select="$cname" /> 
      </span> 

      <xsl:for-each select="/Promotions/Promotion[Category=$cname]"> 
      <ul> 
       <li> 
       <span> 
        <xsl:value-of select="Client" /> 
       </span> 
       </li> 
       <ul> 
       <li> 
        <span> 
        <xsl:value-of select="Title" /> 
        </span> 
       </li> 
       </ul> 
      </ul> 
      </xsl:for-each> 
     </li> 
     </xsl:for-each> 

    </ul> 
    </xsl:template> 
</xsl:stylesheet> 

我的XML:

<Promotions> 
    <Promotion> 
    <Category>Arts &amp; Entertainment</Category> 
    <Client>Client 1</Client> 
    <Title>Get your Free 2</Title> 
    </Promotion> 
    <Promotion> 
    <Category>Arts &amp; Entertainment</Category> 
    <Client>Client 1</Client> 
    <Title>Get your Free 4</Title> 
    </Promotion> 
    <Promotion> 
    <Category>Arts &amp; Entertainment</Category> 
    <Client>Client 1</Client> 
    <Title>Get your Free 5</Title> 
    </Promotion> 
    <Promotion> 
    <Category>Community &amp; Neighborhood</Category> 
    <Client>Client 2</Client> 
    <Title>Get your Free 1</Title> 
    </Promotion> 
    <Promotion> 
    <Category>Education</Category> 
    <Client>Client 3</Client> 
    <Title>Get Your Free 3</Title> 
    </Promotion> 
</Promotions> 

它輸出以下:

<ul id="red" class="treeview-red"> 
    <li><span>Arts &amp; Entertainment</span><ul> 
     <li><span>Client 1</span></li> 
     <ul> 
     <li><span>Get your Free 2</span></li> 
     </ul> 
    </ul> 
    <ul> 
     <li><span>Client 1</span></li> 
     <ul> 
     <li><span>Get your Free 4</span></li> 
     </ul> 
    </ul> 
    <ul> 
     <li><span>Client 1</span></li> 
     <ul> 
     <li><span>Get your Free 5</span></li> 
     </ul> 
    </ul> 
    </li> 
    <li><span>Community &amp; Neighborhood</span><ul> 
     <li><span>Client 2</span></li> 
     <ul> 
     <li><span>Get your Free 1</span></li> 
     </ul> 
    </ul> 
    </li> 
    <li><span>Education</span><ul> 
     <li><span>Client 3</span></li> 
     <ul> 
     <li><span>Get Your Free 3</span></li> 
     </ul> 
    </ul> 
    </li> 
</ul> 

我想輸出進行分組首先由客戶分類,然後由客戶分類,對此的任何見解都會很棒:

<ul id="red" class="treeview-red"> 
    <li><span>Arts &amp; Entertainment</span><ul> 
     <li><span>Client 1</span></li> 
     <ul> 
     <li><span>Get your Free 2</span></li> 
     </ul> 
     <ul> 
     <li><span>Get your Free 4</span></li> 
     </ul> 
     <ul> 
     <li><span>Get your Free 5</span></li> 
     </ul> 
    </ul> 
    </li> 
    <li><span>Community &amp; Neighborhood</span><ul> 
     <li><span>Client 2</span></li> 
     <ul> 
     <li><span>Get your Free 1</span></li> 
     </ul> 
    </ul> 
    </li> 
    <li><span>Education</span><ul> 
     <li><span>client 3</span></li> 
     <ul> 
     <li><span>Get Your Free 3</span></li> 
     </ul> 
    </ul> 
    </li> 
</ul> 

基本上按類別分組後,我只希望看到一個客戶在該類別中該客戶的每次促銷的類別下。

+1

這將是非常有益的,看看源XML,以及要和是結果的XML結構獲得。 – 2010-02-04 22:45:32

+0

現在用XML進行編輯。 – 2010-02-05 03:45:03

回答

9

在沒有看到XML輸入的情況下,很難建議對樣式表進行更改,所以我現在所能做的就是將http://www.biglist.com/lists/xsl-list/archives/200101/msg00070.html作爲使用XSLT 1.0進行多級分組的示例。

[編輯]:這裏是你如何可以申請二級Muenchian分組的:

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

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

    <xsl:key name="k1" match="Promotion" use="Category"/> 
    <xsl:key name="k2" match="Promotion" use="concat(Category, '|', Client)"/> 

    <xsl:template match="Promotions"> 
    <ul id="red" class="treeview-red"> 
     <xsl:for-each select="Promotion[generate-id() = generate-id(key('k1', Category)[1])]"> 
     <li> 
      <span> 
      <xsl:value-of select="Category"/> 
      </span> 
      <xsl:for-each select="key('k1', Category)[generate-id() = generate-id(key('k2', concat(Category, '|', Client))[1])]"> 
      <ul> 
       <li> 
       <span> 
        <xsl:value-of select="Client"/> 
       </span> 
       <xsl:for-each select="key('k2', concat(Category, '|', Client))"> 
        <ul> 
        <li> 
         <span> 
         <xsl:value-of select="Title"/> 
         </span> 
        </li> 
        </ul> 
       </xsl:for-each> 
       </li> 
      </ul> 
      </xsl:for-each> 
     </li> 
     </xsl:for-each> 
    </ul> 
    </xsl:template> 

</xsl:stylesheet> 
+0

接受答案,因爲這正是我所尋找的。很快就會發布代碼和xml。 – 2010-02-05 23:36:17

+0

我編輯了我的問題,雖然你指出我在正確的方向,我不能讓它正常工作,請告知 – 2010-02-07 10:04:56