[xsl] combining two different sections and grouping

Subject: [xsl] combining two different sections and grouping
From: eric@xxxxxxxxxxxxxxxx
Date: Thu, 19 Aug 2004 12:12:22 -0400
Hello, I'm a newb with XSL and I've got a problem I can't figure out.  I've got
two sections in XML that I need to combine into one, and group when combined. 
The original data is already grouped in one section, but not in the other.  The
data that I need to group on may exist in only one or in both of the sections. 
So, I figured my steps would need to be:
1. Group the ungrouped section
2. Iterate through the grouped elements and output the elements from it
3. See if this group element was in the original data in the other section, if
so, output those elements
4. Finally go through the section that was already grouped and output it, but
only if it wasn't also a member of the section I iterated through already.

The last part is what I'm having problems figuring out how to do.  I found out
how to group with the Muenchian method and got all of that working, but I can't
figure out how to do step 4.

I'm realizing that probably made no sense, so here is some simplified test XML,
along with the desired output (in comments), and then my current XSL for this
test.

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

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

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

  <xsl:template match="test/mgr">
  <xsl:for-each select="/test/mgr/person[count(. | key('mgmt-by-dept',
department)[1]) = 1]">
    <xsl:variable name="currentDept" select="department"/>
    <dept>
    <title><xsl:value-of select="$currentDept"/></title>

    <mgmt><!-- build mgmt list -->
      <xsl:for-each select="key('mgmt-by-dept', department)">
        <person><name><xsl:value-of select="name"/></name></person>
      </xsl:for-each>
    </mgmt>

    <!-- build staff list for this dept-->
    <staff>
      <xsl:if test="$currentDept = /test/staff/dept/title">
        <xsl:for-each select="/test/staff/dept[title=$currentDept]/person">
          <person><name><xsl:value-of select="name"/></name></person>
        </xsl:for-each>
      </xsl:if>
    </staff>
    </dept>
  </xsl:for-each>

  <!-- now go through the /test/staff/dept elems not in our group, but how? -->

</xsl:template>
== end test XSL ===

Thanks!
-Eric

Current Thread