<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="xml" version="1.0" indent="yes"/>
	<xsl:key name="parts" match="*[@PARTREF_]" use="@PARTREF_"/>
	<xsl:key name="following-question-generators" match="*[@FOLLOWS_]" use="@FOLLOWS_"/>
	<xsl:template match="QUERY">
	  <xsl:apply-templates
		select="REC[generate-id(.) = generate-id(key('parts', @PARTREF_)[1])]" />
	</xsl:template>
	<xsl:template match="REC">
		<xsl:comment>
		<xsl:value-of select="@PARTREF_"/> | <xsl:value-of select="@PATH_"/></xsl:comment>
		<xsl:for-each select="key('parts', @PARTREF_)">
			<xsl:choose>
				<xsl:when test="@AVAILABILITY_='Hidden'">
					<!--xsl:comment>Hidden Question | <xsl:value-of select="@REF_"/> [<xsl:value-of select="@PARTREF_"/>] : <xsl:value-of select="@TEXT_"/> </xsl:comment-->
				</xsl:when>
				<xsl:otherwise>
					<Question>
						<xsl:attribute name="PartType"><xsl:value-of select="@PARTREF_"/></xsl:attribute>
						<xsl:attribute name="Ref"><xsl:value-of select="@REF_"/></xsl:attribute>
						<xsl:attribute name="DataType"><xsl:value-of select="@TYPE_"/></xsl:attribute>
						<xsl:attribute name="Text"><xsl:value-of select="@TEXT_"/></xsl:attribute>
						<xsl:call-template name="Action"/>
					</Question>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:for-each>
	</xsl:template>
	
	<xsl:template name="Action">
		<xsl:for-each select="key('following-question-generators', @REF_)">
							<Actions>
								<UpdatePart>
									<xsl:attribute name="PartType"><xsl:value-of select="@PARTREF_"/></xsl:attribute>
								</UpdatePart>
							</Actions>
						</xsl:for-each>
		</xsl:template>
</xsl:stylesheet>