<?xml version="1.0"?> 

<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
		<xsl:apply-templates/> 
  </xsl:template>


<xsl:template match="uos">
  <schema_instance express_schema_name="{@schema}">
   <xsl:apply-templates/>
  </schema_instance>
   </xsl:template>

<xsl:template match="iso_10303_28_header|data_section_header|documentation|purpose|time_stamp|document_name|author|originating_organization|authorization|originating_system|preprocessor_version">
	<xsl:copy>
		<xsl:apply-templates/>
	</xsl:copy>
</xsl:template>

<xsl:template match="express_schema|external_refid|schema_decl">
	<xsl:copy>
		<xsl:apply-templates select="@*"/>
		<xsl:apply-templates/> 		
	</xsl:copy>
</xsl:template>

<xsl:template match="iso_10303_28">
	<iso_10303_28>
		<xsl:attribute name="version">PDTS</xsl:attribute>
		<xsl:attribute name="representation_category">LB</xsl:attribute>
		<xsl:apply-templates/> 		
	</iso_10303_28>
</xsl:template>

<xsl:template match="express_data">
	<express_data>
		<xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
		<xsl:attribute name="representation_category">LB</xsl:attribute>
		<xsl:apply-templates/> 		
	</express_data>
</xsl:template>

<xsl:template match="external_refid/@id|external_refid/@xlink:type|external_refid/@xlink:href|external_refid/@xlink:arcrole|external_refid/@xlink:title|external_refid/@xlink:role|external_refid/@xlink:show|external_refid/@xlink:actuate">
	<xsl:copy/>
</xsl:template>

<xsl:template match="express_schema/@id|express_schema/@express_schema_description|express_schema/@express_schema_identifier|express_schema/@express_schema_version">
	<xsl:copy/>
</xsl:template>

<xsl:template name="process_ctn">
 <xsl:param name="lbtype"/>
 <xsl:param name="deftype">none</xsl:param>
 <xsl:param name="stext"/>
 <xsl:variable name="ntext" select="concat(normalize-space($stext),' ')"/>
  <xsl:choose>
   <xsl:when test="$lbtype='entity_instance_ref'">
     <xsl:variable name="pid" select="substring-before($ntext,' ')"/>
     <xsl:choose>
      <xsl:when test="local-name(id($pid))='unset'">
	 <unset></unset>
      </xsl:when>
      <xsl:otherwise>	
       <entity_instance_ref>
	  <xsl:attribute name="refid">
	   <xsl:value-of select="$pid"/>
	  </xsl:attribute>
	 </entity_instance_ref>
      </xsl:otherwise>
     </xsl:choose>      
   </xsl:when>
   <xsl:when test="$lbtype='string_literal' or $lbtype='binary_literal'">
    <xsl:choose>
      <xsl:when test="$deftype='none'">
         <xsl:element name="{$lbtype}">
           <xsl:value-of select="id(substring-before($ntext,' '))"/>
         </xsl:element>
      </xsl:when>
      <xsl:otherwise>
       <xsl:element name="type_literal">
	  <xsl:attribute name="express_type_name"><xsl:value-of select="$deftype"/></xsl:attribute>
         <xsl:element name="{$lbtype}">
          <xsl:value-of select="id(substring-before($ntext,' '))"/>
         </xsl:element>
        </xsl:element>
       </xsl:otherwise>
      </xsl:choose>
   </xsl:when> 
   <xsl:when test="$lbtype='select'">
        <xsl:apply-templates select="id(substring-before($ntext,' '))" mode="xselect"/>	
   </xsl:when> 
   <xsl:otherwise>
     <xsl:choose>
      <xsl:when test="$deftype='none'">
         <xsl:element name="{$lbtype}">
			<xsl:value-of select="substring-before($ntext,' ')"/>
         </xsl:element>
      </xsl:when>
      <xsl:otherwise>
       <xsl:element name="type_literal">
	 <xsl:attribute name="express_type_name"><xsl:value-of select="$deftype"/></xsl:attribute>
         <xsl:element name="{$lbtype}">
			<xsl:value-of select="substring-before($ntext,' ')"/>
         </xsl:element>
       </xsl:element>
      </xsl:otherwise>
     </xsl:choose>
   </xsl:otherwise> 
  </xsl:choose>
  <xsl:if test="substring-after($ntext,' ')">
    <xsl:call-template name="process_ctn">
      <xsl:with-param name="lbtype"><xsl:value-of select="$lbtype"/></xsl:with-param>

 	<xsl:with-param name="deftype"><xsl:value-of select="$deftype"/></xsl:with-param>
	<xsl:with-param name="stext" select="substring-after($ntext,' ')"/>
	</xsl:call-template>
  </xsl:if>
</xsl:template>

<xsl:template name="process_nested_ctn">
 <xsl:param name="inner_aggs"/>
 <xsl:param name="lbtype"/>
 <xsl:param name="deftype">none</xsl:param>
 <xsl:param name="stext"/>
 <xsl:variable name="naggs" select="concat(normalize-space($inner_aggs),' ')"/>
 <xsl:variable name="ntext" select="concat(normalize-space($stext),' ')"/>
 	<xsl:choose>
	   <xsl:when test="substring-after($naggs,' ')">
              <xsl:element name="{substring-before($naggs,' ')}">
   		   <xsl:call-template name="process_nested_ctn">
		      <xsl:with-param name="lbtype"><xsl:value-of select="$lbtype"/></xsl:with-param>
			<xsl:with-param name="inner_aggs"><xsl:value-of select="substring-after($naggs,' ')"/></xsl:with-param>
		      <xsl:with-param name="stext"><xsl:value-of select="id(substring-before($ntext,' '))/@c"/></xsl:with-param>
		   </xsl:call-template>			 
              </xsl:element>
		</xsl:when>
		<xsl:otherwise>
             <xsl:element name="{substring-before($naggs,' ')}">
              <xsl:variable name="ctntext" select="substring-before($ntext,' ')"/>
 		  <xsl:call-template name="process_ctn">
                <xsl:with-param name="lbtype"><xsl:value-of select="$lbtype"/></xsl:with-param>
 	          <xsl:with-param name="deftype"><xsl:value-of select="$deftype"/></xsl:with-param>
	          <xsl:with-param name="stext" select="id($ctntext)/@c"/>
		  </xsl:call-template>
             </xsl:element>
	   </xsl:otherwise>
      </xsl:choose>
      <xsl:if test="substring-after($ntext,' ')">
  		   <xsl:call-template name="process_nested_ctn">
		      <xsl:with-param name="lbtype"><xsl:value-of select="$lbtype"/></xsl:with-param>
			<xsl:with-param name="inner_aggs"><xsl:value-of select="$inner_aggs"/></xsl:with-param>
		      <xsl:with-param name="stext"><xsl:value-of select="substring-after($ntext,' ')"/></xsl:with-param>
		   </xsl:call-template>			 
      </xsl:if>			              			
</xsl:template>

<xsl:template match="*">
</xsl:template>