Xmlinput2xsf Xsl

Conversion template to generate an XCrysDen structure file (.xsf file) out of the exciting input.xml file.

How this template is used:

 xsltproc xmlinput2xcf.xsl input.xml > structure.xsf

Note: xsl needs the atomic number for selecting the right color. If present, the <atomicNumber> attribute of the species element is used. Otherwise the template tries to open the species files. Because the path lookup happens relative to the template and not to the source document it is necessary to specify the species path on th command line:
xsltproc -path ../../species ../../xml/xmlinput2xsf.xsl  input.xml > structure.xsf

The xmlinput2xcf.xsl template is given below:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:str="http://exslt.org/strings" xmlns:math="http://exslt.org/math">
  <xsl:output method="text" />
  <!-- usage: xsltproc xmlinput2xcf.xsl input.xml > structure.xsf -->
  <xsl:template name="norm">
    <xsl:param name="vectorstring" />
    <xsl:value-of
      select="math:sqrt(
math:power(str:tokenize($vectorstring)[1]*$scale,2)
+math:power(str:tokenize($vectorstring)[2]*$scale,2)
+math:power(str:tokenize($vectorstring)[3]*$scale,2)
)*$bohr2angstr" />
  </xsl:template>
  <xsl:variable name="bohr2angstr" select="0.529177" />
  <xsl:variable name="scale">
    <xsl:choose>
      <xsl:when test="/input/structire/crystal/@scale">
        <xsl:value-of select="/input/structire/crystal/@scale" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="1" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>
  <xsl:variable name="a">
    <xsl:call-template name="norm">
      <xsl:with-param name="vectorstring" select="/input/structure/crystal/basevect[1]" />
    </xsl:call-template>
  </xsl:variable>
  <xsl:variable name="b">
    <xsl:call-template name="norm">
      <xsl:with-param name="vectorstring" select="/input/structure/crystal/basevect[2]" />
    </xsl:call-template>
  </xsl:variable>
  <xsl:variable name="c">
    <xsl:call-template name="norm">
      <xsl:with-param name="vectorstring" select="/input/structure/crystal/basevect[3]" />
    </xsl:call-template>
  </xsl:variable>
  <xsl:template match="/">
    <xsl:text>
 INFO
 nunit      1    1    1
 unit   cell
 celltype   convcell
 shape   parapipedal
 END_INFO
 DIM-GROUP
           3           1
 PRIMVEC
 </xsl:text>
    <xsl:for-each select="/input/structure/crystal/basevect">
      <xsl:for-each select="str:tokenize(.)">
        <xsl:value-of select="$scale * ./.*$bohr2angstr" />
        <xsl:text>   </xsl:text>
      </xsl:for-each>
      <xsl:text>
   </xsl:text>
    </xsl:for-each>
    <xsl:text>  
 PRIMCOORD
    </xsl:text>
    <xsl:value-of select="count(input/structure/species/atom)" />
    <xsl:text> 1</xsl:text>
    <xsl:for-each select="input/structure/species/atom">
      <xsl:text>
</xsl:text>
      <xsl:choose>
        <xsl:when test="../@atomicNumber">
          <xsl:value-of select="../@atomicNumber" />
        </xsl:when>
        <xsl:otherwise>
          <xsl:variable name="speciesfile">
            <xsl:value-of select="/input/structure/@speciespath" />
            <xsl:text>/</xsl:text>
            <xsl:value-of select="../@speciesfile" />
          </xsl:variable>
          <xsl:value-of select="-1*document($speciesfile)/spdb/sp/@z" />
        </xsl:otherwise>
      </xsl:choose>
      <xsl:text>   </xsl:text>
      <xsl:value-of select="str:tokenize(@coord)[1]*$a" />
      <xsl:text>  </xsl:text>
      <xsl:value-of select="str:tokenize(@coord)[2]*$b" />
      <xsl:text>  </xsl:text>
      <xsl:value-of select="str:tokenize(@coord)[3]*$c" />
      <xsl:text>  </xsl:text>
    </xsl:for-each>
    <xsl:text>
   </xsl:text>
  </xsl:template>
</xsl:stylesheet>
Page tags: template visualization
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License