<?xml version="1.0" encoding="UTF-8" ?>

<xsl:stylesheet exclude-result-prefixes="xze" extension-element-prefixes="exsl" version="2.0" xmlns:exsl="http://exslt.org/common" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xze="blah">
	<xsl:output indent="yes" method="xml"/>
	<xsl:template match="/">
	
		<SystemDefinition name="Zebra" revision="R1A">
		<systemModel>
		<!-- Group by layers and name each layer group-->
		<xsl:for-each-group group-by="layer" select="/components/component">
			<xsl:element name="layer">
				<xsl:attribute name="name" select="current-grouping-key()"/>
				<!-- Group by logicalsets and name each logicalset group-->
				<xsl:for-each-group group-by="logicalset" select="current-group()">
					<!-- logicalsets may be empty - indicating no membership so do not display but continue processing-->
					<xsl:variable name="clsname" select="current-grouping-key()"/>
					<xsl:choose>
						<xsl:when test="$clsname!=''">
							<xsl:element name="logicalset">
								<xsl:attribute name="name" select="$clsname"/>
								
								<!-- Group by logicalsubsets and name each logicalsubset-->
								<xsl:for-each-group group-by="logicalsubset" select="current-group()">
									<xsl:variable name="clssname" select="current-grouping-key()"/>
									<xsl:choose>
										<xsl:when test="$clssname!=''">
											<xsl:element name="logicalsubset">
												<xsl:attribute name="name" select="current-grouping-key()"/>
								
												<!-- Group by modules and name each module -->
												<xsl:for-each-group group-by="module" select="current-group()">
													<xsl:element name="module">
														<xsl:attribute name="name" select="current-grouping-key()"/>
												
														<!-- write component information-->
														<xsl:apply-templates mode="unit" select="current-group()"/>
													</xsl:element>
											 	</xsl:for-each-group>
			
											</xsl:element>
										</xsl:when>
										<xsl:otherwise>
											<!-- logicalsubset is empty, so skip straight to modules-->
											<xsl:for-each-group group-by="module" select="current-group()">
												<xsl:element name="module">
													<xsl:attribute name="name" select="current-grouping-key()"/>
														<xsl:apply-templates mode="unit" select="current-group()"/>
													</xsl:element>
										 	</xsl:for-each-group>
										</xsl:otherwise>
									</xsl:choose>
								</xsl:for-each-group>
								<!-- logicalsubsets end-->
							</xsl:element>
						</xsl:when>
						<xsl:otherwise><!-- logicalset is empty, so skip straight to modules-->
										<!-- Group by modules and name each module -->
										<xsl:for-each-group group-by="module" select="current-group()">
											<xsl:element name="module">
												<xsl:attribute name="name" select="current-grouping-key()"/>
												
												<!-- write component information-->
													<xsl:apply-templates mode="unit" select="current-group()"/>
											</xsl:element>
									 	</xsl:for-each-group>
						</xsl:otherwise>
					</xsl:choose>
					</xsl:for-each-group>
					<!-- logicalsets end-->
				</xsl:element>
			</xsl:for-each-group>
			<!-- layers end-->
			</systemModel>
			</SystemDefinition>
	</xsl:template>

	<!-- function not currently used because 'element' tags do not get written for reasons I dont yet understand-->
	<xsl:function name="xze:modules">
		<xsl:param name="aModules"/>
		<xsl:for-each-group group-by="module/@name" select="$aModules">
			<xsl:element name="module">
				<xsl:attribute name="name" select="current-grouping-key()"/>
				<!-- write component information-->
				<xsl:apply-templates mode="unit" select="current-group()"/>
			</xsl:element>
		</xsl:for-each-group>
	</xsl:function>

	<!-- write component information-->
	<xsl:template match="*" mode="unit" name="unit">
		<xsl:element name="component">
		<xsl:attribute name="name" select="displayName"/>
			<xsl:element name="unit">
				<xsl:attribute name="unitID" select="@id"/><!-- Check if we need UnitID for anything -->
				<xsl:attribute name="name" select="@id"/>
				<xsl:attribute name="bldFile" select="bldInf"/><!-- Check if bldFile is required for DepModel-->
				<xsl:attribute name="mrp" select="mrpFile"/><!-- Check if mrp is required for DepModel-->
			</xsl:element>
		</xsl:element>
	</xsl:template>
