2013-05-06 75 views
0

希望你做得很好 我有一種情況,我從那個是平的服務的結果集結構 這個結果集經理,他的項目和活動中的每個項目XSLT - 組2級

我想改造這個扁平結構,以XML是

Grouped by Manager 
    Grouped by Project 
     Activities in that Project 

這裏是XML,我有

<Results> 
<Result> 
    <ActivityId>101</ActivityId> 
    <ActivityName>T1</ActivityName> 
    <ProjectId>1001</ProjectId> 
    <ProjectName>Prj1</ProjectName> 
    <Description>some desc</Description> 
    <manager>jcooper</manager> 
</Result> 
<Result> 
    <ActivityId>102</ActivityId> 
    <ActivityName>T2</ActivityName> 
    <ProjectId>1002</ProjectId> 
    <ProjectName>Prj2</ProjectName> 
    <Description>some desc</Description> 
    <manager>jstein</manager> 
</Result> 
<Result> 
    <ActivityId>103</ActivityId> 
    <ActivityName>T3</ActivityName> 
    <ProjectId>1001</ProjectId> 
    <ProjectName>Prj1</ProjectName> 
    <Description>some desc</Description> 
    <manager>jcooper</manager> 
</Result> 
<Result> 
    <ActivityId>104</ActivityId> 
    <ActivityName>T4</ActivityName> 
    <ProjectId>1002</ProjectId> 
    <ProjectName>Prj2</ProjectName> 
    <Description>some desc</Description> 
    <manager>jcooper</manager> 
</Result> 
<Result> 
    <ActivityId>105</ActivityId> 
    <ActivityName>T5</ActivityName> 
    <ProjectId>1002</ProjectId> 
    <ProjectName>Prj2</ProjectName> 
    <Description>some desc</Description> 
    <manager>jstein</manager> 
</Result> 
</Results> 

在這個什麼, 經理jcooper具有Prj1 {已經活動T1,T3},並Prj2 {已經活動T4} 經理jstein具有Prj1(具有活性T2)和prj2 {已經活動T5}

這裏是我想要的格式爲

Manager 
    List of Prjs 
     List of Activities in corresponding Project 

<Results> 
    <ManagerWiseResult> 
     <Manager>jcooper</Manager> 
     <Project> 
      <ProjectId>1001</ProjectId> 
      <ProjectName>Prj1</ProjectName> 
      <Activities> <!--Activities in Project1 --> 
       <IndActivity> 
        <ActivityId>101</ActivityId> 
        <ActivityName>T1</ActivityName> 
       </IndActivity> 
       <IndActivity> 
        <ActivityId>103</ActivityId> 
        <ActivityName>T2</ActivityName> 
       </IndActivity> 
      </Activities> 
     </Project> 
     <Project> 
      <ProjectId>1002</ProjectId> 
      <ProjectName>Prj2</ProjectName> 
      <Activities> 
       <IndActivity> 
        <ActivityId>104</ActivityId> 
        <ActivityName>T4</ActivityName> 
       </IndActivity> 
      </Activities> 
     </Project> 
    </ManagerWiseResult> 
    <ManagerWiseResult> 
     <Project> 
      <ProjectId>1001</ProjectId> 
      <ProjectName>Prj1</ProjectName> 
      <Activities> 
       <IndActivity> 
        <ActivityId>102</ActivityId> 
        <ActivityName>T2</ActivityName> 
       </IndActivity> 
      </Activities> 
     </Project> 
     <Project> 
      <ProjectId>1002</ProjectId> 
      <ProjectName>Prj2</ProjectName> 
      <Activities> 
       <IndActivity> 
        <ActivityId>105</ActivityId> 
        <ActivityName>T5</ActivityName> 
       </IndActivity> 
      </Activities> 
     </Project> 
    </ManagerWiseResult> 
</Results> 

我想弄清楚一個解決方案,但不能。 專家,請你在這方面幫助我。

由於事先 拉維

+0

您僅限於使用XSLT 1.0嗎?或者你可以使用2.0的設施嗎? – Borodin 2013-05-06 19:19:22

+0

我可以使用XSLT 2.0。 你介意看看我的評論,並幫助我與2.0語法。在此先謝謝 – user2350784 2013-05-06 20:13:01

