<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="xml" />

<!-- load external files -->
<xsl:variable name="grootboek" select="document('GrootboekVerdichtingscode.xml')" />
<xsl:variable name="verdicode" select="document('Verdicode_USGAAP_NL.xml')" />

<!-- Start transformation -->
<xsl:template match="/">
	<xsl:apply-templates select="@*|node()" />
	<!--xsl:apply-templates select="//GLAccount" /-->
</xsl:template>

<!-- process all entries of GLAccount and try to replace @code -->
<xsl:template match="GLAccount">
<GLAccount>
	<xsl:attribute name="code"><xsl:call-template name="concatAccount" /></xsl:attribute>
	<xsl:attribute name="type"><xsl:value-of select="@type" /></xsl:attribute>
	<xsl:attribute name="side"><xsl:value-of select="@side" /></xsl:attribute>
	<Description><xsl:value-of select="./Description" /></Description>
</GLAccount>
</xsl:template>

<!-- copy all elements -->
<xsl:template match="@*|node()">
	<xsl:copy>
		<xsl:apply-templates select="@*" />
		<xsl:apply-templates />
	</xsl:copy>
</xsl:template>


<!-- template to concat GLAccount with find corresponding USGAAPcode -->
<xsl:template name="concatAccount">
	<xsl:variable name="code" select="@code"/>
	<!-- use this to remove leading spaces -->
	<!--xsl:variable name="code" select="normalize-space(@code)"/-->
	
	<!-- find matching code in Grootboek / Grtbk. -->
	<xsl:variable name="GrootboekGrtbk" select="$grootboek//row[number(Grtbk.) = number($code)]"></xsl:variable>
	<xsl:variable name="costcenter" select="../Costcenter/@code" />
	<xsl:choose>
		<!-- if match found and costcenter defined, concat with corresponding verdicode // USGAAPcode -->
		<xsl:when test="$GrootboekGrtbk and number($costcenter) > 0"><xsl:value-of select="concat($code, $verdicode/document/row[Kostenplaats = number	($costcenter)]/USGAAPcode)" /></xsl:when>
		<xsl:otherwise><xsl:value-of select="$code" /></xsl:otherwise>
	</xsl:choose>	
</xsl:template>

</xsl:stylesheet>
<!-- Stylus Studio meta-information - (c)1998-2004. Sonic Software Corporation. All rights reserved.
<metaInformation>
<scenarios ><scenario default="yes" name="Scenario1" userelativepaths="yes" externalpreview="no" url="Main.xml" htmlbaseurl="" outputurl="" processortype="msxmldotnet" profilemode="0" profiledepth="" profilelength="" urlprofilexml="" commandline="" additionalpath="" additionalclasspath="" postprocessortype="none" postprocesscommandline="" postprocessadditionalpath="" postprocessgeneratedext=""/></scenarios><MapperMetaTag><MapperInfo srcSchemaPathIsRelative="yes" srcSchemaInterpretAsXML="no" destSchemaPath="" destSchemaRoot="" destSchemaPathIsRelative="yes" destSchemaInterpretAsXML="no" ><SourceSchema srcSchemaPath="Main.xml" srcSchemaRoot="FinEntries" AssociatedInstance="" loaderFunction="document" loaderFunctionUsesURI="no"/></MapperInfo><MapperBlockPosition><template match="/"><block path="xsl:apply&#x2D;templates" x="183" y="0"/></template></MapperBlockPosition></MapperMetaTag>
</metaInformation>
-->