<?xml version="1.0"?>
<xsl:stylesheet version="1.1"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:xforms="http://www.w3.org/2002/xforms"
  xmlns:ev="http://www.w3.org/2001/xml-events" 
  xmlns:fo="http://www.w3.org/1999/XSL/Format"
  xmlns:exslt="http://exslt.org/common"
  extension-element-prefixes="exslt">
   
  <!-- Stylesheet for converting schemas to XForms
       Created by Matthew J. Graham, Caltech, Feb 2004
         based on SchemaXForm.xsl by Philip Garvey 
         added support for multiple namespaces, substitution groups,
         imported/included schemata
       Amended to use FormsPlayer when possible, Mar 2004 
       Amended for new schema structure (usage of xsi:type), Jun 2004 
       Fixed for repeat nodeset bug, Jul 2004 -->

  <!-- Syntax for use: xsltproc (-)-stringparam root <root>
                                (-)-stringparam rootType <rootType> 
                                (-)-stringparam targetAction <post>
                                (-)-stringparam substitute <xx:type1/yy:type2;...>
                                -->

  <!-- Global declarations -->
  
  <xsl:output method="xml" indent="yes"/>
  
  <xsl:variable name="newline">
<xsl:text>
</xsl:text>
  </xsl:variable> 
  
  <!-- User-defined values -->
  
  <!-- the type of the root element of the instance associated with the form document -->
  <xsl:param name="root"/>
  <xsl:param name="rootType"/>
  <xsl:variable name="targetNS" select="substring-before($rootType, ':')"/>
  <xsl:variable name="targetType" select="substring-after($rootType, ':')"/>
  <!-- the form data will be posted to this URI -->
  <!--currently this transform only supports the 'post' method, though this could be entirely configurable-->
  <xsl:param name="targetAction"/>
  
  <xsl:param name="substitute"/>
  <xsl:variable name="subs">
    <xsl:call-template name="Substitutes">
      <xsl:with-param name="args" select="$substitute"/>
    </xsl:call-template>
  </xsl:variable>
  <xsl:template name="Substitutes">
    <xsl:param name="args"/>
    <xsl:if test="string-length($args) > 0">
      <xsl:variable name="sub">
        <xsl:choose>
          <xsl:when test="contains($args, ';')">
            <xsl:value-of select="substring-before($args, ';')"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$args"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:element name="Substitute"> 
        <xsl:attribute name="Group"><xsl:value-of select="substring-before($sub, '/')"/></xsl:attribute>
        <xsl:attribute name="TypeNS"><xsl:value-of select="substring-before(substring-after($sub, '/'), ':')"/></xsl:attribute>
        <xsl:attribute name="Type"><xsl:value-of select="substring-after(substring-after($sub, '/'), ':')"/></xsl:attribute>
      </xsl:element>
      <xsl:choose>
        <xsl:when test="string-length(substring-after($args, ';')) > 0">
      	  <xsl:call-template name="Substitutes">
	    <xsl:with-param name="args" select="substring-after($args, ';')"/>
	  </xsl:call-template>
        </xsl:when>
      </xsl:choose>
    </xsl:if>
  </xsl:template>
  
  <xsl:variable name="namespace" select="/xs:schema/@targetNamespace"/>
  <xsl:variable name="prefix">
    <xsl:value-of select="name(/xs:schema/namespace::*[string() = $namespace])"/>
  </xsl:variable>
      
        
  <xsl:variable name="allSchema">
    <xsl:apply-templates select="/xs:schema/xs:import" mode="index"/>
    <xsl:apply-templates select="/xs:schema/xs:include" mode="index"/>
  </xsl:variable>
  <xsl:template match="xs:import" mode="index">
    <xsl:variable name="importedNS" select="@namespace"/>
    <xsl:variable name="pref" select="name(document(@schemaLocation)/xs:schema/namespace::*[string() = $importedNS])"/>
    <xsl:variable name="ns" select="@namespace"/>
    <xsl:element name="XMLSchema">
      <xsl:attribute name="prefix"><xsl:value-of select="$pref"/></xsl:attribute>
      <xsl:attribute name="urn"><xsl:value-of select="$ns"/></xsl:attribute>
      <xsl:attribute name="uri"><xsl:value-of select="@schemaLocation"/></xsl:attribute>
    </xsl:element>
    <xsl:apply-templates select="document(@schemaLocation)/xs:schema/xs:import" mode="index"/>
    <xsl:for-each select="document(@schemaLocation)/xs:schema/xs:include">
      <xsl:call-template name="include">
        <xsl:with-param name="pref" select="$pref"/>
	<xsl:with-param name="urn" select="$ns"/>
      </xsl:call-template>
    </xsl:for-each>	
  </xsl:template>
  <xsl:template match="xs:include" mode="index">
    <xsl:element name="XMLSchema">
      <xsl:attribute name="prefix"><xsl:value-of select="$prefix"/></xsl:attribute>
      <xsl:attribute name="urn"><xsl:value-of select="$namespace"/></xsl:attribute>
      <xsl:attribute name="uri"><xsl:value-of select="@schemaLocation"/></xsl:attribute>
    </xsl:element>
    <xsl:apply-templates select="document(@schemaLocation)/xs:schema/xs:import" mode="index"/>
    <xsl:apply-templates select="document(@schemaLocation)/xs:schema/xs:include" mode="index"/>
  </xsl:template>
  <xsl:template name="include">
    <xsl:param name="pref"/>
    <xsl:param name="urn"/>
    <xsl:element name="XMLSchema">
      <xsl:attribute name="prefix"><xsl:value-of select="$pref"/></xsl:attribute>
      <xsl:attribute name="urn"><xsl:value-of select="$urn"/></xsl:attribute>
      <xsl:attribute name="uri"><xsl:value-of select="@schemaLocation"/></xsl:attribute>
    </xsl:element>
    <xsl:apply-templates select="document(@schemaLocation)/xs:schema/xs:import" mode="index"/>
    <xsl:for-each select="document(@schemaLocation)/xs:schema/xs:include">
      <xsl:call-template name="include">
        <xsl:with-param name="pref" select="$pref"/>
	<xsl:with-param name="urn" select="$urn"/>
      </xsl:call-template>
    </xsl:for-each>	
  </xsl:template>
    
  <xsl:variable name="externals">
    <xsl:for-each select="exslt:node-set($allSchema)/XMLSchema">
      <xsl:sort select="@uri"/>
      <xsl:variable name = "lastURI" select="@uri"/>
      <xsl:if test="not(preceding-sibling::XMLSchema[@uri=$lastURI])">      
      <xsl:element name="schema">
        <xsl:attribute name="prefix"><xsl:value-of select="@prefix"/></xsl:attribute>
	<xsl:attribute name="urn"><xsl:value-of select="@urn"/></xsl:attribute>
	<xsl:copy-of select="document(@uri)"/>
      </xsl:element>
    </xsl:if>
    </xsl:for-each>
  </xsl:variable>
      
  <xsl:variable name="local">
    <xsl:element name="schema">
      <xsl:attribute name="urn"><xsl:value-of select="$namespace"/></xsl:attribute>
      <xsl:attribute name="prefix"><xsl:value-of select="$prefix"/></xsl:attribute>
      <xsl:copy-of select="/"/>
    </xsl:element>
  </xsl:variable>  
    
  <!-- ************** -->
  <!--  Root element  -->
  <!-- ************** -->  
  
  <xsl:template match="xs:schema">
    <!--Build the XFORMS model element first -->
    <xsl:element name="html" namespace="http://www.w3.org/1999/xhtml">
    <!-- Hack namespaces -->
    <xsl:call-template name="HackNamespaces"/>
    <xsl:attribute name="xforms:sims"  namespace="http://www.w3.org/2002/xforms"/>
    <xsl:attribute name="xsi:sims" namespace="http://www.w3.org/2001/XMLSchema-instance"/>
    <xsl:element name="object">
      <xsl:attribute name="id">FormsPlayer</xsl:attribute>
      <xsl:attribute name="classid">CLSID:4D0ABA11-C5F0-4478-991A-375C4B648F58</xsl:attribute>
      <xsl:attribute name="width">0</xsl:attribute>
      <xsl:attribute name="height">0</xsl:attribute>
      <b>FormsPlayer has failed to load! Please check your installation.</b>
      <br/>
      <br/>
    </xsl:element> 
    <xsl:processing-instruction name="import">namespace="xforms" implementation="#FormsPlayer"</xsl:processing-instruction> 
    <!--<html xmlns="http://www.w3.org/1999/xhtml">-->
      <xsl:element name="head" namespace="http://www.w3.org/1999/xhtml">
        <xsl:element name="xforms:model">
	      <xsl:attribute name="id">
	        <xsl:value-of select="concat($targetType,'Model')"/>
	      </xsl:attribute>
	  <!-- should we put an xforms:schema element here? would need documentation inside the schema giving its name-->
          <!-- how about xforms:bind elements? -->
	  <!-- How about a link to an instance instead of embedding the instance here?-->
	  <xsl:element name="xforms:instance">
	    <xsl:attribute name="id">
	      <xsl:value-of select="concat($targetType,'Instance')"/>
	    </xsl:attribute>
        <xsl:element name="{$root}">
          <xsl:attribute name="type" namespace="http://www.w3.org/2001/XMLSchema-instance">
              <xsl:value-of select="$rootType"/>
          </xsl:attribute>
          <xsl:call-template name="Attributes">
	        <xsl:with-param name="typeName" select="$targetType"/>
	        <xsl:with-param name="typeNS" select="$targetNS"/>
	      </xsl:call-template>
    	      <xsl:apply-templates select="exslt:node-set($local)//xs:schema/xs:complexType[@name = $targetType]" mode="buildInstance"/>
	      <xsl:apply-templates select="exslt:node-set($externals)/schema[@prefix = $targetNS]/xs:schema/xs:complexType[@name = $targetType]" mode="buildInstance"/>
        </xsl:element>
	  </xsl:element>
	  <xsl:element name="xforms:submission">
	    <xsl:attribute name="id">formSubmit</xsl:attribute>
	    <xsl:attribute name="method">post</xsl:attribute>
	    <xsl:attribute name="action">
	      <xsl:value-of select="$targetAction"/>
	    </xsl:attribute>
	  </xsl:element>
        </xsl:element>
      </xsl:element>
      <xsl:element name="body" namespace="http://www.w3.org/1999/xhtml">
        <xsl:call-template name="HackNamespaces"/>
	    <xsl:apply-templates select="exslt:node-set($local)//xs:schema/xs:element[@name = $targetType]" mode="buildUI">
	      <xsl:with-param name="currXPath" select="@name"/>
	      <xsl:with-param name="type" select="$rootType"/>
	    </xsl:apply-templates>
	    <xsl:apply-templates select="exslt:node-set($externals)/schema[@prefix = $targetNS]/xs:schema/xs:element[@name = $targetType]" mode="buildUI">
	      <xsl:with-param name="currXPath" select="@name"/>
          <xsl:with-param name="type" select="$rootType"/>
	    </xsl:apply-templates> 
	    <xsl:call-template name="RootElement">
	      <xsl:with-param name="currXPath" select="concat('/', $root)"/>
	      <xsl:with-param name="name" select="$root"/>
	      <xsl:with-param name="type" select="$rootType"/>
	    </xsl:call-template> 
	    <xsl:element name="xforms:submit" namespace="http://www.w3.org/2002/xforms">
	      <xsl:attribute name="submission">formSubmit</xsl:attribute>
	      <xsl:element name="xforms:label">Submit</xsl:element>
	    </xsl:element>
      </xsl:element>
    </xsl:element>
    
  <!--</html>-->
  </xsl:template>
  
  <xsl:template match="*"/> 

  <!-- ************************************* -->
  <!--  Build the XForms Instance templates  -->
  <!-- ************************************* -->

  <xsl:template match="xs:element[@ref]" mode="buildInstance">
    <xsl:variable name="elementNSPrefix" select="substring-before(@ref, ':')"/>
    <xsl:variable name="elementName" select="substring-after(@ref, ':')"/>
    <xsl:choose>
      <!-- Recursion trap -->
      <xsl:when test="not(ancestor::xs:element[@name=$elementName])">
        <xsl:apply-templates select="exslt:node-set($externals)/schema[@prefix = $elementNSPrefix]/xs:schema/xs:element[@name = $elementName]" mode="buildInstance"/>	
        <xsl:apply-templates select="exslt:node-set($local)//xs:schema/xs:element[@name = $elementName]" mode="buildInstance"/>
      </xsl:when>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template match="xs:element[@name]" mode="buildInstance">
    <xsl:variable name="ref" select="@type"/>
    <xsl:variable name="typeNS" select="substring-before(@type, ':')"/>
    <xsl:variable name="type" select="substring-after(@type, ':')"/>
    <xsl:variable name="TypeNS">
      <xsl:choose>
        <xsl:when test="count(exslt:node-set($subs)/Substitute[@Group = $ref]) > 0">
	      <xsl:value-of select="exslt:node-set($subs)/Substitute[@Group = $ref]/@TypeNS"/>
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:value-of select="$typeNS"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="Type">
      <xsl:choose>
        <xsl:when test="count(exslt:node-set($subs)/Substitute[@Group = $ref]) > 0">
	      <xsl:value-of select="exslt:node-set($subs)/Substitute[@Group = $ref]/@Type"/>
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:value-of select="$type"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <!-- Make sure that the element is not abstract -->
    <xsl:choose>
      <xsl:when test="@abstract"/>
      <xsl:otherwise>   
        <xsl:element name="{@name}" namespace="{ancestor::schema/@urn}">   
          <xsl:if test="count(exslt:node-set($subs)/Substitute[@Group = $ref]) > 0"> 
            <xsl:attribute name="type" namespace="http://www.w3.org/2001/XMLSchema-instance">
              <xsl:value-of select="concat($TypeNS, ':', $Type)"/>
            </xsl:attribute>
          </xsl:if>
          <xsl:call-template name="Attributes">
	        <xsl:with-param name="typeName" select="$Type"/>
	        <xsl:with-param name="typeNS" select="$TypeNS"/>
	      </xsl:call-template>
	      <xsl:choose>
	        <!-- The type is named -->
	        <xsl:when test="$type">
              <!-- Simple types 
	          <xsl:apply-templates select="exslt:node-set($local)//xs:schema/xs:simpleType[@name = $Type]" mode="buildInstance"/>
	          <xsl:apply-templates select="exslt:node-set($externals)/schema[@prefix = $TypeNS]/xs:schema/xs:simpleType[@name = $Type]" mode="buildInstance"/>  -->
              <!-- Complex types -->
	          <xsl:apply-templates select="exslt:node-set($local)//xs:schema/xs:complexType[@name = $Type]" mode="buildInstance"/>
	          <xsl:apply-templates select="exslt:node-set($externals)/schema[@prefix = $TypeNS]/xs:schema/xs:complexType[@name = $Type]" mode="buildInstance"/>  
	        </xsl:when>
	        <xsl:otherwise>
	          <xsl:apply-templates select="*" mode="buildInstance"/>
	        </xsl:otherwise>
	      </xsl:choose>
	      <xsl:call-template name="DefaultValue"/>
	    </xsl:element>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template match="xs:complexType|xs:complexContent|xs:sequence|xs:all|xs:group[@name]|xs:simpleContent" mode="buildInstance">
    <xsl:apply-templates select="*" mode="buildInstance"/>
  </xsl:template>
  
  <xsl:template match="xs:extension" mode="buildInstance">
    <xsl:variable name="NS" select="substring-before(@base, ':')"/>
    <xsl:variable name="type" select="substring-after(@base, ':')"/>
    <xsl:call-template name="Attributes">
      <xsl:with-param name="typeName" select="$type"/>
      <xsl:with-param name="typeNS" select="$NS"/>
    </xsl:call-template>
    <xsl:apply-templates select="exslt:node-set($local)//xs:schema/xs:complexType[@name = $type]" mode="buildInstance"/>
    <xsl:apply-templates select="exslt:node-set($externals)/schema[@prefix = $NS]/xs:schema/xs:complexType[@name = $type]" mode="buildInstance"/>
    <xsl:apply-templates select="*" mode="buildInstance"/>
  </xsl:template>

  <!-- Only interested in simpleTypes which have constrained values through enumeration
       Set the default value to be that of the first enumerated value -->
  <xsl:template match="xs:simpleType" mode="buildInstance">
    <xsl:call-template name="DefaultValue"/>
  </xsl:template>
        
  <xsl:template match="*" mode="buildInstance"/>

  <!--****************************************-->
  <!-- build the XForms UI templates-->
  <!--****************************************-->
  
  <xsl:template match="xs:complexType" mode="buildUI">
    <xsl:param name="currXPath"/>
    <xsl:param name="beingExtended"/>
    <xsl:param name="refMaxOccurs"/>
    <xsl:param name="maxOccurs"/>
    <xsl:param name="label"/>
    <!-- build a group with a ref to the currXPath -->
    <!--<p>
      <xsl:value-of select="@name"/>
    </p>-->
    <!-- if this complex type is being matched on as a result of being extended, we don't want to make another group. To accomplish this, this template will be called with the parameter 'beingExtended'. if this maps to 'true', then we just call templates without forming a group.-->
    <!-- will need to add logic to handle repeating elements. in the case of a repeating element, the stylesheet should create an xforms:repeat and not an xforms:group -->
    <xsl:choose>
      <xsl:when test="$beingExtended='true'">
        <xsl:apply-templates select="*" mode="buildUI">
	  <xsl:with-param name="currXPath" select="$currXPath"/>
	  <xsl:with-param name="label" select="$label"/>
        </xsl:apply-templates>
      </xsl:when>
      <xsl:otherwise>
        <xsl:choose>
	  <xsl:when test="number($maxOccurs)>1">
	    <xsl:call-template name="BuildRepeatControl">
	      <xsl:with-param name="currNode" select="."/>
	      <xsl:with-param name="currXPath" select="$currXPath"/>
	      <xsl:with-param name="label" select="$label"/>
	    </xsl:call-template>
	  </xsl:when>
	  <xsl:when test="number($refMaxOccurs)>1">
	    <xsl:call-template name="BuildRepeatControl">
	      <xsl:with-param name="currNode" select="."/>
	      <xsl:with-param name="currXPath" select="$currXPath"/>
	      <xsl:with-param name="label" select="$label"/>
	    </xsl:call-template>
          </xsl:when>
	  <xsl:when test="$maxOccurs='unbounded'">
	    <xsl:call-template name="BuildRepeatControl">
	      <xsl:with-param name="currNode" select="."/>
	      <xsl:with-param name="currXPath" select="$currXPath"/>
	      <xsl:with-param name="label" select="$label"/>
	    </xsl:call-template>
	  </xsl:when>
	  <xsl:when test="$refMaxOccurs='unbounded'">
	    <xsl:call-template name="BuildRepeatControl">
	      <xsl:with-param name="currNode" select="."/>
	      <xsl:with-param name="currXPath" select="$currXPath"/>
	      <xsl:with-param name="label" select="$label"/>
	    </xsl:call-template>
	  </xsl:when>
	  <xsl:otherwise>