回答

0

這裏的解決方案基於「Muenchian分組的」

試試這個:

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

    <xsl:key name="kmanager" match="Result" use="manager" /> 
    <xsl:key name="kmanager_prj" match="Result" use="concat(manager,'#',ProjectId)" /> 

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

    <xsl:template match="Result"> 
     <IndActivity> 
      <xsl:apply-templates select="ActivityId" /> 
      <xsl:apply-templates select="ActivityName" /> 
     </IndActivity> 
    </xsl:template> 

    <xsl:template match="Result" mode="project"> 
     <Project> 
      <xsl:copy-of select="ProjectId"/> 
      <xsl:copy-of select="ProjectName"/> 

      <Activities> 
       <xsl:for-each select="key('kmanager_prj', concat(manager,'#',ProjectId))">  
        <xsl:apply-templates select="."/> 
       </xsl:for-each> 
      </Activities> 

     </Project> 
    </xsl:template> 


     <xsl:template match="Result" mode="manager"> 
     <xsl:apply-templates select="customer | jobType" /> 
      <ManagerWiseResult> 
       <Manager> 
        <xsl:value-of select="manager"/> 
       </Manager> 
       <xsl:apply-templates mode="project" 
      select="//Result[ 
       generate-id()= 
       generate-id(
        key('kmanager_prj', 
          concat (current()/manager,'#', ProjectId) 
         ) [1])]" />    
      </ManagerWiseResult> 
    </xsl:template> 
    <xsl:template match="/*"> 
     <xsl:copy> 
     <xsl:apply-templates mode="manager" 
     select="//Result[ 
       generate-id()= 
       generate-id(
        key('kmanager', 
          manager 
         ) [1])]" /> 
     </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

將產生這樣的輸出:

<?xml version="1.0"?> 
<Results> 
    <ManagerWiseResult> 
     <Manager>jcooper</Manager> 
     <Project> 
      <ProjectId>1001</ProjectId> 
      <ProjectName>Prj1</ProjectName> 
      <Activities> 
       <IndActivity> 
        <ActivityId>101</ActivityId> 
        <ActivityName>T1</ActivityName> 
       </IndActivity> 
       <IndActivity> 
        <ActivityId>103</ActivityId> 
        <ActivityName>T3</ActivityName> 
       </IndActivity> 
      </Activities> 
     </Project> 
     <Project> 
      <ProjectId>1002</ProjectId> 
      <ProjectName>Prj2</ProjectName> 
      <Activities> 
       <IndActivity> 
        <ActivityId>104</ActivityId> 
        <ActivityName>T4</ActivityName> 
       </IndActivity> 
      </Activities> 
     </Project> 
    </ManagerWiseResult> 
    <ManagerWiseResult> 
     <Manager>jstein</Manager> 
     <Project> 
      <ProjectId>1002</ProjectId> 
      <ProjectName>Prj2</ProjectName> 
      <Activities> 
       <IndActivity> 
        <ActivityId>102</ActivityId> 
        <ActivityName>T2</ActivityName> 
       </IndActivity> 
       <IndActivity> 
        <ActivityId>105</ActivityId> 
        <ActivityName>T5</ActivityName> 
       </IndActivity> 
      </Activities> 
     </Project> 
    </ManagerWiseResult> 
</Results> 

注意:但是,這並不符合你的說法:

在此,經理jcooper有Prj1 {具有活動T1,T3}和Prj2 {具有活動T4}管理者jstein具有Prj1(具有活動T2)並且prj2 {具有活動T5}

+0

這真棒!非常感謝。 – user2350784 2013-05-06 20:06:51

+0

很高興我能幫到你。如果這是您正在尋找的內容,請將答案標爲有效。 – 2013-05-06 20:10:59

+0

嗨 這太棒了!像魅力一樣工作,非常感謝。 你介意用XSLT 2幫助我嗎?0語法,我已經完成了這個,但是這隻給出了一部分結果,prj明智的seggregation沒有爲我發愁。我想在這裏粘貼我的XSL,但它不允許我這樣做,因爲我可以回答我的問題。只有8小時後,評論部分太少了。我將一個接一個地粘貼我的XSLT。 – user2350784 2013-05-06 20:13:48

