<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                exclude-result-prefixes="msxsl">
   <xsl:output method="xml" encoding="UTF-8" omit-xml-declaration="yes" indent="no" version="1.0" standalone="yes" />
   <xsl:variable name="lowercase">abcdefghijklmnopqrstuvwxyz</xsl:variable>
   <xsl:variable name="uppercase">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>

   <xsl:template match="@* | node()">
      <xsl:copy>
         <xsl:apply-templates select="@* | node()" />
      </xsl:copy>
   </xsl:template>

   <xsl:template match="propDefn" />

   <xsl:template match="assembly">
      <xsl:copy>
         <xsl:apply-templates select="@id" />
         <xsl:apply-templates select="@qty" />
         <xsl:apply-templates select="prop">
            <xsl:sort select="translate(@id, $uppercase, $lowercase)" />
         </xsl:apply-templates>
         <xsl:apply-templates select="assembly" />
         <xsl:apply-templates select="material">
            <xsl:sort select="translate(@id, $uppercase, $lowercase)" />
         </xsl:apply-templates>
         <xsl:apply-templates select="operation" />
      </xsl:copy>
   </xsl:template>

   <xsl:template match="material">
      <xsl:copy>
         <xsl:apply-templates select="@*" />
         <xsl:apply-templates select="prop">
            <xsl:sort select="translate(@id, $uppercase, $lowercase)" />
         </xsl:apply-templates>
         <xsl:apply-templates select="*[not(self::prop)]" />
      </xsl:copy>
   </xsl:template>
   
   <xsl:template match="operation">
      <xsl:copy>
         <xsl:apply-templates select="@id" />
         <xsl:apply-templates select="@qty" />
         <xsl:apply-templates select="prop">
            <xsl:sort select="translate(@id, $uppercase, $lowercase)" />
         </xsl:apply-templates>
         <xsl:apply-templates select="*[not(self::prop)]" />
      </xsl:copy>
   </xsl:template>

   <xsl:template match="assembly/material">
      <xsl:copy>
         <xsl:apply-templates select="@id" />
         <xsl:apply-templates select="@qty" />
         <xsl:apply-templates select="prop">
            <xsl:sort select="translate(@id, $uppercase, $lowercase)" />
         </xsl:apply-templates>
      </xsl:copy>
   </xsl:template>

   <xsl:template match="assembly/prop">
      <xsl:variable name="testName" select="translate(@id, $uppercase, $lowercase)" />
      <xsl:if test="$testName='erp_itemno'">
         <xsl:copy>
            <xsl:apply-templates select="@*" />
         </xsl:copy>
      </xsl:if>
      <xsl:if test="$testName='erp_configid'">
         <xsl:copy>
            <xsl:apply-templates select="@*" />
         </xsl:copy>
      </xsl:if>
      <xsl:if test="$testName='erp_uom'">
         <xsl:copy>
            <xsl:apply-templates select="@*" />
         </xsl:copy>
      </xsl:if>
      <xsl:if test="$testName='erp_bomname'">
         <xsl:copy>
            <xsl:apply-templates select="@*" />
         </xsl:copy>
      </xsl:if>
   </xsl:template>

   <xsl:template match="material/prop">
      <xsl:variable name="testName" select="translate(@id, $uppercase, $lowercase)" />
      <xsl:if test="$testName='erp_uom'">
         <xsl:copy>
            <xsl:apply-templates select="@*" />
         </xsl:copy>
      </xsl:if>
	  <xsl:if test="$testName='linetype'">
         <xsl:copy>
            <xsl:apply-templates select="@*" />
         </xsl:copy>
      </xsl:if>
	  <xsl:if test="$testName='consumptionwarehouseid'">
         <xsl:copy>
            <xsl:apply-templates select="@*" />
         </xsl:copy>
      </xsl:if>
   </xsl:template>

   <xsl:template match="@rid" />

   <xsl:template match="assembly/@name|material/@name|operation/@name" />

   <xsl:template match="@id|@catid">
      <xsl:attribute name="{local-name()}">
         <xsl:value-of select="translate(., $uppercase, $lowercase)" />
      </xsl:attribute>
   </xsl:template>
</xsl:stylesheet>
