2017-02-27 58 views
1

我希望有人能夠幫助我,因爲我一直在試圖弄清楚如何將數據集轉換爲分組在多個級別上的表結構。第一級是事件日期,第二級是公司名稱,最後一級是用戶。xsl用於對多層結果進行分組的轉換

以下是XML。

<xmlData> 
    <records> 
     <record> 
     <userid>1</userid> 
     <usersname>Jane Doe</usersname> 
     <companyname>Company A</companyname> 
     <eventdate>01 FEB 2017</eventdate> 
     <jeventdate>2457786</jeventdate> 
     </record> 
     <record> 
     <userid>3</userid> 
     <usersname>Jane Doe</usersname> 
     <companyname>Company B</companyname> 
     <eventdate>01 FEB 2017</eventdate> 
     <jeventdate>2457786</jeventdate> 
     </record> 
     <record> 
     <userid>2</userid> 
     <usersname>Joe Smith</usersname> 
     <companyname>Company B</companyname> 
     <eventdate>01 DEC 2016</eventdate> 
     <jeventdate>2457724</jeventdate> 
     </record> 
     <record> 
     <userid>2</userid> 
     <usersname>Joe Smith</usersname> 
     <companyname>Company B</companyname> 
     <eventdate>01 JAN 2017</eventdate> 
     <jeventdate>2457755</jeventdate> 
     </record> 
     <record> 
     <userid>2</userid> 
     <usersname>Joe Smith</usersname> 
     <companyname>Company B</companyname> 
     <eventdate>01 FEB 2017</eventdate> 
     <jeventdate>2457786</jeventdate> 
     </record> 
    </records> 
</xmlData> 

我試圖得到的結果顯示在以下簡單的HTML輸出中。

<h1>01 DEC 2016</h1> 
    <h2>Company B</h2> 
    <table> 
    <tr><td>2</td><td>Joe Smith</td></tr> 
    </table> 
<h1>01 JAN 2017</h1> 
    <h2>Company B</h2> 
    <table> 
    <tr><td>2</td><td>Joe Smith</td></tr> 
    </table> 
<h1>01 FEB 2017</h1> 
    <h2>Company A</h2> 
    <table> 
    <tr><td>1</td><td>Jane Doe</td></tr> 
    </table> 
    <h2>Company B</h2> 
    <table> 
    <tr><td>3</td><td>Dave Dodd</td></tr> 
    <tr><td>2</td><td>Joe Smith</td></tr> 
    </table> 

我遇到的問題實際上是兩部分。首先是獲得三個層次的深度。第二個是獲取所有記錄,而不僅僅是前幾個。

這是我一直在使用的XSL。

<xsl:key name="monthof" match="record" use="eventdate"/> 
<xsl:key name="companyof" match="record" use="concat(eventdate,'|',companyname)"/> 
<xsl:key name="userof" match="record" use="concat(eventdate,'|',companyname,'|',usersname)"/> 

<xsl:template match="xmlData/records"> 
    <xsl:for-each select="record[key('monthof',eventdate)]"> 
     <xsl:sort select="jeventdate"/> 
     <xsl:variable name="lstEventDate" select="key('monthof',eventdate)" /> 
     <h2><xsl:value-of select="eventdate"/></h2> 
     <xsl:for-each select="key('companyof',concat(eventdate,'|',companyname))"> 
      <xsl:sort select="companyname"/> 
      <h3> 
       <xsl:value-of select="companyname"/> 
      </h3> 
      <table> 
       <xsl:for-each select="key('userof',concat(eventdate,'|',companyname,'|',usersname))"> 
        <tr> 
         <td><xsl:value-of select="userid"/></td> 
         <td><xsl:value-of select="usersname"/></td> 
        </tr>         
       </xsl:for-each> 
      </table> 
     </xsl:for-each> 
    </xsl:for-each> 
</xsl:template> 

這裏是我得到的輸出。

01 DEC 2016 

Company B 

2 Jane Doe 

01 JAN 2017 

Company B 

2 Jane Doe 

01 FEB 2017 

Company A 

1 Joe Smith 

01 FEB 2017 

Company B 

1 Joe Smith 

Company B 

3 Dave Dodd 

01 FEB 2017 

Company B 

1 Joe Smith 

Company B 

2 Jane Doe 
+0

開始:http://www.jenitennison.com/xslt/grouping/muenchian.html然後做了多層次Muenchian分組的例子搜索在這裏。 –

+0

對於分組問題,您需要指定XSLT 1.0或2.0,因爲答案會非常不同。 –

回答

0

您所定義的按鍵都很好,但你需要再使用它們,如在評論已經指出,與Muenchian grouping方法:

<xsl:template match="xmlData/records"> 
    <xsl:for-each select="record[generate-id() = generate-id(key('monthof',eventdate)[1])]"> 
     <xsl:sort select="jeventdate"/> 
     <h2><xsl:value-of select="eventdate"/></h2> 
     <xsl:for-each select="key('monthof', eventdate)[generate-id() = generate-id(key('companyof',concat(eventdate,'|',companyname))[1])]"> 
      <xsl:sort select="companyname"/> 
      <h3> 
       <xsl:value-of select="companyname"/> 
      </h3> 
      <table> 
       <xsl:for-each select="key('companyof',concat(eventdate,'|',companyname))[generate-id() = generate-id(key('userof',concat(eventdate,'|',companyname,'|',usersname))[1])]"> 
        <tr> 
         <td><xsl:value-of select="userid"/></td> 
         <td><xsl:value-of select="usersname"/></td> 
        </tr>         
       </xsl:for-each> 
      </table> 
     </xsl:for-each> 
    </xsl:for-each> 
</xsl:template> 

您可能要檢查您是否可」 T移至XSLT 2.0,它成爲一個更容易在這裏做

<xsl:template match="xmlData/records"> 
    <xsl:for-each-group select="record" group-by="eventdate"> 
     <xsl:sort select="jeventdate"/> 
     <h2><xsl:value-of select="eventdate"/></h2> 
     <xsl:for-each-group select="current-group()" group-by="companyname"> 
      <xsl:sort select="companyname"/> 
      <h3> 
       <xsl:value-of select="companyname"/> 
      </h3> 
      <table> 
       <xsl:for-each-group select="current-group()" group-by="usersname"> 
        <tr> 
         <td><xsl:value-of select="userid"/></td> 
         <td><xsl:value-of select="usersname"/></td> 
        </tr>         
       </xsl:for-each-group> 
      </table> 
     </xsl:for-each-group> 
    </xsl:for-each-group> 
</xsl:template> 
+0

感謝馬丁,這完全做了我一直在尋找的東西。這是關鍵的多層次應用,我只是沒有得到,現在看到更多。希望我能做到V2,但我對微軟在Visual Studio舉行贖金,我不希望做一個切換到另一個引擎在整個應用程序的潛在變動將無法保證。 – user3528015