<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet href="#self" type="text/xsl" title="Generate text/plain JS+CSS Entitags"?>
<?xml-stylesheet href="#self" type="text/xml" title="Generate text/plain JS+CSS Entitags"?>
<!DOCTYPE xsl:stylesheet
          [
          <!ATTLIST xsl:stylesheet id ID #REQUIRED>
          <!ENTITY cr "&#xD;"><!ENTITY lf "&#xA;"><!ENTITY br "&lf;">
          <!ENTITY nl "<xsl:text xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>&br;</xsl:text>">
          ]>
<!--  ================================================  -->
<!--  ================================================  -->
<!--  ================================================ 


      ================================================  -->
<!--  ================================================  -->
<!--  ================================================  -->

<xsl:stylesheet version="1.0" id="self"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:ent="urn:entitags:entity-as-empty-element-tag"
                >
<xsl:output method="text"
            media-type="text/plain"
            encoding="utf-8" />
<xsl:preserve-space elements="*" />
<xsl:template match="xsl:stylesheet" />

<xsl:param name="NN" select="''" />     <!-- generateNN  -->

<xsl:template name="LFtoCRLF">
    <xsl:param name="str" />
    <xsl:choose>
        <xsl:when test="contains($str,'&#xA;')">
            <xsl:value-of select="substring-before($str,'&#xA;')" /><xsl:text>&#xD;&#xA;</xsl:text>
            <xsl:call-template name="LFtoCRLF">
                <xsl:with-param name="str" select="substring-after($str,'&#xA;')" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$str" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:variable name="NofEntitags" select="count(document('entitags.xml')//ent:el)" />

<xsl:template match="/">
<xsl:choose>
	<xsl:when test="false()">
		<xsl:call-template name="LFtoCRLF"> <!-- only for test -->
		<xsl:with-param name="str">
			<xsl:call-template name="generator" />
		</xsl:with-param>
		</xsl:call-template>
	</xsl:when>
	<xsl:otherwise>
		<xsl:call-template name="generator" />
	</xsl:otherwise>
</xsl:choose>
</xsl:template>

<!--  ================================================  -->
<!--  ================================================  -->
<!--  ================================================  -->

<xsl:template name="generator">
    <xsl:choose>
        <xsl:when test="$NN='JS'">
            <xsl:call-template name="generateJSarray" />
            <xsl:call-template name="generateJSobjlit" />
        </xsl:when>
        <xsl:when test="$NN='CSS2'">
            <xsl:call-template name="generateCSS2" />
        </xsl:when>
        <xsl:when test="$NN='CSS3'">
            <xsl:call-template name="generateCSS3" />
        </xsl:when>
        <xsl:otherwise>
            <xsl:call-template name="generateJSobjlit" />
            <xsl:call-template name="generateCSS3" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template name="generateCSS3">
    &nl;
    <xsl:text>/*   ********************************************* &br;</xsl:text>
    <xsl:text> *   CSS-3 generated/transformed from entitagX.xml &br;</xsl:text>
    <xsl:text> *   XML v</xsl:text><xsl:value-of select="system-property('xsl:version')" /> &nl;
    <xsl:text> *   XSLTransformed by &br;</xsl:text>
    <xsl:text> *   </xsl:text><xsl:value-of select="system-property('xsl:vendor')" /> &nl;
    <xsl:text> *   </xsl:text><xsl:value-of select="system-property('xsl:vendor-url')" /> &nl;
    <xsl:text> */  </xsl:text>
    &nl;
    <xsl:text>&br;@namespace ent uri(urn:entitags:entity-as-empty-element-tag); </xsl:text>
    <xsl:text>&br;ent|*[unicode]::after { content: attr(unicode); }  /* DOC-local or DTD-default */&br;&br;</xsl:text>
    <xsl:for-each select="document('entitags.xml')//ent:el">
        <xsl:text>ent|</xsl:text>
        <xsl:value-of select="@tagname" />
        <xsl:text>::after { content: &quot;</xsl:text>
        <xsl:value-of select="@csscode" />
        <xsl:text>&quot;; }</xsl:text>
        <xsl:text>  /*</xsl:text><xsl:value-of select="following::comment()" /><xsl:text>*/ &br;</xsl:text>
    </xsl:for-each>
    &nl;
</xsl:template>

<xsl:template name="generateCSS2">
    &nl;
    <xsl:text>/*   ********************************************* &br;</xsl:text>
    <xsl:text> *   CSS-2 generated/transformed from entitagX.xml &br;</xsl:text>
    <xsl:text> *   XML v</xsl:text><xsl:value-of select="system-property('xsl:version')" /> &nl;
    <xsl:text> *   XSLTransformed by &br;</xsl:text>
    <xsl:text> *   </xsl:text><xsl:value-of select="system-property('xsl:vendor')" /> &nl;
    <xsl:text> *   </xsl:text><xsl:value-of select="system-property('xsl:vendor-url')" /> &nl;
    <xsl:text> */  </xsl:text>
    &nl;
    <xsl:text>&br;*[unicode]:after { content: attr(unicode); } &br;&br;</xsl:text>
    <xsl:for-each select="document('entitags.xml')//ent:el">
        <xsl:value-of select="@tagname" />
        <xsl:text>:after { content: &quot;</xsl:text>
        <xsl:value-of select="@csscode" />
        <xsl:text>&quot;; }</xsl:text>
        <xsl:text>  /*</xsl:text><xsl:value-of select="following::comment()" /><xsl:text>*/ &br;</xsl:text>
    </xsl:for-each>
    &nl;
</xsl:template>

<xsl:template name="bslashESC">
    <xsl:param name="str" />
    <xsl:param name="esc" />
    <xsl:choose>
        <xsl:when test="contains($str,$esc)">
            <xsl:value-of select="substring-before($str,$esc)" /><xsl:text>\</xsl:text><xsl:value-of select="$esc" />
            <xsl:call-template name="bslashESC">
                <xsl:with-param name="str" select="substring-after($str,$esc)" />
                <xsl:with-param name="esc" select="$esc" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$str" />
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

<xsl:template name="generateJSarray">
    &nl;
    <xsl:text>/*   ************************************************ &br;</xsl:text>
    <xsl:text> *   JS-Array generated/transformed from entitagX.xml &br;</xsl:text>
    <xsl:text> *   XML v</xsl:text><xsl:value-of select="system-property('xsl:version')" /> &nl;
    <xsl:text> *   XSLTransformed by &br;</xsl:text>
    <xsl:text> *   </xsl:text><xsl:value-of select="system-property('xsl:vendor')" /> &nl;
    <xsl:text> *   </xsl:text><xsl:value-of select="system-property('xsl:vendor-url')" /> &nl;
    <xsl:text> */  </xsl:text>
    &nl;
    <xsl:text>&br;var ent = new Array(</xsl:text><xsl:value-of select="$NofEntitags" /><xsl:text>);   //  Array for Entitags-Transformation &br;&br;</xsl:text>
    <xsl:for-each select="document('entitags.xml')//ent:el">
        <xsl:text>ent[&quot;</xsl:text>
        <xsl:value-of select="@tagname" />
        <xsl:text>&quot;]=&quot;</xsl:text>
        <xsl:choose>
            <xsl:when test="string-length(@texcode)=1">
                <xsl:text>\u</xsl:text><xsl:value-of select="substring(@csscode,4)" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:variable name="texcode">
                    <xsl:call-template name="bslashESC">
                        <xsl:with-param name="str" select="@texcode" />
                        <xsl:with-param name="esc" select="'\'" />
                    </xsl:call-template>
                </xsl:variable>
                <xsl:call-template name="bslashESC">
                    <xsl:with-param name="str" select="$texcode" />
                    <xsl:with-param name="esc" select="'&quot;'" />
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
        <xsl:text>&quot;; &br;</xsl:text>
    </xsl:for-each>
    &nl;
</xsl:template>

<xsl:template name="generateJSobjlit">
    &nl;
    <xsl:text>/*   ********************************************************** &br;</xsl:text>
    <xsl:text> *   JS-Object-Literals generated/transformed from entitagX.xml &br;</xsl:text>
    <xsl:text> *   XML v</xsl:text><xsl:value-of select="system-property('xsl:version')" /> &nl;
    <xsl:text> *   XSLTransformed by &br;</xsl:text>
    <xsl:text> *   </xsl:text><xsl:value-of select="system-property('xsl:vendor')" /> &nl;
    <xsl:text> *   </xsl:text><xsl:value-of select="system-property('xsl:vendor-url')" /> &nl;
    <xsl:text> */  </xsl:text>
    &nl;
    <xsl:text>&br;               //  USAGE: ent.isoname OR ent[&quot;isoname&quot;] OR ent[el.localName]</xsl:text>
    <xsl:text>&br;               //         if (&quot;isoname&quot; in ent) {}; OR if (el.localName in ent) {};</xsl:text>
    <xsl:text>&br;               //         for (var n in ent) {};</xsl:text>
    <xsl:text>&br;const ent = {  //  </xsl:text><xsl:value-of select="$NofEntitags" /><xsl:text>  Object-Literals for Entitags-Transformation&br;&br;</xsl:text>
    <xsl:for-each select="document('entitags.xml')//ent:el">
        <xsl:value-of select="@tagname" />
        <xsl:text>:&quot;</xsl:text>
        <xsl:choose>
            <xsl:when test="string-length(@texcode)=1">
                <xsl:text>\u</xsl:text><xsl:value-of select="substring(@csscode,4)" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:variable name="texcode">
                    <xsl:call-template name="bslashESC">
                        <xsl:with-param name="str" select="@texcode" />
                        <xsl:with-param name="esc" select="'\'" />
                    </xsl:call-template>
                </xsl:variable>
                <xsl:call-template name="bslashESC">
                    <xsl:with-param name="str" select="$texcode" />
                    <xsl:with-param name="esc" select="'&quot;'" />
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
        <xsl:text>&quot;</xsl:text>
		<xsl:if test="not(position()=$NofEntitags)"><xsl:text>, &br;</xsl:text></xsl:if>
    </xsl:for-each>
    <xsl:text>  };  //  end of ent &br;&br;</xsl:text>
</xsl:template>


<!--  ================================================  -->
<!--  ================================================  -->
<!--  ================================================  -->

</xsl:stylesheet>

<!--  ================================================  -->
<!--  EOF                                               -->
<!--  ================================================  -->