<?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" />

  <!-- convert particleset xml files from ohmms version 0.2.3 to 0.2.3.1 -->
  <xsl:template match="/ParticleSet">
    <xsl:call-template name="ohmms.particleset" />
  </xsl:template>

  <!-- change from PARAMETER  to parameter -->
  <xsl:template name="ohmms.parameter">
    <parameter>
      <xsl:for-each select="@name">
        <xsl:attribute name="name">
          <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: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 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 particleset -->
  <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>
</xsl:stylesheet>