<!--	    <xsl:element name="xforms:group"> 
	      <xsl:attribute name="ref"><xsl:value-of select="$currXPath"/></xsl:attribute> -->
	      <xsl:element name="xforms:label">
      		<xsl:value-of select="$label"/>
	      </xsl:element>
	      <xsl:apply-templates select="*" mode="buildUI">
		    <xsl:with-param name="currXPath" select="$currXPath"/>
		    <xsl:with-param name="label" select="$label"/>
	      </xsl:apply-templates>
<!--	    </xsl:element> -->
	  </xsl:otherwise>
	</xsl:choose>
      </xsl:otherwise>
    </xsl:choose> 
  </xsl:template>

  <xsl:template match="xs:extension" mode="buildUI">
    <xsl:param name="currXPath"/>
    <xsl:param name="label"/>
    <xsl:variable name="typeNS" select="substring-before(@base,':')"/>
    <xsl:variable name="typeName" select="substring-after(@base,':')"/>
    <!-- Base is an xs type -->
    <xsl:choose>
      <xsl:when test="contains($typeNS, 'xs')">
        <xsl:call-template name="BuildSingleInputControl">
          <xsl:with-param name="currXPath" select="$currXPath"/>
	  <xsl:with-param name="label" select="$label"/>
	</xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:apply-templates select="exslt:node-set($externals)/schema[@prefix=$typeNS]/xs:schema/xs:complexType[@name=$typeName]" mode="buildUI">
      	  <xsl:with-param name="currXPath" select="$currXPath"/>
    	  <xsl:with-param name="label" select="$label"/>
    	  <xsl:with-param name="beingExtended">true</xsl:with-param>
        </xsl:apply-templates>
        <xsl:apply-templates select="exslt:node-set($local)//xs:schema/xs:complexType[@name=$typeName]" mode="buildUI">
	  <xsl:with-param name="currXPath" select="$currXPath"/>
    	  <xsl:with-param name="label" select="$label"/>
    	  <xsl:with-param name="beingExtended">true</xsl:with-param>
        </xsl:apply-templates>
        <xsl:apply-templates select="exslt:node-set($externals)/schema[@prefix=$typeNS]/xs:schema/xs:simpleType[@name=$typeName]" mode="buildUI">
    	  <xsl:with-param name="currXPath" select="$currXPath"/>
    	  <xsl:with-param name="label" select="$label"/>
    	  <xsl:with-param name="beingExtended">true</xsl:with-param>
        </xsl:apply-templates>
        <xsl:apply-templates select="exslt:node-set($local)//xs:schema/xs:simpleType[@name=$typeName]" mode="buildUI">
    	  <xsl:with-param name="currXPath" select="$currXPath"/>
    	  <xsl:with-param name="label" select="$label"/>
    	  <xsl:with-param name="beingExtended">true</xsl:with-param>
        </xsl:apply-templates>
      </xsl:otherwise>
    </xsl:choose>  
        <xsl:apply-templates select="*" mode="buildUI">
    	  <xsl:with-param name="currXPath" select="$currXPath"/>
    	  <xsl:with-param name="label" select="$label"/>
        </xsl:apply-templates>
  </xsl:template>
  
  <xsl:template match="xs:simpleType" mode="buildUI">
    <!-- this also needs to check whether the element of this type is repeating. in the case of a repeating element, the stylesheet should create a xforms:repeat or an xforms:select-->
    <xsl:param name="currXPath"/>
    <xsl:param name="label"/>
    <xsl:param name="beingExtended"/>
    <xsl:param name="elementName"/>
    <xsl:param name="refMaxOccurs"/>
    <xsl:param name="maxOccurs"/>
    <!--<xsl:param name="repeating"/>-->
    <xsl:choose>
      <xsl:when test="xs:restriction/xs:enumeration">
        <!-- here it needs to differentiate select vs select1 based on the refMaxOccurs and maxOccurs parameters-->
        <!--
        <xsl:element name="xforms:select1">
          <xsl:attribute name="ref"><xsl:value-of select="$currXPath"/></xsl:attribute>
          <xsl:element name="xforms:label">
            <xsl:value-of select="$label"/>
          </xsl:element>
          <xsl:for-each select="xs:restriction/xs:enumeration">
            <xsl:element name="xforms:item">
              <xsl:element name="xforms:caption">
                <xsl:value-of select="xs:annotation/xs:appinfo"/>
              </xsl:element>
              <xsl:element name="xforms:value">
                <xsl:value-of select="@value"/>
              </xsl:element>
            </xsl:element>
          </xsl:for-each>
        </xsl:element>-->
        <xsl:choose>
          <xsl:when test="number($refMaxOccurs)>1">
            <xsl:call-template name="BuildSelectControl">
	      <xsl:with-param name="currXPath" select="$currXPath"/>
              <xsl:with-param name="label" select="$label"/>
              <xsl:with-param name="currentNode" select="."/>
              <xsl:with-param name="select">select</xsl:with-param>
	    </xsl:call-template>
          </xsl:when>
          <xsl:when test="number($maxOccurs)>1">
	    <xsl:call-template name="BuildSelectControl">
	      <xsl:with-param name="currXPath" select="$currXPath"/>
	      <xsl:with-param name="label" select="$label"/>
	      <xsl:with-param name="currentNode" select="."/>
	      <xsl:with-param name="select">select</xsl:with-param>
            </xsl:call-template>
	  </xsl:when>
	  <xsl:when test="$refMaxOccurs = 'unbounded'">
	    <xsl:call-template name="BuildSelectControl">
	      <xsl:with-param name="currXPath" select="$currXPath"/>
	      <xsl:with-param name="label" select="$label"/>
	      <xsl:with-param name="currentNode" select="."/>
	      <xsl:with-param name="select">select</xsl:with-param>
	    </xsl:call-template>
    	  </xsl:when>
    	  <xsl:when test="$maxOccurs='unbounded'">
	    <xsl:call-template name="BuildSelectControl">
	      <xsl:with-param name="currXPath" select="$currXPath"/>
	      <xsl:with-param name="label" select="$label"/>
	      <xsl:with-param name="currentNode" select="."/>
	      <xsl:with-param name="select">select</xsl:with-param>
	    </xsl:call-template>
          </xsl:when>
    	  <xsl:otherwise>
    	    <xsl:call-template name="BuildSelectControl">
	      <xsl:with-param name="currXPath" select="$currXPath"/>
	      <xsl:with-param name="label" select="$label"/>
	      <xsl:with-param name="currentNode" select="."/>
	      <xsl:with-param name="select">select1</xsl:with-param>
    	    </xsl:call-template>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
    	<xsl:call-template name="BuildSingleInputControl">
      	  <xsl:with-param name="currXPath" select="$currXPath"/>
          <xsl:with-param name="label" select="$label"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
	
  <xsl:template match="xs:complexContent|xs:simpleContent|xs:sequence|xs:all|xs:group[@name]" mode="buildUI">
    <xsl:param name="currXPath"/>
    <xsl:param name="label"/>
    <xsl:apply-templates select="*" mode="buildUI">
      <xsl:with-param name="currXPath" select="$currXPath"/>
      <xsl:with-param name="label" select="$label"/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="xs:attribute" mode="buildUI">
    <xsl:param name="currXPath"/>
    <xsl:variable name="xpath" select="concat($currXPath,'/@',@name)"/>
