Re: [xsl] combining two different sections and grouping

Subject: Re: [xsl] combining two different sections and grouping
From: eric@xxxxxxxxxxxxxxxx
Date: Thu, 19 Aug 2004 14:48:11 -0400
Whoa, I figured it out.  It's probably still not the right way to do it, but
here's what I did.  Instead of starting by going through the not-grouped
section that I built a key for, I went through the already grouped section and
started building that list, then checked to see if that group was in my key and
if it was dumped that out.  When all that was done, I went through the key
again and if it was not in the other section already sorted on, I write out
it's elements.  Something tell me this isn't the best way to do it, but it's
working, so for now, I'm happy.  In case anybody is wondering, here's the XSL
that did the trick:

==== XSL ====
<xsl:key name="mgmt-by-dept" match="/test/mgr/person" use="department"/>

<xsl:template match="/">
  <team><xsl:apply-templates select="test/staff"/></team>
</xsl:template>

<xsl:template match="test/staff">
  <xsl:for-each select="dept">
    <xsl:variable name="dept" select="title"/>
    <dept>
      <title><xsl:value-of select="title"/></title>
      <staff>
        <xsl:for-each select="person">
          <person><name><xsl:value-of select="name"/></name></person>
        </xsl:for-each>
      </staff>
      <mgmt>
      <xsl:for-each select="key('mgmt-by-dept', $dept)">
        <person><name><xsl:value-of select="name"/></name></person>
      </xsl:for-each>
      </mgmt>
    </dept>
  </xsl:for-each>

  <!-- now iterate through the keyed list of departments from management -->
  <xsl:for-each select="/test/mgr/person[count(. | key('mgmt-by-dept',
department)[1]) = 1]">
    <xsl:variable name="dept" select="department"/>
    <xsl:if test="not($dept = /test/staff/dept/title)">
      <dept><title><xsl:value-of select="$dept"/></title>
        <staff/>
        <mgmt>
          <xsl:for-each select="key('mgmt-by-dept', department)">
            <person><name><xsl:value-of select="name"/></name></person>
          </xsl:for-each>
        </mgmt>
      </dept>
    </xsl:if>
  </xsl:for-each>
</xsl:template>
===== END XSL =====

-Eric

> ===== test XML ====
> <test>
> 	<staff>
> 		<dept>
> 			<title>receiving</title>
> 			<person><name>mike</name></person>
> 		</dept>
> 		<dept>
> 			<title>shipping</title>
> 			<person><name>eric</name></person>
> 		</dept>
> 	</staff>
>
> 	<mgr>
> 		<person>
> 			<name>fred</name>
> 			<department>shipping</department>
> 		</person>
> 		<person>
> 			<name>john</name>
> 			<department>accounting</department>
> 		</person>
> 	</mgr>
> </test>
>
> <!--
> 	Desired output:
>
> 	<team>
> 		<dept>
> 			<title>shipping</title>
> 			<staff>
> 				<person><name>eric</name></person>
> 			</staff>
> 			<mgmt>
> 				<person><name>fred</name></person>
> 			</mgmt>
> 		</dept>
> 		<dept>
> 			<title>receiving</title>
> 			<staff>
> 				<person><name>mike</name></person>
> 			</staff>
> 			<mgmt/>
> 		</dept>
> 		<dept>
> 			<title>accounting</title>
> 			<staff/>
> 			<mgmt>
> 				<person><name>john</name></person>
> 			</mgmt>
> 		</dept>
> 	</team>
>
> -->
> === end test XML ======

Current Thread