0

下面是一個可能的XSLT 2.0解決方案。

當這個XSLT:

<?xml version="1.0" encoding="UTF-8"?> 
<Results> 
    <Result> 
    <ActivityId>101</ActivityId> 
    <ActivityName>T1</ActivityName> 
    <ProjectId>1001</ProjectId> 
    <ProjectName>Prj1</ProjectName> 
    <Description>some desc</Description> 
    <manager>jcooper</manager> 
    </Result> 
    <Result> 
    <ActivityId>102</ActivityId> 
    <ActivityName>T2</ActivityName> 
    <ProjectId>1002</ProjectId> 
    <ProjectName>Prj2</ProjectName> 
    <Description>some desc</Description> 
    <manager>jstein</manager> 
    </Result> 
    <Result> 
    <ActivityId>103</ActivityId> 
    <ActivityName>T3</ActivityName> 
    <ProjectId>1001</ProjectId> 
    <ProjectName>Prj1</ProjectName> 
    <Description>some desc</Description> 
    <manager>jcooper</manager> 
    </Result> 
    <Result> 
    <ActivityId>104</ActivityId> 
    <ActivityName>T4</ActivityName> 
    <ProjectId>1002</ProjectId> 
    <ProjectName>Prj2</ProjectName> 
    <Description>some desc</Description> 
    <manager>jcooper</manager> 
    </Result> 
    <Result> 
    <ActivityId>105</ActivityId> 
    <ActivityName>T5</ActivityName> 
    <ProjectId>1002</ProjectId> 
    <ProjectName>Prj2</ProjectName> 
    <Description>some desc</Description> 
    <manager>jstein</manager> 
    </Result> 
</Results> 

......想要的結果產生:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:output omit-xml-declaration="yes" indent="yes"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:key name="kResultByManager" match="Result" use="manager"/> 

    <xsl:template match="/*"> 
    <Results> 
     <xsl:for-each-group 
     select="*/manager" 
     group-by="."> 
     <ManagerWiseResult> 
      <Manager> 
      <xsl:value-of select="."/> 
      </Manager> 
      <xsl:for-each-group 
      select="key('kResultByManager', current-grouping-key())" 
      group-by="ProjectId"> 
      <Project> 
       <xsl:copy-of select="ProjectId|ProjectName"/> 
       <Activities> 
        <xsl:for-each select="current-group()"> 
        <IndActivity> 
         <xsl:copy-of select="ActivityId|ActivityName"/> 
        </IndActivity> 
        </xsl:for-each> 
       </Activities> 
       </Project> 
      </xsl:for-each-group> 
      </ManagerWiseResult> 
     </xsl:for-each-group> 
     </Results> 
     </xsl:template> 
</xsl:stylesheet> 

......是對所提供的XML應用

<Results> <ManagerWiseResult> <Manager>jcooper</Manager> <Project> <ProjectId>1001</ProjectId> <ProjectName>Prj1</ProjectName> <Activities> <IndActivity> <ActivityId>101</ActivityId> <ActivityName>T1</ActivityName> </IndActivity> <IndActivity> <ActivityId>103</ActivityId> <ActivityName>T3</ActivityName> </IndActivity> </Activities> </Project> <Project> <ProjectId>1002</ProjectId> <ProjectName>Prj2</ProjectName> <Activities> <IndActivity> <ActivityId>104</ActivityId> <ActivityName>T4</ActivityName> </IndActivity> </Activities> </Project> </ManagerWiseResult> <ManagerWiseResult> <Manager>jstein</Manager> <Project> <ProjectId>1002</ProjectId> <ProjectName>Prj2</ProjectName> <Activities> <IndActivity> <ActivityId>102</ActivityId> <ActivityName>T2</ActivityName> </IndActivity> <IndActivity> <ActivityId>105</ActivityId> <ActivityName>T5</ActivityName> </IndActivity> </Activities> </Project> </ManagerWiseResult> </Results> 
+0

非常感謝,它的工作 如何標記特定答案爲正確的? – user2350784 2013-05-07 04:05:43

+0

您只需點擊答案左上角的複選標記即可。 – ABach 2013-05-07 04:12:18