<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

   <xsl:output method="xml" omit-xml-declaration="yes" indent="no" />
   
   <xsl:template match="/">
      <xsl:apply-templates select="Filter" />
   </xsl:template>
   
   <xsl:template match="Filter">   
      <Filter xmlns:pwa="http://microsoft.com/ProjectServer/FilterSchema.xsd">
         <Criteria>
            <xsl:choose>
               <xsl:when test="count(LogicalOperator) = 0">
                  <LogicalOperator logicalOperationType="Nop">
                     <xsl:call-template name="BuildFieldOperator">
                        <xsl:with-param name="pContextNode" select="FieldComparison" />
                     </xsl:call-template>
                  </LogicalOperator>
               </xsl:when>
               <xsl:otherwise>
                  <xsl:apply-templates select="LogicalOperator" />
               </xsl:otherwise>
            </xsl:choose>
         </Criteria>
      </Filter>
   </xsl:template>
   
   <xsl:template match="LogicalOperator">
      <LogicalOperator>
         <xsl:attribute name="logicalOperationType">
            <xsl:value-of select="@Type" />
         </xsl:attribute>
         <xsl:apply-templates select="LogicalOperator" />
         <xsl:apply-templates select="FieldComparison" />
      </LogicalOperator>   
   </xsl:template>
   
   <xsl:template match="FieldComparison">
      <xsl:call-template name="BuildFieldOperator">
         <xsl:with-param name="pContextNode" select="." />
      </xsl:call-template>
  </xsl:template>
  
   <xsl:template name="BuildFieldOperator">
      <xsl:param name="pContextNode" />
      
      <FieldOperator>
         <xsl:attribute name="fieldOperationType">
            <xsl:call-template name="TranslateOperator">
               <xsl:with-param name="pOperator" select="$pContextNode/Operator" />
            </xsl:call-template>
         </xsl:attribute>
         <Field>

            <xsl:attribute name="fieldName">
               <xsl:value-of select="$pContextNode/Field" />
            </xsl:attribute>
         </Field>
         <Operand>

                  <xsl:message terminate="no">
                     <xsl:text>x: </xsl:text><xsl:value-of select="$pContextNode/Operand" />
                  </xsl:message>

            <xsl:choose>
               <!--
               This section basically works like a Join() function using a comma to delimit
               the joined items.
               -->
               <xsl:when test="count($pContextNode/Operand) > 1">
                  <xsl:message terminate="no">
                     <xsl:text>a: </xsl:text>
                  </xsl:message>
                  <xsl:for-each select="$pContextNode/Operand">
                     <xsl:value-of select="." />
                     <xsl:if test="position() != last()">
                        <xsl:text>,</xsl:text>
                     </xsl:if>
                  </xsl:for-each>
               </xsl:when>
               <xsl:otherwise>
                  <xsl:message terminate="no">
                     <xsl:text>b: </xsl:text><xsl:value-of select="$pContextNode/Operand" />
                  </xsl:message>
                  <xsl:call-template name="FormatOperand">
                     <xsl:with-param name="pOperand" select="$pContextNode/Operand" />
                     <xsl:with-param name="pOperator" select="$pContextNode/Operator" />
                  </xsl:call-template>
               </xsl:otherwise>
            </xsl:choose>
         </Operand>
      </FieldOperator>
   </xsl:template>   

   <!-- Utility Templates -->

   <xsl:template name="FormatOperand">
      <xsl:param name="pOperand" />
      <xsl:param name="pOperator" />
            
      <!--
      RobXML treats 'BeginsWith' and 'Contains' as different flavors of the 'Like' operator,
      so we need to add in the '%' character(s) for these operators coming from BenXML
      -->
      <xsl:choose>
         <xsl:when test="$pOperator = 'BeginsWith'">
            <xsl:value-of select="$pOperand" /><xsl:text>%</xsl:text>
         </xsl:when>
         <xsl:when test="$pOperator = 'Contains'">
            <xsl:text>%</xsl:text><xsl:value-of select="$pOperand" /><xsl:text>%</xsl:text>
         </xsl:when>
         <xsl:otherwise>
            <xsl:value-of select="$pOperand" />
         </xsl:otherwise>      
      </xsl:choose>
      
   </xsl:template>

   <xsl:template name="TranslateOperator">
      <xsl:param name="pOperator" />

      <xsl:choose>
         <xsl:when test="$pOperator='Equals'">
            <xsl:text>Equal</xsl:text>
         </xsl:when>

         <xsl:when test="$pOperator='NotEqual'">
            <xsl:text>NotEqual</xsl:text> <!-- same as P12 -->
         </xsl:when>

         <!-- SV only -->
         <xsl:when test="$pOperator='LessThan'">
            <xsl:text>LessThan</xsl:text> <!-- same as P12 -->
         </xsl:when>

         <!-- SV only -->
         <xsl:when test="$pOperator='LessThanOrEqual'">
            <xsl:text>LessThanEqual</xsl:text>
         </xsl:when>

         <!-- SV only -->
         <xsl:when test="$pOperator='GreaterThan'">
            <xsl:text>GreaterThan</xsl:text> <!-- same as P12 -->
         </xsl:when>

         <!-- SV only -->
         <xsl:when test="$pOperator='GreaterThanOrEqual'">
            <xsl:text>GreaterThanEqual</xsl:text>
         </xsl:when>

         <!-- SV only; String only -->
         <xsl:when test="$pOperator='BeginsWith'">
            <xsl:text>Like</xsl:text>
         </xsl:when>

         <!-- String only -->
         <xsl:when test="$pOperator='Contains'">
            <xsl:text>Like</xsl:text>
         </xsl:when>

         <!-- String only -->
         <xsl:when test="$pOperator='Excludes'">
            <xsl:text>NotContain</xsl:text>
         </xsl:when>

         <!-- NEW FOR P14 : MV only -->
         <xsl:when test="$pOperator='ContainsValues'">
            <xsl:text>Contain</xsl:text> <!-- AKA IsSupersetOf -->
         </xsl:when>

         <!-- NEW FOR P14 : MV only -->
         <xsl:when test="$pOperator='ExcludesValues'">ExcludesValues
            <xsl:text>NotContain_MULTI_VALUES</xsl:text> <!-- AKA IsNotSupersetOf -->
         </xsl:when>
      </xsl:choose>
   </xsl:template>
         
</xsl:stylesheet>