<!--    <xsl:variable name="label" select="xs:annotation/xs:appinfo/Label"/> -->
    <xsl:variable name="label" select="@name"/>
    <xsl:variable name="attributeName" select="@name"/>
    <xsl:variable name="typeNS" select="substring-before(@type,':')"/>
    <xsl:variable name="typeName" select="substring-after(@type,':')"/>
    <xsl:choose>
      <xsl:when test="@fixed">
        <!-- when an attribute has a fixed value, don't create an input control-->
      </xsl:when>
      <!-- add processing for creating select1 control for attributes of type xs:boolean -->
      <xsl:when test="contains(@type, 'boolean')">
    	<xsl:call-template name="BuildBooleanSelect">
    	  <xsl:with-param name="currXPath" select="$xpath"/>
    	  <xsl:with-param name="label" select="$label"/>
    	</xsl:call-template>
      </xsl:when>
      <xsl:when test="contains(@type,'xs')">
    	<xsl:call-template name="BuildSingleInputControl">
    	  <xsl:with-param name="currXPath" select="$xpath"/>
    	  <xsl:with-param name="label" select="$label"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:when test="$typeName">
	<!-- process the attribute's simple type here. -->
    	<xsl:apply-templates select="exslt:node-set($local)//xs:schema/xs:simpleType[@name=$typeName]" mode="buildUI">
    	  <xsl:with-param name="currXPath" select="$xpath"/>
    	  <xsl:with-param name="attributeName" select="$attributeName"/>
    	  <xsl:with-param name="label" select="$label"/>
    	</xsl:apply-templates>
    	<xsl:apply-templates select="exslt:node-set($externals)/schema[@prefix=$typeNS]/xs:schema/xs:simpleType[@name=$typeName]" mode="buildUI">
    	  <xsl:with-param name="currXPath" select="$xpath"/>
    	  <xsl:with-param name="attributeName" select="$attributeName"/>
    	  <xsl:with-param name="label" select="$label"/>
    	</xsl:apply-templates>
      </xsl:when>
      <xsl:otherwise>
      <!-- No type given - inline definition -->
	<xsl:apply-templates select="*" mode="buildUI">
	  <xsl:with-param name="currXPath" select="$xpath"/>
	  <xsl:with-param name="label" select="$label"/>
	</xsl:apply-templates>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="xs:element[@ref]" mode="buildUI">
    <xsl:param name="currXPath"/>
    <xsl:param name="label"/>
    <!--<xsl:variable name="refRepeating" select="@maxOccurs"/>-->
    <xsl:variable name="ref" select="@ref"/>
    <xsl:variable name="elementNSPrefix" select="substring-before(@ref, ':')"/>
    <xsl:variable name="elementName" select="substring-after(@ref, ':')"/>
    <xsl:variable name="ElementNS" select="$elementNSPrefix"/>
    <xsl:variable name="Element" select="$elementName"/>
    <xsl:apply-templates select="exslt:node-set($externals)/schema[@prefix = $ElementNS]/xs:schema/xs:element[@name = $Element]" mode="buildUI">
      <xsl:with-param name="currXPath" select="$currXPath"/>
      <xsl:with-param name="label" select="$label"/>
      <xsl:with-param name="refMaxOccurs" select="@maxOccurs"/>
    </xsl:apply-templates>
    <xsl:apply-templates select="exslt:node-set($local)//xs:schema/xs:element[@name = $Element]" mode="buildUI">
      <xsl:with-param name="currXPath" select="$currXPath"/>
      <xsl:with-param name="label" select="$label"/>
      <xsl:with-param name="refMaxOccurs" select="@maxOccurs"/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="xs:element[@name]" mode="buildUI">
    <!-- get the type
      if 'xs', do control
    -->
    <xsl:param name="currXPath"/>
    <xsl:param name="type"/>
    <!--<xsl:param name="refRepeating"/>-->
    <xsl:param name="refMaxOccurs"/>
    <xsl:param name="refMinOccurs"/>
    <!--<xsl:variable name="repeating"/>-->
    <xsl:variable name="maxOccurs" select="@maxOccurs"/>
    <xsl:variable name="ref" select="@type"/>

    <xsl:variable name="typeNS">
      <xsl:choose>
        <xsl:when test="count(exslt:node-set($subs)/Substitute[@Group = $ref]) > 0">
	      <xsl:value-of select="exslt:node-set($subs)/Substitute[@Group = $ref]/@TypeNS"/>
	    </xsl:when>
	    <xsl:otherwise>
	      <xsl:choose>
	        <xsl:when test="$type">
	          <xsl:value-of select="substring-before($type, ':')"/>
	        </xsl:when>
	        <xsl:otherwise>
	          <xsl:value-of select="substring-before(@type, ':')"/>
	        </xsl:otherwise>
	      </xsl:choose>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="typeName">
      <xsl:choose>
        <xsl:when test="count(exslt:node-set($subs)/Substitute[@Group = $ref]) > 0">
      	  <xsl:value-of select="exslt:node-set($subs)/Substitute[@Group = $ref]/@Type"/>
	    </xsl:when>
	    <xsl:otherwise>
          <xsl:choose>
            <xsl:when test="$type">
              <xsl:value-of select="substring-after($type, ':')"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="substring-after(@type, ':')"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="elementName" select="@name"/>
    <xsl:variable name="NSprefix" select="ancestor::schema/@prefix"/>
    <xsl:variable name="label" select="@name"/>
    <!--    <xsl:variable name="label" select="xs:annotation/xs:appinfo/Label"/>
        <xsl:variable name="xpath" select="concat($currXPath,'/',ancestor::xs:schema/xs:annotation/xs:appinfo/Prefix,':',@name)"/> -->
    <xsl:variable name="xpath">
      <xsl:choose>
        <xsl:when test="@type and not(contains($typeNS, 'xs'))">
