<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <xsl:output method="xml" encoding="UTF-8" />
  <!-- author: Jeongnim Kim
       convert xml files from ohmms version 0.2.3 to 0.2.3.1 -->
  <xsl:template match="/Main">
    <simulation>
      <xsl:attribute name="xsi:noNamespaceSchemaLocation">ohmms_dtd.xsd</xsl:attribute>
      <xsl:for-each select="Jobs">
        <xsl:variable name="V2" select="." />
          <xsl:for-each select="@file">
            <xsl:attribute name="src">
              <xsl:value-of select="." />
            </xsl:attribute>
          </xsl:for-each>
          <xsl:for-each select="Project">
            <xsl:call-template name="ohmms.project" />
          </xsl:for-each>
          <xsl:for-each select="Random">
            <xsl:call-template name="ohmms.random" />
          </xsl:for-each>
          <xsl:for-each select="random">
            <xsl:call-template name="ohmms.random" />
          </xsl:for-each>
        <process>
          <xsl:for-each select="ParticleSets">
            <xsl:call-template name="ohmms.particlesets" />
          </xsl:for-each>
          <xsl:for-each select="TransformParticle">
            <xsl:call-template name="ohmms.transform" />
          </xsl:for-each>
          <xsl:for-each select="MDSim">
            <mdsim>
              <xsl:for-each select="ParticleSets">
                <xsl:call-template name="ohmms.particlesets" />
              </xsl:for-each>
              <xsl:for-each select="Potential">
                <xsl:call-template name="ohmms.potential" />
              </xsl:for-each>
              <xsl:for-each select="potential">
                <xsl:call-template name="ohmms.potential" />
              </xsl:for-each>
              <xsl:for-each select="Ensemble">
                <xsl:call-template name="ohmms.ensemble" />
              </xsl:for-each>
              <xsl:for-each select="ensemble">
                <xsl:call-template name="ohmms.ensemble" />
              </xsl:for-each>
              <xsl:for-each select="record">
                <xsl:call-template name="ohmms.record" />
              </xsl:for-each>
              <xsl:for-each select="Record">
                <xsl:call-template name="ohmms.record" />
              </xsl:for-each>
              <xsl:for-each select="Loops">
                <xsl:call-template name="ohmms.loop" />
              </xsl:for-each>
            </mdsim>
          </xsl:for-each>
        </process>
      </xsl:for-each>
    </simulation>
  </xsl:template>

  <!-- change from PARAMETER  to parameter -->
  <xsl:template name="ohmms.parameter">
    <parameter>
      <xsl:for-each select="@name">
        <xsl:attribute name="name">
           <xsl:choose>
              <xsl:when test=". = 'unitcell_grid'">
                <xsl:text>uc_grid</xsl:text>
              </xsl:when>
             <xsl:otherwise>
                <xsl:value-of select="." />
             </xsl:otherwise>
           </xsl:choose>
        </xsl:attribute>
      </xsl:for-each>
      <xsl:for-each select="@Unit">
        <xsl:attribute name="condition">
          <xsl:value-of select="." />
        </xsl:attribute>
      </xsl:for-each>
      <xsl:value-of select="." />
    </parameter>
  </xsl:template>

  <!-- change for attrib -->
  <xsl:template name="ohmms.attrib">
    <attrib>
      <xsl:for-each select="@name">
        <xsl:attribute name="name">
           <xsl:choose>
              <xsl:when test=". = 'positions'">
                <xsl:text>position</xsl:text>
              </xsl:when>
              <xsl:when test=". = 'trajectory'">
                <xsl:text>position</xsl:text>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="." />
             </xsl:otherwise>
           </xsl:choose>
        </xsl:attribute>
      </xsl:for-each>
      <xsl:for-each select="@type">
        <xsl:attribute name="datatype">
          <xsl:choose>
            <xsl:when test=". = 'PositionType'">
              <xsl:text>posArray</xsl:text>
            </xsl:when>
            <xsl:when test=". = 'ScalarType'">
              <xsl:text>scalarArray</xsl:text>
            </xsl:when>
            <xsl:when test=". = 'IndexType'">
              <xsl:text>indexArray</xsl:text>
            </xsl:when>
            <xsl:when test=". = 'StringType'">
              <xsl:text>stringArray</xsl:text>
            </xsl:when>
            <xsl:when test=". = 'TensorType'">
              <xsl:text>tensorArray</xsl:text>
            </xsl:when>
            <xsl:when test=". = 'XmolType'">
              <xsl:text>xmolArray</xsl:text>
            </xsl:when>
            <xsl:otherwise>
              <xsl:text>unknown</xsl:text>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:attribute>
      </xsl:for-each>
      <xsl:for-each select="@inunit">
        <xsl:attribute name="condition">
          <xsl:value-of select="." />
        </xsl:attribute>
      </xsl:for-each>
      <xsl:value-of select="." />
    </attrib>
  </xsl:template>

  <!-- change for record -->
  <xsl:template name="ohmms.record">
    <record>
      <xsl:for-each select="@name">
        <xsl:attribute name="name">
           <xsl:value-of select="." />
        </xsl:attribute>
      </xsl:for-each>
      <xsl:for-each select="@format">
        <xsl:attribute name="format">
          <xsl:value-of select="." />
        </xsl:attribute>
      </xsl:for-each>
      <xsl:for-each select="@stride">
        <xsl:attribute name="stride">
          <xsl:value-of select="." />
        </xsl:attribute>
      </xsl:for-each>
      <xsl:for-each select="@type">
        <xsl:attribute name="condition">
        <xsl:choose>
          <xsl:when test=". = 'restart'">
            <xsl:text>overwrite</xsl:text>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text>append</xsl:text>
          </xsl:otherwise>
        </xsl:choose>
        </xsl:attribute>
      </xsl:for-each>
      <xsl:for-each select="attrib">
        <xsl:call-template name="ohmms.attrib" />
      </xsl:for-each>
    </record>
  </xsl:template>

  <!-- change for ensemble -->
  <xsl:template name="ohmms.ensemble">
    <ensemble>
      <xsl:for-each select="@name">
        <xsl:attribute name="name">
          <xsl:value-of select="." />
        </xsl:attribute>
      </xsl:for-each>
      <xsl:for-each select="@method">
        <xsl:attribute name="method">
          <xsl:value-of select="." />
        </xsl:attribute>
      </xsl:for-each>
      <xsl:for-each select="PARAMETER">
        <xsl:call-template name="ohmms.parameter" />
      </xsl:for-each>
    </ensemble>
  </xsl:template>

  <!-- change for loop -->
  <xsl:template name="ohmms.loop">
    <loop>
      <xsl:for-each select="@blocks">
        <xsl:attribute name="blocks">
          <xsl:value-of select="." />
        </xsl:attribute>
      </xsl:for-each>
      <xsl:for-each select="@steps">
        <xsl:attribute name="steps">
          <xsl:value-of select="." />
        </xsl:attribute>
      </xsl:for-each>
      <xsl:for-each select="Record">
        <xsl:call-template name="ohmms.record" />
      </xsl:for-each>
    </loop>
  </xsl:template>

  <!-- change for transform -->
  <xsl:template name="ohmms.transform">
    <transform>
      <xsl:for-each select="@action">
        <xsl:attribute name="role">
          <xsl:value-of select="." />
        </xsl:attribute>
      </xsl:for-each>
      <xsl:for-each select="@name">
        <xsl:attribute name="target">
          <xsl:value-of select="." />
        </xsl:attribute>
      </xsl:for-each>
      <xsl:for-each select="PARAMETER">
        <xsl:call-template name="ohmms.parameter" />
      </xsl:for-each>
    </transform>
  </xsl:template>

  <!-- change for random -->
  <xsl:template name="ohmms.random">
    <random>
      <xsl:for-each select="@parallel">
        <xsl:attribute name="parallel">
          <xsl:value-of select="." />
        </xsl:attribute>
      </xsl:for-each>
      <xsl:for-each select="@seed">
        <xsl:attribute name="seed">
          <xsl:value-of select="." />
        </xsl:attribute>
      </xsl:for-each>
    </random>
  </xsl:template>

  <!-- change for atom -->
  <xsl:template name="ohmms.atom">
    <atom>
      <xsl:for-each select="@name">
        <xsl:attribute name="name">
          <xsl:value-of select="." />
        </xsl:attribute>
      </xsl:for-each>
      <xsl:for-each select="@inunit">
        <xsl:attribute name="condition">
          <xsl:value-of select="." />
        </xsl:attribute>
      </xsl:for-each>
      <xsl:value-of select="." />
    </atom>
  </xsl:template>

  <!-- change for particlesets -->
  <xsl:template name="ohmms.particlesets">
    <particlesets>
      <xsl:for-each select="ParticleSet">
        <xsl:call-template name="ohmms.particleset" />
      </xsl:for-each>
      <xsl:for-each select="TransformParticle">
        <xsl:call-template name="ohmms.transform" />
      </xsl:for-each>
      <xsl:for-each select="nnlist">
        <xsl:call-template name="ohmms.playout" />
      </xsl:for-each>
    </particlesets>
  </xsl:template>

  <xsl:template name="ohmms.particleset">
    <particleset>
      <xsl:for-each select="@name">
        <xsl:attribute name="name">
          <xsl:value-of select="." />
        </xsl:attribute>
      </xsl:for-each>
      <xsl:for-each select="@size">
        <xsl:attribute name="size">
          <xsl:value-of select="." />
        </xsl:attribute>
      </xsl:for-each>
      <xsl:for-each select="@file">
        <xsl:attribute name="src">
          <xsl:value-of select="." />
        </xsl:attribute>
      </xsl:for-each>
      <xsl:for-each select="UnitCell">
        <xsl:call-template name="ohmms.unitcell" />
      </xsl:for-each>
      <xsl:for-each select="attrib">
        <xsl:call-template name="ohmms.attrib" />
      </xsl:for-each>
      <xsl:for-each select="atom">
        <xsl:call-template name="ohmms.atom" />
      </xsl:for-each>
    </particleset>
  </xsl:template>

  <!-- change for transform -->
  <xsl:template name="ohmms.unitcell">
    <unitcell>
      <xsl:for-each select="PARAMETER">
        <xsl:call-template name="ohmms.parameter" />
      </xsl:for-each>
    </unitcell>
  </xsl:template>

  <!-- change for particle layout -->
  <xsl:template name="ohmms.playout">
     <particlelayout>
       <xsl:for-each select="@method">
         <xsl:attribute name="method">
           <xsl:value-of select="." />
         </xsl:attribute>
       </xsl:for-each>
     </particlelayout>
  </xsl:template>

  <!-- change for potential -->
  <xsl:template name="ohmms.potential">
     <potential>
       <xsl:for-each select="@name">
         <xsl:attribute name="name">
            <xsl:value-of select="." />
         </xsl:attribute>
       </xsl:for-each>
       <xsl:for-each select="@file">
         <filename>
            <xsl:value-of select="." />
         </filename>
       </xsl:for-each>
       <xsl:for-each select="kpoint">
         <kpoint>
           <xsl:for-each select="@distributed">
             <xsl:attribute name="role">
               <xsl:value-of select="." />
             </xsl:attribute>
           </xsl:for-each>
           <xsl:for-each select="@file">
             <xsl:attribute name="src">
               <xsl:value-of select="." />
             </xsl:attribute>
           </xsl:for-each>
         </kpoint>
       </xsl:for-each>
       <xsl:for-each select="PARAMETER">
         <xsl:choose>
           <xsl:when test="@name = 'potfile'">
             <filename>
               <xsl:value-of select="." />
             </filename>
           </xsl:when>
           <xsl:otherwise>
             <xsl:call-template name="ohmms.parameter" />
           </xsl:otherwise>
         </xsl:choose>
       </xsl:for-each>
     </potential>
  </xsl:template>

  <!-- change for project -->
  <xsl:template name="ohmms.project">
    <project>
      <xsl:for-each select="@ID">
        <xsl:attribute name="id">
          <xsl:value-of select="." />
        </xsl:attribute>
      </xsl:for-each>
      <xsl:for-each select="@series">
        <xsl:attribute name="series">
          <xsl:value-of select="." />
        </xsl:attribute>
      </xsl:for-each>
    </project>
  </xsl:template>

<!-- check if this allows only one instance
          <project>
            <xsl:for-each select="$V2/Project">
              <xsl:for-each select="@ID">
                <xsl:attribute name="id">
                  <xsl:value-of select="." />
                </xsl:attribute>
              </xsl:for-each>
            </xsl:for-each>
            <xsl:for-each select="$V2/Project">
              <xsl:for-each select="@series">
                <xsl:attribute name="series">
                  <xsl:value-of select="." />
                </xsl:attribute>
              </xsl:for-each>
            </xsl:for-each>
          </project>
-->

</xsl:stylesheet>
