<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
	xmlns:xs="http://www.w3.org/2001/XMLSchema"
	xmlns:frm="http://faculty.washington.edu/cliffsch/xmlcert-2/2003/winter/project">
<xsl:output method="xml" encoding="UTF-16"/>

<xsl:template match="/">
  <html xmlns="http://www.w3.org/1999/xhtml" >
    <body style="background-color:#FFFFFF;padding-top:5px;padding-left:10px;">
      <xsl:apply-templates/>
    </body>
  </html>
</xsl:template>

<xsl:param name="targetNUN">element::alert</xsl:param>

<xsl:template match="xs:schema">
  <xsl:variable name="NUNModified" select="concat($targetNUN,'/')"/>
  <xsl:variable name="NUNToken" 
       select="substring-after(substring-before($NUNModified,'/'),'element::')"/>
  <xsl:apply-templates select="xs:element[@name=$NUNToken]">
    <xsl:with-param name="NUNRemain" select="substring-after($NUNModified,'/')"/>
  </xsl:apply-templates>

</xsl:template>

<xsl:template match="xs:element[@name]">
  <xsl:param name="NUNRemain"/>
  <xsl:choose>
    <xsl:when test="string-length($NUNRemain)=0">
      <xsl:apply-templates select="." mode="targetElementForm"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:apply-templates select="xs:complexType/*">
        <xsl:with-param name="NUNRemainder" 
                        select="substring-after($NUNRemain,'/')"/>
        <xsl:with-param name="NUNToken" 
             select="substring-after(substring-before($NUNRemain,'/')
                     ,'element::')"/>
      </xsl:apply-templates>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>	

<xsl:template match="xs:sequence|xs:choice|xs:all">
  <xsl:param name="NUNRemain"/>
  <xsl:param name="NUNToken"/>
  <xsl:apply-templates 
       select="xs:sequence|xs:choice|xs:all|xs:element[@name=$NUNToken]">
    <xsl:with-param name="NUNRemain" select="$NUNRemain"/>
    <xsl:with-param name="NUNToken" select="$NUNToken"/>
  </xsl:apply-templates>
</xsl:template>

<xsl:template match="*"/>


<!-- TEMPLATE BUILDS END OF FORM TABLE AND CREATES SUBMIT BUTTON -->

<xsl:template match="xs:element[@name]" mode="targetElementForm">
  <b>Film DB <xsl:value-of select="xs:annotation/xs:appinfo/frm:label"/></b><br/>
  <xsl:copy-of select="xs:annotation/xs:documentation/node()"/>

  <form>
    <input type="hidden" name="%%elementNUN" 
     value="{/xs:schema/@targetNamespace}#{$targetNUN}"/>
    <table align="left" border="0" cellpadding="1" cellspacing="3" valign="top" style="">
      <xsl:apply-templates select="*|/xs:schema/xs:complexType[@name=current()/@type]"
                           mode="findChildNodes"/>
      <tr><td align="left" colspan="2"><input type="submit" value=" SAVE FORM " style="background-color:silver;color:#330000;font-face:arial-black;font-size:8pt;letter-spacing:1pt;valign:middle;width:120px"/></td></tr>
    </table>
  </form>
</xsl:template>

<xsl:template match="*" mode="targetElement"/>


<xsl:template match="xs:complexType|xs:complexContent|
                     xs:sequence|xs:all|xs:group[@name]"
              mode="findChildNodes">
  <xsl:apply-templates select="*" mode="findChildNodes"/>
</xsl:template>


<xsl:template match="xs:group[@ref]" mode="findChildNodes">
  <xsl:apply-templates select="/xs:schema/xs:group[@name=current()/@ref]" 
                       mode="findChildNodes"/>
</xsl:template>

<xsl:template match="xs:element[@ref]" mode="findChildNodes">
  <xsl:apply-templates select="/xs:schema/xs:element[@name=current()/@ref]" 
                       mode="findChildNodes"/>
</xsl:template>


<xsl:template match="xs:extension" mode="findChildNodes">
  <xsl:apply-templates select="/xs:schema/xs:complexType[@name=current()/@base]" 
                       mode="findChildNodes"/>
  <xsl:apply-templates select="*" mode="findChildNodes"/>
</xsl:template>


<!-- TEMPLATE BEGINS BUILDING FORM AND TABLE ROWS -->