<!--	  <xsl:value-of select="concat($currXPath,'/', $typeNS, ':', @name)"/>  -->  
	  <xsl:value-of select="concat($currXPath,'/', $NSprefix, ':', @name)"/>    
	</xsl:when>
	<xsl:otherwise>
<!--          <xsl:value-of select="concat($currXPath, '/', name(ancestor::xs:schema/namespace::*[string() = $NSprefix]), ':', @name)"/> -->
          <xsl:value-of select="concat($currXPath, '/', ancestor::schema/@prefix, ':', @name)"/>  
	</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <!-- Make sure that the element is not abstract -->
    <xsl:choose>
      <xsl:when test="not(@abstract)">
      	<!-- if the element is repeating, we need to create an xforms:repeat instead of an xforms:group when processing the type -->
      	<xsl:choose>
	  <xsl:when test="contains($typeNS, 'xs')">
            <!-- Documentation  -->
<!--            <xsl:for-each select=".//xs:documentation">
              <xsl:value-of select="normalize-space(.)"/>
              <xsl:value-of select="$newline"/>
	    </xsl:for-each> -->
	    <xsl:choose>
    	      <!--need to construct repeat containers for repeating elements with xs types-->
	      <xsl:when test="number($refMaxOccurs) > 1">
    	        <xsl:call-template name="BuildSimpleRepeat">
		  <xsl:with-param name="currXPath" select="$xpath"/>
		  <xsl:with-param name="elementName" select="$elementName"/>
		  <xsl:with-param name="label" select="$label"/>
		</xsl:call-template>
	      </xsl:when>
	      <xsl:when test="number($maxOccurs) > 1">
	        <xsl:call-template name="BuildSimpleRepeat">
		  <xsl:with-param name="currXPath" select="$xpath"/>
		  <xsl:with-param name="elementName" select="$elementName"/>
		  <xsl:with-param name="label" select="$label"/>
		</xsl:call-template>
	      </xsl:when>
	      <xsl:when test="$refMaxOccurs='unbounded'">
	        <xsl:call-template name="BuildSimpleRepeat">
		  <xsl:with-param name="currXPath" select="$xpath"/>
		  <xsl:with-param name="elementName" select="$elementName"/>
		  <xsl:with-param name="label" select="$label"/>
		</xsl:call-template>
	      </xsl:when>
	      <xsl:when test="$maxOccurs='unbounded'">
	        <xsl:call-template name="BuildSimpleRepeat">
		  <xsl:with-param name="currXPath" select="$xpath"/>
		  <xsl:with-param name="elementName" select="$elementName"/>
		  <xsl:with-param name="label" select="$label"/>
		</xsl:call-template>
	      </xsl:when>
	      <xsl:otherwise>
	        <xsl:call-template name="BuildSingleInputControl">
		  <xsl:with-param name="currXPath" select="$xpath"/>
		  <xsl:with-param name="label" select="$label"/>
		</xsl:call-template>
	      </xsl:otherwise>
	    </xsl:choose>
	  </xsl:when>
          <xsl:otherwise>
	    <!-- The type is named -->
	    <xsl:choose>
	      <xsl:when test="$typeName">
	      	<!-- process the type 
		  - complexTypes
		  - simpleType
		-->
		<!--<p><xsl:value-of select="@name"/><xsl:text>: </xsl:text><xsl:value-of select="$xpath"/></p>-->
		<!--<xsl:call-template name="Attributes" mode="buildUI">
		  <xsl:with-param name="currXPath" select="$xpath"/>
		  <xsl:with-param name="typeNS" select="$typeNS"/>
		  <xsl:with-param name="typeName" select="$typeName"/>
		  </xsl:call-template>-->
		<xsl:apply-templates select="exslt:node-set($local)//xs:schema/xs:complexType[@name=$typeName]" mode="buildUI">
		  <xsl:with-param name="currXPath" select="$xpath"/>
		  <xsl:with-param name="label" select="$label"/>
		  <xsl:with-param name="refMaxOccurs" select="$refMaxOccurs"/>
		  <xsl:with-param name="maxOccurs" select="$maxOccurs"/>
		</xsl:apply-templates>
	        <xsl:apply-templates select="exslt:node-set($externals)/schema[@prefix=$typeNS]/xs:schema/xs:complexType[@name=$typeName]" mode="buildUI">
	      	  <xsl:with-param name="currXPath" select="$xpath"/>
		  <xsl:with-param name="label" select="$label"/>
		  <xsl:with-param name="refMaxOccurs" select="$refMaxOccurs"/>
		  <xsl:with-param name="maxOccurs" select="$maxOccurs"/>
	        </xsl:apply-templates>
	        <xsl:apply-templates select="exslt:node-set($local)//xs:schema/xs:simpleType[@name=$typeName]" mode="buildUI">
	      	  <xsl:with-param name="currXPath" select="$xpath"/>
		  <xsl:with-param name="elementName" select="$elementName"/>
		  <xsl:with-param name="label" select="$label"/>
		  <xsl:with-param name="refMaxOccurs" select="$refMaxOccurs"/>
		  <xsl:with-param name="maxOccurs" select="$maxOccurs"/>
	        </xsl:apply-templates>
	        <xsl:apply-templates select="exslt:node-set($externals)/schema[@prefix=$typeNS]/xs:schema/xs:simpleType[@name=$typeName]" mode="buildUI">
		  <xsl:with-param name="currXPath" select="$xpath"/>
		  <xsl:with-param name="elementName" select="$elementName"/>
		  <xsl:with-param name="label" select="$label"/>
		  <xsl:with-param name="refMaxOccurs" select="$refMaxOccurs"/>
		  <xsl:with-param name="maxOccurs" select="$maxOccurs"/>
		</xsl:apply-templates>
	      </xsl:when>
	      <xsl:otherwise>
	        <xsl:apply-templates select="*" mode="buildUI">
		  <xsl:with-param name="currXPath" select="$xpath"/>
		  <xsl:with-param name="label" select="$label"/>
		</xsl:apply-templates>
	      </xsl:otherwise>
	    </xsl:choose>  
	  </xsl:otherwise>
	</xsl:choose>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="xs:restriction" mode="buildUI">
    <xsl:param name="currXPath"/>
    <xsl:param name="label"/>
    <xsl:variable name="typeNS" select="substring-before(@base,':')"/>
    <xsl:variable name="typeName" select="substring-after(@base,':')"/>
    <xsl:choose>
      <xsl:when test="*">
    	<!--do nothing now, only looking for empty restrictions-->
      </xsl:when>
      <xsl:otherwise>
        <!-- this template will only be applied as a result of an xs:restriction inside a complexType. A complexType cannot restrict a simpleType.
    	     The program will only look for complexTypes to process. -->
    	<xsl:apply-templates select="exslt:node-set($externals)/schema[@prefix=$typeNS]/xs:schema/xs:complexType[@name=$typeName]" mode="buildUI">
	  <xsl:with-param name="currXPath" select="$currXPath"/>
	  <xsl:with-param name="label" select="$label"/>
	  <xsl:with-param name="beingExtended">true</xsl:with-param>
    	</xsl:apply-templates>
	<xsl:apply-templates select="exslt:node-set($local)//xs:schema/xs:complexType[@name=$typeName]" mode="buildUl">
	  <xsl:with-param name="currXPath" select="$currXPath"/>
	  <xsl:with-param name="label" select="$label"/>
	  <xsl:with-param name="beingExtended">true</xsl:with-param>
	</xsl:apply-templates>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="*" mode="buildUI"/>

  <!-- Named templates-->

  <xsl:template name="Attributes">
    <xsl:param name="typeNS"/>
    <xsl:param name="typeName"/>
    <xsl:for-each select="exslt:node-set($local)//xs:schema/xs:complexType[@name=$typeName]//xs:attribute">
      <xsl:if test="not(@fixed)">
        <!-- If an attribute is fixed, there's no need to put it into the model -->
        <xsl:attribute name="{@name}">
        <xsl:call-template name="DefaultValue"/>
        </xsl:attribute>
      </xsl:if>
    </xsl:for-each>
    <xsl:for-each select="exslt:node-set($externals)/schema[@prefix=$typeNS]/xs:schema/xs:complexType[@name=$typeName]//xs:attribute">
      <xsl:if test="not(@fixed)">
        <xsl:attribute name="{@name}">
        <xsl:call-template name="DefaultValue"/>     
        </xsl:attribute>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="DefaultValue">
    <xsl:variable name="typeNS" select="substring-before(@type, ':')"/>
    <xsl:variable name="type" select="substring-after(@type, ':')"/>
      <xsl:choose>
        <!-- Default value -->
        <xsl:when test="@default">
          <xsl:value-of select="@default"/>
        </xsl:when>
        <xsl:otherwise>
          <!-- Base types -->
          <xsl:call-template name="DefaultBaseValue">
            <xsl:with-param name="type" select="$type"/>
          </xsl:call-template>
          <!-- Inline enumeration -->
          <xsl:if test="descendant::xs:enumeration">
            <xsl:value-of select="descendant::xs:enumeration/@value"/>
          </xsl:if>
          <!-- External simple type enumeration -->
          <xsl:for-each select="exslt:node-set($local)//xs:schema/xs:simpleType[@name=$type]">
            <xsl:value-of select="xs:restriction/xs:enumeration/@value"/>
          </xsl:for-each>
          <xsl:for-each select="exslt:node-set($externals)/schema[@prefix=$typeNS]/xs:schema/xs:simpleType[@name=$type]">
            <xsl:value-of select="xs:restriction/xs:enumeration/@value"/>
          </xsl:for-each>
          <!-- Inline extension -->
          <xsl:if test="descendant::xs:extension">
            <xsl:call-template name="DefaultBaseValue">
              <xsl:with-param name="type" select="substring-after(descendant::xs:extension[@base], ':')"/>
            </xsl:call-template>
          </xsl:if>
          <!-- External extension -->
          <xsl:for-each select="exslt:node-set($local)//xs:schema/xs:complexType[@name=$type]//xs:extension">
            <xsl:variable name="base" select="substring-after(@base, ':')"/>
            <xsl:apply-templates select="exslt:node-set($local)//xs:schema/xs:simpleType[@name = $base]" mode="buildInstance"/>
            <xsl:apply-templates select="exslt:node-set($externals)/schema[@prefix = $typeNS]/xs:schema/xs:simpleType[@name = $base]" mode="buildInstance"/>
            <xsl:call-template name="DefaultBaseValue">
              <xsl:with-param name="type" select="$base"/>
            </xsl:call-template>            
          </xsl:for-each>
          <xsl:for-each select="exslt:node-set($externals)/schema[@prefix=$typeNS]/xs:schema/xs:complexType[@name=$type]//xs:extension">
            <xsl:variable name="base" select="substring-after(@base, ':')"/>
            <xsl:apply-templates select="exslt:node-set($local)//xs:schema/xs:simpleType[@name = $base]" mode="buildInstance"/>
            <xsl:apply-templates select="exslt:node-set($externals)/schema[@prefix = $typeNS]/xs:schema/xs:simpleType[@name = $base]" mode="buildInstance"/>
            <xsl:call-template name="DefaultBaseValue">
              <xsl:with-param name="type" select="$base"/>
            </xsl:call-template>
          </xsl:for-each>
          <!-- IVOA specific value -->
          <xsl:if test="contains($type, 'IdentifierURI')">
            <xsl:text>ivo://x.x/</xsl:text>
          </xsl:if>
        </xsl:otherwise>
      </xsl:choose>
  </xsl:template>

  <xsl:template name="DefaultBaseValue">
    <xsl:param name="type"/>
    <xsl:if test="contains($type, 'string')">
      <xsl:text>NULL</xsl:text>
    </xsl:if>
    <xsl:if test="contains($type, 'float')">
      <xsl:text>0.0</xsl:text>
    </xsl:if>
    <xsl:if test="contains($type, 'int')">
      <xsl:text>0</xsl:text>
    </xsl:if>
    <xsl:if test="contains($type, 'boolean')">
      <xsl:text>true</xsl:text>
    </xsl:if>
    <xsl:if test="contains($type, 'date')">
      <xsl:text>1970-01-01+00:00</xsl:text>
    </xsl:if>
  </xsl:template>

  <xsl:template name="BuildSingleInputControl">
    <!--build an xforms:input control that maps (with ref or with nodeset?) to the currXPath, with the caption (and bound to the type?)-->
    <xsl:param name="currXPath"/>
    <xsl:param name="label"/>
    <xsl:param name="type"/>
    <xsl:element name="xforms:input">
      <xsl:attribute name="ref">
        <xsl:choose>
          <xsl:when test="contains($currXPath, './')">
            <xsl:value-of select="substring-after($currXPath, './')"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$currXPath"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>
      <xsl:element name="xforms:label">
        <xsl:element name="a">
          <xsl:attribute name="href">
	    <xsl:text>http://mercury.cacr.caltech.edu:8080/carnivore/glossary#</xsl:text>
	    <xsl:value-of select="$label"/>
	  </xsl:attribute>
          <xsl:element name="b">
            <xsl:value-of select="$label"/>
          </xsl:element>
        </xsl:element>
      </xsl:element>
    </xsl:element>
    <!-- Documentation  
    <xsl:for-each select=".//xs:documentation">
      <xsl:value-of select="normalize-space(.)"/>
    </xsl:for-each> 
    <xsl:element name="br"/> -->
  </xsl:template>

  <xsl:template name="BuildSimpleRepeat">
    <!--build a xforms:repeat container for simple xs type content-->
    <xsl:param name="currXPath"/>
    <xsl:param name="elementName"/>
    <xsl:param name="label"/>
    <xsl:variable name="repeatID" select="concat($currXPath,'Repeat')"/> 
    <xsl:element name="xforms:repeat">
      <xsl:attribute name="nodeset"><xsl:value-of select="$currXPath"/></xsl:attribute>
      <xsl:attribute name="id"><xsl:value-of select="$repeatID"/></xsl:attribute>
      <xsl:call-template name="BuildSingleInputControl">