</xsl:stylesheet>
<!-- Stylus Studio meta-information - (c) 2004-2006. Progress Software Corporation. All rights reserved.
<metaInformation>
<scenarios ><scenario default="yes" name="working sys def xml" userelativepaths="yes" externalpreview="no" url="..\_workingzebra_system_definition.xml" htmlbaseurl="" outputurl="" processortype="internal" useresolver="yes" profilemode="0" profiledepth="" profilelength="" urlprofilexml="" commandline="" additionalpath="" additionalclasspath="" postprocessortype="none" postprocesscommandline="" postprocessadditionalpath="" postprocessgeneratedext="" validateoutput="no" validator="internal" customvalidator=""/></scenarios><MapperMetaTag><MapperInfo srcSchemaPathIsRelative="yes" srcSchemaInterpretAsXML="no" destSchemaPath="" destSchemaRoot="" destSchemaPathIsRelative="yes" destSchemaInterpretAsXML="no" ><SourceSchema srcSchemaPath="..\_workingzebra_system_definition.xml" srcSchemaRoot="components" AssociatedInstance="" loaderFunction="document" loaderFunctionUsesURI="no"/></MapperInfo><MapperBlockPosition><template match="/"><block path="SystemDefinition/systemModel/xsl:for&#x2D;each&#x2D;group" x="325" y="0"/><block path="SystemDefinition/systemModel/xsl:for&#x2D;each&#x2D;group/xsl:element/xsl:for&#x2D;each&#x2D;group" x="295" y="0"/><block path="SystemDefinition/systemModel/xsl:for&#x2D;each&#x2D;group/xsl:element/xsl:for&#x2D;each&#x2D;group/xsl:choose" x="325" y="0"/><block path="SystemDefinition/systemModel/xsl:for&#x2D;each&#x2D;group/xsl:element/xsl:for&#x2D;each&#x2D;group/xsl:choose/!=[0]" x="279" y="0"/><block path="SystemDefinition/systemModel/xsl:for&#x2D;each&#x2D;group/xsl:element/xsl:for&#x2D;each&#x2D;group/xsl:choose/xsl:when/xsl:element/xsl:for&#x2D;each&#x2D;group" x="325" y="0"/><block path="SystemDefinition/systemModel/xsl:for&#x2D;each&#x2D;group/xsl:element/xsl:for&#x2D;each&#x2D;group/xsl:choose/xsl:when/xsl:element/xsl:for&#x2D;each&#x2D;group/xsl:choose" x="325" y="0"/><block path="SystemDefinition/systemModel/xsl:for&#x2D;each&#x2D;group/xsl:element/xsl:for&#x2D;each&#x2D;group/xsl:choose/xsl:when/xsl:element/xsl:for&#x2D;each&#x2D;group/xsl:choose/!=[0]" x="279" y="0"/><block path="SystemDefinition/systemModel/xsl:for&#x2D;each&#x2D;group/xsl:element/xsl:for&#x2D;each&#x2D;group/xsl:choose/xsl:when/xsl:element/xsl:for&#x2D;each&#x2D;group/xsl:choose/xsl:when/xsl:element/xsl:for&#x2D;each&#x2D;group" x="325" y="0"/><block path="SystemDefinition/systemModel/xsl:for&#x2D;each&#x2D;group/xsl:element/xsl:for&#x2D;each&#x2D;group/xsl:choose/xsl:when/xsl:element/xsl:for&#x2D;each&#x2D;group/xsl:choose/xsl:when/xsl:element/xsl:for&#x2D;each&#x2D;group/xsl:element/xsl:apply&#x2D;templates" x="325" y="0"/><block path="SystemDefinition/systemModel/xsl:for&#x2D;each&#x2D;group/xsl:element/xsl:for&#x2D;each&#x2D;group/xsl:choose/xsl:when/xsl:element/xsl:for&#x2D;each&#x2D;group/xsl:choose/xsl:otherwise/xsl:for&#x2D;each&#x2D;group" x="325" y="0"/><block path="SystemDefinition/systemModel/xsl:for&#x2D;each&#x2D;group/xsl:element/xsl:for&#x2D;each&#x2D;group/xsl:choose/xsl:when/xsl:element/xsl:for&#x2D;each&#x2D;group/xsl:choose/xsl:otherwise/xsl:for&#x2D;each&#x2D;group/xsl:element/xsl:apply&#x2D;templates" x="325" y="0"/><block path="SystemDefinition/systemModel/xsl:for&#x2D;each&#x2D;group/xsl:element/xsl:for&#x2D;each&#x2D;group/xsl:choose/xsl:otherwise/xsl:for&#x2D;each&#x2D;group" x="325" y="0"/><block path="SystemDefinition/systemModel/xsl:for&#x2D;each&#x2D;group/xsl:element/xsl:for&#x2D;each&#x2D;group/xsl:choose/xsl:otherwise/xsl:for&#x2D;each&#x2D;group/xsl:element/xsl:apply&#x2D;templates" x="325" y="0"/></template><template match="*"></template></MapperBlockPosition><TemplateContext></TemplateContext><MapperFilter side="source"></MapperFilter></MapperMetaTag>
</metaInformation>
-->