<xsl:template match="xs:element[@name]" mode="findChildNodes">
  <xsl:if test="not(xs:complexType|
                /xs:schema/xs:complexType[@name=current()/@type]|
                xs:annotation/xs:appinfo/frm:readonly)">
    <tr>
      <td style="background-color:#FFFFCC">
      	<div style="color:#333300;font-family:franklin gothic,arial,helvetica;font-size:10pt;font-weight:bold;letter-spacing:1px;padding-left:12px;width:125px">
        <xsl:choose>
          <xsl:when test="xs:annotation/xs:appinfo/frm:label">	
            <xsl:value-of select="xs:annotation/xs:appinfo/frm:label"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="@name"/>
          </xsl:otherwise>	
        </xsl:choose>
        </div>
      </td>
      <td style="background-color:#FFCC66">
        <xsl:apply-templates select="." mode="childNodeInput">
          <xsl:with-param name="nodeName" select="@name"/>
        </xsl:apply-templates>
      </td>
    </tr>

  </xsl:if>
  <xsl:if test="xs:complexType">
  <tr>
  		<td style="background-color:#FFFF99">
  		<div style="color:#333300;font-family:franklin gothic,arial,helvetica;font-size:10pt;font-weight:bold;letter-spacing:1px;padding-left:12px;width:125px">
  			<xsl:for-each select=".">
  				<xsl:element name="{name()}">
  					<xsl:value-of select="@name" />		
  				</xsl:element>
  			</xsl:for-each>
  			</div>
  		</td>
  		<td style="background-color:#CCCC99">
  			<div style="background-color:orange;color:#333300;font-family:franklin gothic,arial,helvetica;font-size:10pt;font-weight:bold;letter-spacing:1px;padding-left:12px;"><xsl:apply-templates select="."><xsl:with-param name="nodeName" select="@name"/></xsl:apply-templates></div>
      </td>
  	</tr>
  </xsl:if>
</xsl:template>

<xsl:template match="option">
	<xsl:value-of select="." />
</xsl:template>


<xsl:template match="*" mode="findChildNodes"/>



<xsl:template match="xs:element[@type]" mode="childNodeInput" priority="0">
  <xsl:param name="nodeName"/>
  <xsl:param name="nodeValue" select="@default"/>
  <xsl:apply-templates 
       select="/xs:schema/xs:simpleType[@name=current()/@type]/xs:restriction"
       mode="childNodeInput">
    <xsl:with-param name="nodeName" select="$nodeName"/>
    <xsl:with-param name="nodeValue" select="$nodeValue"/>
  </xsl:apply-templates>
</xsl:template>

<xsl:template match="xs:element[xs:simpleType]" mode="childNodeInput">
  <xsl:param name="nodeName"/>
  <xsl:param name="nodeValue" select="@default"/>
   <xsl:apply-templates select="xs:simpleType/xs:restriction" mode="childNodeInput">
    <xsl:with-param name="nodeName" select="$nodeName"/>
    <xsl:with-param name="nodeValue" select="$nodeValue"/>
  </xsl:apply-templates>
</xsl:template>



<xsl:template match="xs:element[@type='xs:string']|xs:restriction[@base='xs:string']"
              mode="childNodeInput">
  <xsl:param name="nodeName"/>
  <xsl:param name="nodeValue" select="@default"/>
    <xsl:choose>
      <xsl:when test="xs:maxLength">
        <input type="text" name="{$nodeName}" value="{$nodeValue}">
          <xsl:apply-templates select="*" mode="childNodeInput"/>
        </input>					
      </xsl:when>
      <xsl:otherwise>
        <textArea name="{$nodeName}">
          <xsl:apply-templates select="*" mode="childNodeInput"/>
          <xsl:value-of select="$nodeValue"/>
        </textArea>
      </xsl:otherwise>
    </xsl:choose>
</xsl:template>	

<xsl:template match="xs:element[@type='xs:boolean']|xs:restriction[@base='xs:boolean']"
              mode="childNodeInput">
  <xsl:param name="nodeName"/>
  <xsl:param name="nodeValue" select="@default"/>
  <input type="radio" name="{$nodeName}" value="true">
    <xsl:if test="$nodeValue='true'">
      <xsl:attribute name="checked">checked</xsl:attribute>
    </xsl:if>
    Yes
  </input>
  <input type="radio" name="{$nodeName}" value="false">
    <xsl:if test="$nodeValue='false'">
      <xsl:attribute name="checked">checked</xsl:attribute>
    </xsl:if>
    No
  </input>
</xsl:template>



<xsl:template match="xs:maxLength" mode="childNodeInput">
  <xsl:attribute name="maxLength">
    <xsl:value-of select="@value"/>
  </xsl:attribute>
</xsl:template>

<xsl:template match="xs:pattern" mode="childNodeInput">
  <xsl:attribute name="validationRegExp">
    <xsl:value-of select="@value"/>
  </xsl:attribute>
</xsl:template>


<xsl:template match="xs:restriction[xs:enumeration]" mode="childNodeInput" 
              priority="1">
  <xsl:param name="nodeName"/>
  <xsl:param name="nodeValue"/>
  <select name="{$nodeName}">
    <xsl:apply-templates select="*" mode="childNodeInput">
      <xsl:with-param name="nodeValue" select="$nodeValue"/>
    </xsl:apply-templates>
  </select>
</xsl:template>

<xsl:template match="xs:enumeration" mode="childNodeInput">
  <xsl:param name="nodeValue"/>
  <option value="{@value}">
    <xsl:if test="@value=$nodeValue">
      <xsl:attribute name="selected">
        selected
      </xsl:attribute>
    </xsl:if>
    <xsl:choose>
      <xsl:when test="xs:annotation/xs:documentation">
        <xsl:value-of select="xs:annotation/xs:documentation"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="@value"/>
      </xsl:otherwise>
    </xsl:choose>
  </option>
</xsl:template>

<xsl:template match="*" mode="childNodeInput"/>


</xsl:stylesheet>