<!--        <xsl:with-param name="currXPath" select="$currXPath"/> -->
        <xsl:with-param name="currXPath" select="'.'"/>
	<xsl:with-param name="label" select="$label"/>
      </xsl:call-template>
    </xsl:element>
    <xsl:call-template name="BuildRepeatButtons">
      <xsl:with-param name="currXPath" select="$currXPath"/>
      <xsl:with-param name="repeatID" select="$repeatID"/>
      <xsl:with-param name="label" select="$label"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="HackNamespaces">
    <!-- we have to get the namespaces used by the schemas into the body element. This is an ugly way to do it, following a suggestion made by Michael Kay on a newsgroup. You create a dummy attribute you don't want to use on the body, making sure that it's prefix is the one used by the schema. You also assign the dummy attribute to the correct namespace URI. This will force the transformer to put an xmlns: declaration in the body element for all your imported elements. In XSL 2.0, it is claimed, there will be an xsl:namespace tag, which will be a better solution to this problem. You can find Michael Kay's post here: http://sources.redhat.com/ml/xsl-list/2001-09/msg01204.html -->
    <xsl:attribute name="{concat($prefix,':sims')}" namespace="{$namespace}"/>
    <xsl:for-each select="exslt:node-set($externals)/schema">
      <xsl:attribute name="{concat(@prefix,':sims')}" namespace="{@urn}"/>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="BuildSelectControl">
    <xsl:param name="currXPath"/>
    <xsl:param name="label"/>
    <xsl:param name="currentNode"/>
    <xsl:param name="select"/>
    <xsl:element name="{concat('xforms:',$select)}">
      <xsl:attribute name="ref">
        <xsl:choose>
          <xsl:when test="contains($currXPath, './')">
            <xsl:value-of select="substring-after($currXPath, './')"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$currXPath"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>
