<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:teix="http://www.tei-c.org/ns/1.0"
  version="1.0">
  
  <xsl:template match="/">
    <html>
      <head><title>Test</title></head>
      <body>
        <h1>Test of egXML transform</h1>
        <div>
          <xsl:apply-templates/>
        </div>
      </body>
    </html>
  </xsl:template>
  
  <xsl:template match="teix:egXML/descendant::*">
    <xsl:choose>
      <xsl:when test="count(text()) = 1 and count(*) = 0 and count(comment()) = 0 and count(processing-instruction()) = 0">
        <!-- text-only, no child elements, comments, or PIs -->
        <span class="STAGO"><xsl:text>&lt;</xsl:text></span>
        <span class="GI"><xsl:value-of select="name()"/></span>
        <xsl:apply-templates select="@*"/>
        <xsl:call-template name="text-only"/>
      </xsl:when>
      <xsl:when test="*|comment()|processing-instruction()">
        <span class="STAGO"><xsl:text>&lt;</xsl:text></span>
        <span class="GI"><xsl:value-of select="name()"/></span>
        <xsl:apply-templates select="@*"/>
        <span class="STAGC"><xsl:text>&gt;</xsl:text></span>
        <xsl:apply-templates/>
        <span class="ETAGO"><xsl:text>&lt;/</xsl:text></span>
        <span class="GI"><xsl:value-of select="name()"/></span>
        <span class="ETAGC"><xsl:text>&gt;</xsl:text></span>
      </xsl:when>
      <xsl:otherwise>
        <span class="STAGO"><xsl:text>&lt;</xsl:text></span>
        <span class="GI"><xsl:value-of select="name()"/></span>
        <xsl:apply-templates select="@*"/>
        <span class="ETAGC"><xsl:text>/&gt;</xsl:text></span>
        <xsl:apply-templates/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template match="@*">
    <xsl:text>  </xsl:text>
    <span class="attrName"><xsl:value-of select="name()"/></span>
    <xsl:variable name="LITa">
      <xsl:choose>
        <xsl:when test='contains(.,&apos;&quot;&apos;)'>
          <xsl:text>&apos;</xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>&quot;</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <span class="VI"><xsl:text>=</xsl:text></span>
    <span class="LITa"><xsl:value-of select="$LITa"/></span>
    <span class="attrVal"><xsl:value-of select="."/></span>
    <span class="LITa"><xsl:value-of select="$LITa"/></span>
  </xsl:template>
  
  <xsl:template match="comment()">
    <span class="MDO"><xsl:text>&lt;!</xsl:text></span>
    <span class="COM"><xsl:text>--</xsl:text></span>
    <span class="comment"><xsl:value-of select="."/></span>
    <span class="COM"><xsl:text>--</xsl:text></span>
    <span class="MDC"><xsl:text>&gt;</xsl:text></span>
  </xsl:template>
  
  <xsl:template match="processing-instruction()">
    <span class="PIO"><xsl:text>&lt;?</xsl:text></span>
    <span class="PIname"><xsl:value-of select="name()"/></span>
    <xsl:text>  </xsl:text>
    <span class="PIcontent"><xsl:value-of select="."/></span>
    <span class="PIC"><xsl:text>?&gt;</xsl:text></span>
  </xsl:template>
  
  <xsl:template match="text()">
    <xsl:if test="string-length(.)">
      <span class="PCDATA"><xsl:value-of select="."/></span>
    </xsl:if>
  </xsl:template>
  
  <xsl:template name="text-only">
    <xsl:choose>
      <xsl:when test="string-length(.)">
        <span class="STAGC"><xsl:text>&gt;</xsl:text></span>
        <span class="PCDATA"><xsl:value-of select="."/></span>
        <span class="ETAGO"><xsl:text>&lt;/</xsl:text></span>
        <span class="GI"><xsl:value-of select="name()"/></span>
        <span class="ETAGC"><xsl:text>&gt;</xsl:text></span>
      </xsl:when>
      <xsl:otherwise>
        <span class="ETAGC"><xsl:text>/&gt;</xsl:text></span>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
</xsl:stylesheet>