0

希望你做得很好我有一个场景,我从一个扁平结构的服务中得到一个结果集这个结果集有经理,他的项目和每个项目中的活动

我想把这个平面结构转换成 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>

我正在尝试找出解决方案,但无法解决。专家,请您在这方面帮助我。

提前致谢, 拉维

4

2 回答 2

0

这是一种可能的 XSLT 2.0 解决方案。

当这个 XSLT:

<?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 应用:

<?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>

...产生了想要的结果:

<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>
于 2013-05-06T21:13:36.263 回答
0

这里有一个基于“Muenchian grouping”的解决方案。

尝试这个:

<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}

于 2013-05-06T19:58:01.893 回答