<!--    <xsl:attribute name="ref">          
        <xsl:value-of select="$currXPath"/> 
      </xsl:attribute> -->
      <xsl:element name="xforms:label">
    	    <xsl:value-of select="$label"/>
      </xsl:element>
      <xsl:for-each select="$currentNode/xs:restriction/xs:enumeration">
    	<xsl:element name="xforms:item">
    	  <xsl:element name="xforms:label">
    	    <xsl:value-of select="@value"/>
    	  </xsl:element>
    	  <xsl:element name="xforms:value">
    	    <xsl:value-of select="@value"/>
    	  </xsl:element>
        </xsl:element>
      </xsl:for-each>
    </xsl:element>
  </xsl:template>

  <xsl:template name="BuildRepeatControl">
    <xsl:param name="currNode"/>
    <xsl:param name="currXPath"/>
    <xsl:param name="label"/>
    <xsl:variable name="repeatID" select="concat($currXPath,'Repeat')"/> 
    <xsl:element name="xforms:repeat">
      <xsl:attribute name="nodeset"><xsl:value-of select="$currXPath"/></xsl:attribute>
      <xsl:attribute name="id"><xsl:value-of select="$repeatID"/></xsl:attribute>
      <xsl:element name="xforms:label">
        <xsl:value-of select="$label"/>
      </xsl:element>
      <xsl:apply-templates select="$currNode/*" mode="buildUI">
