1<?xml version="1.0" encoding="utf-8"?>
2<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" 
3					 xmlns:exslt="http://exslt.org/common"
4					 xmlns:set="http://exslt.org/sets"
5                extension-element-prefixes="set">
6
7
8	<xsl:output method="xml"/>
9  
10	<xsl:template match="document">
11		<xsl:element name="document">
12			<xsl:copy-of select="/document/metaproperties"/>
13			<xsl:apply-templates select="/document/operator-stack/operator[@instruction=1]">
14				<xsl:with-param name="selection-criteria">
15					<xsl:copy-of select="/document/paradigm"/>
16				</xsl:with-param>
17			</xsl:apply-templates>
18		</xsl:element>
19	</xsl:template>
20	
21	<xsl:template match="operand[@type='domain']">
22		<xsl:param name="selection-criteria"/>
23		<xsl:variable name="butterfly"><xsl:value-of select="."/></xsl:variable>
24		<xsl:for-each select="/document/attributes/name[@name=$butterfly]/value">
25			<xsl:variable name="koala"><xsl:value-of select="@value"/></xsl:variable>
26			<xsl:variable name="pidgeon">
27				<xsl:element name="paradigm">
28					<xsl:copy-of select="exslt:node-set($selection-criteria)/paradigm/form[/attribute[@name=$butterfly and @value=$koala]]"/>
29				</xsl:element>
30			</xsl:variable>
31			<xsl:if test="count(exslt:node-set($selection-criteria)/paradigm/form[/attribute[@name=$butterfly and @value=$koala]]) &gt; 0">
32				<xsl:element name="node">
33					<xsl:attribute name="heading"><xsl:value-of select="@value"/></xsl:attribute>
34				</xsl:element>
35				<xsl:element name="forms">
36					<xsl:copy-of select="exslt:node-set($pidgeon)/paradigm/form"/>
37				</xsl:element>
38			</xsl:if>	
39		</xsl:for-each>		
40	</xsl:template>
41	
42	<xsl:template match="operand[@type='branch']">
43		<xsl:param name="selection-criteria"/>
44		<xsl:variable name="catapilla"><xsl:value-of select="."/></xsl:variable>
45		<xsl:apply-templates select="/document/operator-stack/operator[@instruction=$catapilla]">
46			<xsl:with-param name="selection-criteria">
47				<xsl:copy-of select="$selection-criteria"/>
48			</xsl:with-param>
49		</xsl:apply-templates>	
50	</xsl:template>
51
52	<xsl:template match="operator[@opcode='tabular']">
53		<xsl:param name="selection-criteria"/>
54		
55		<xsl:variable name="horizontal">
56			<xsl:apply-templates select="operand[@arg='1']">
57				<xsl:with-param name="selection-criteria">
58					<xsl:copy-of select="$selection-criteria"/>
59				</xsl:with-param>
60			</xsl:apply-templates>	
61		</xsl:variable>
62		
63		<xsl:variable name="vertical">
64			<xsl:apply-templates select="operand[@arg='2']">
65				<xsl:with-param name="selection-criteria">
66					<xsl:copy-of select="$selection-criteria"/>
67				</xsl:with-param>
68			</xsl:apply-templates>	
69		</xsl:variable>
70		
71		<!--	
72		<xsl:element name="horizontal">
73			<xsl:copy-of select="$horizontal"/>
74		</xsl:element>
75	
76		<xsl:element name="vertical">
77			<xsl:copy-of select="$vertical"/>
78		</xsl:element>
79		-->
80
81		<xsl:element name="cells">
82			<xsl:for-each select="exslt:node-set($horizontal)//forms">
83				<xsl:variable name="horizontal-forms">
84					<xsl:copy-of select="/form"/>
85				</xsl:variable>
86				
87				<xsl:for-each select="exslt:node-set($vertical)//forms">
88					<xsl:variable name="vertical-forms">
89						<xsl:copy-of select="/form"/>
90					</xsl:variable>
91					<xsl:copy-of select="exslt:node-set($horizontal-forms)/form | exslt:node-set($vertical-forms)/form"/>
92					<xsl:variable name="pidgeon">
93						<xsl:element name="paradigm">
94							<!--<xsl:copy-of select="$horizontal-forms[count(.|$vertical-forms)=count($vertical-forms)]"/>-->
95							<!--<xsl:copy-of select="set:intersection($horizontal-forms, $vertical-forms)"/>-->
96							<!--
97							<xsl:call-template name="set:intersection">
98							   <xsl:with-param name="nodes1" select="$horizontal-forms" />
99								<xsl:with-param name="nodes2" select="$vertical-forms" />
100							</xsl:call-template>
101							-->
102						</xsl:element>
103					</xsl:variable>
104					<xsl:copy-of select="$pidgeon"/>
105				</xsl:for-each>
106			</xsl:for-each>
107		</xsl:element>
108		
109	</xsl:template>
110	
111	<xsl:template match="operator[@opcode='hierarchical']">
112		<xsl:param name="selection-criteria"/>
113		<xsl:variable name="horse"><xsl:value-of select="@instruction"/></xsl:variable>
114		<xsl:element name="node">
115			<xsl:attribute name="heading"><xsl:value-of select="operand[@arg='1']"/></xsl:attribute>
116			<xsl:variable name="grub"><xsl:value-of select="operand[@arg='1']"/></xsl:variable>
117			
118			<xsl:for-each select="/document/attributes/name[@name=$grub]/value">
119				<xsl:variable name="koala"><xsl:value-of select="@value"/></xsl:variable>
120				<xsl:variable name="pidgeon">
121					<xsl:element name="paradigm">
122						<xsl:copy-of select="exslt:node-set($selection-criteria)/paradigm/form[/attribute[@name=$grub and @value=$koala]]"/>
123					</xsl:element>
124				</xsl:variable>
125				
126				<xsl:if test="count(exslt:node-set($selection-criteria)/paradigm/form[/attribute[@name=$grub and @value=$koala]]) &gt; 0">
127					<xsl:element name="node">
128						<xsl:attribute name="heading"><xsl:value-of select="@value"/></xsl:attribute>
129						<xsl:apply-templates select="/document/operator-stack/operator[@instruction=$horse]/operand[@arg='2']">
130							<xsl:with-param name="selection-criteria">
131								<xsl:copy-of select="$pidgeon"/>
132							</xsl:with-param>
133						</xsl:apply-templates>	
134					</xsl:element>
135				</xsl:if>
136			</xsl:for-each>	
137			
138		</xsl:element>
139	</xsl:template>
140
141</xsl:stylesheet>
142
143