<!--     	<xsl:with-param name="currXPath" select="$currXPath"/> -->
     	<xsl:with-param name="currXPath" select="'.'"/> 
    	    <xsl:with-param name="label" select="$label"/>
      </xsl:apply-templates>
    </xsl:element>
    <xsl:call-template name="BuildRepeatButtons">
      <xsl:with-param name="currXPath" select="$currXPath"/>
      <xsl:with-param name="repeatID" select="$repeatID"/>
      <xsl:with-param name="label" select="$label"/>
    </xsl:call-template>
  </xsl:template>

  <xsl:template name="BuildRepeatButtons">
    <xsl:param name="currXPath"/>
    <xsl:param name="repeatID"/>
    <xsl:param name="label"/>
    <xsl:element name="xforms:trigger" namespace="http://www.w3.org/2002/xforms">
      <xsl:element name="xforms:label">
    	<xsl:value-of select="concat('Insert New ',$label)"/>
      </xsl:element>
      <xsl:element name="xforms:action">
    	<xsl:attribute name="ev:event"><xsl:text>DOMActivate</xsl:text></xsl:attribute>
    	<xsl:element name="xforms:insert">
    	  <xsl:attribute name="nodeset"><xsl:value-of select="$currXPath"/></xsl:attribute>
    	  <xsl:attribute name="at"><xsl:text>xforms:index('</xsl:text><xsl:value-of select="$repeatID"/>')</xsl:attribute>
    	  <xsl:attribute name="position"><xsl:text>after</xsl:text></xsl:attribute>
    	</xsl:element>
      </xsl:element>
    </xsl:element>
    <xsl:element name="xforms:trigger" namespace="http://www.w3.org/2002/xforms">
      <xsl:element name="xforms:label">
    	<xsl:value-of select="concat('Delete ',$label)"/>
      </xsl:element>
      <xsl:element name="xforms:action">
        <xsl:attribute name="ev:event"><xsl:text>DOMActivate</xsl:text></xsl:attribute>
        <xsl:element name="xforms:delete">
    	  <xsl:attribute name="nodeset"><xsl:value-of select="$currXPath"/></xsl:attribute>
    	  <xsl:attribute name="at"><xsl:text>xforms:index('</xsl:text><xsl:value-of select="$repeatID"/><xsl:text>')</xsl:text></xsl:attribute>
<!--    	  <xsl:attribute name="position"><xsl:text>after</xsl:text></xsl:attribute> -->
    	</xsl:element>
      </xsl:element>
    </xsl:element>
  </xsl:template>

  <xsl:template name="BuildBooleanSelect">
    <xsl:param name="currXPath"/>
    <xsl:param name="label"/>
    <xsl:element name="xforms:select1">
      <xsl:attribute name="ref">
        <xsl:choose>
          <xsl:when test="contains($currXPath, './')">
            <xsl:value-of select="substring-after($currXPath, './')"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$currXPath"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>
<!--        <xsl:value-of select="$currXPath"/>
      </xsl:attribute> -->
      <xsl:element name="xforms:label"><xsl:value-of select="$label"/></xsl:element>
      <xsl:element name="xforms:choices">
        <xsl:element name="xforms:item">
    	  <xsl:element name="xforms:caption">Yes</xsl:element>
    	  <xsl:element name="xforms:value">true</xsl:element>
    	</xsl:element>
    	<xsl:element name="xforms:item">
	  <xsl:element name="xforms:caption">No</xsl:element>
    	  <xsl:element name="xforms:value">false</xsl:element>
        </xsl:element>					
      </xsl:element>
    </xsl:element>
  </xsl:template>

  <xsl:template name="RootElement">
    <xsl:param name="currXPath"/>
    <xsl:param name="name"/>
    <xsl:param name="type"/>
    <xsl:variable name="typeNS" select="substring-before($type, ':')"/>
    <xsl:variable name="typeName" select="substring-after($type, ':')"/>
    <xsl:variable name="elementName" select="$name"/>
    <xsl:variable name="NSprefix" select="ancestor::schema/@prefix"/>
    <xsl:variable name="label" select="$name"/>
    <!--    <xsl:variable name="label" select="xs:annotation/xs:appinfo/Label"/>
        <xsl:variable name="xpath" select="concat($currXPath,'/',ancestor::xs:schema/xs:annotation/xs:appinfo/Prefix,':',@name)"/> -->
    <xsl:variable name="xpath" select="$currXPath"/>
	<xsl:apply-templates select="exslt:node-set($local)//xs:schema/xs:complexType[@name=$typeName]" mode="buildUI">
      <xsl:with-param name="currXPath" select="$xpath"/>
	  <xsl:with-param name="label" select="$label"/>
	</xsl:apply-templates>
	<xsl:apply-templates select="exslt:node-set($externals)/schema[@prefix=$typeNS]/xs:schema/xs:complexType[@name=$typeName]" mode="buildUI">
	  <xsl:with-param name="currXPath" select="$xpath"/>
	  <xsl:with-param name="label" select="$label"/>
    </xsl:apply-templates>
  </xsl:template>

</xsl:stylesheet>
