xsl-list
[Top] [All Lists]

[xsl] XSLT 2.0 Quine

2011-08-11 12:21:32
Dear all,

While working on the first XProc Quine[1] , I discover that there was
only two attempts to a quine in XSLT 1.0 [2]

The existing two are based on
* the DOE trick
* the document('') trick

So I was a bit disappointed

That's why I crafted this one which is not using DOE nor document trick

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="2.0">
  <!-- (c) Innovimax 2011 - XSLT 2.0 Quine without DOE -->
  <xsl:variable name="code">
    <xsl:element name="xsl:stylesheet">
      <xsl:attribute name="version" select="'2.0'"/>
      <xsl:comment> (c) Innovimax 2011 - XSLT 2.0 Quine without DOE
</xsl:comment>
      <xsl:element name="xsl:variable">
        <xsl:attribute name="name" select="'code'"/>
        <xsl:element name="foo"/>
      </xsl:element>
      <xsl:element name="xsl:template">
        <xsl:attribute name="match" select="'/'"/>
        <xsl:element name="xsl:apply-templates">
          <xsl:attribute name="select" select="'$code'"/>
          <xsl:attribute name="mode" select="'copy'"/>
        </xsl:element>
      </xsl:element>
      <xsl:element name="xsl:template">
        <xsl:attribute name="match" select="'@*|node()'"/>
        <xsl:attribute name="mode" select="'copy'"/>
        <xsl:element name="xsl:copy">
          <xsl:element name="xsl:apply-templates">
            <xsl:attribute name="select" select="'@*|node()'"/>
            <xsl:attribute name="mode" select="'copy'"/>
          </xsl:element>
        </xsl:element>
      </xsl:element>
      <xsl:element name="xsl:template">
        <xsl:attribute name="match" select="'foo'"/>
        <xsl:attribute name="mode" select="'copy'"/>
        <xsl:element name="xsl:apply-templates">
          <xsl:attribute name="select" select="'$code'"/>
          <xsl:attribute name="mode" select="'el'"/>
        </xsl:element>
      </xsl:element>
      <xsl:element name="xsl:template">
        <xsl:attribute name="match" select="'*'"/>
        <xsl:attribute name="mode" select="'el'"/>
        <xsl:element name="xsl:element">
          <xsl:attribute name="name" select="'xsl:element'"/>
          <xsl:element name="xsl:attribute">
            <xsl:attribute name="name" select="'name'"/>
            <xsl:attribute name="select" select="'name()'"/>
          </xsl:element>
          <xsl:element name="xsl:apply-templates">
            <xsl:attribute name="select" select="'@*|*|text()|comment()'"/>
            <xsl:attribute name="mode" select="'el'"/>
          </xsl:element>
        </xsl:element>
      </xsl:element>
      <xsl:element name="xsl:template">
        <xsl:attribute name="match" select="'@*'"/>
        <xsl:attribute name="mode" select="'el'"/>
        <xsl:element name="xsl:element">
          <xsl:attribute name="name" select="'xsl:attribute'"/>
          <xsl:element name="xsl:attribute">
            <xsl:attribute name="name" select="'name'"/>
            <xsl:attribute name="select" select="'name()'"/>
          </xsl:element>
          <xsl:element name="xsl:attribute">
            <xsl:attribute name="name" select="'select'"/>
            <xsl:text>'</xsl:text>
            <xsl:element name="xsl:value-of">
              <xsl:attribute name="select" select="'.'"/>
            </xsl:element>
            <xsl:text>'</xsl:text>
          </xsl:element>
        </xsl:element>
      </xsl:element>
      <xsl:element name="xsl:template">
        <xsl:attribute name="match" select="'text()'"/>
        <xsl:attribute name="mode" select="'el'"/>
        <xsl:element name="xsl:element">
          <xsl:attribute name="name" select="'xsl:text'"/>
          <xsl:element name="xsl:value-of">
            <xsl:attribute name="select" select="'.'"/>
          </xsl:element>
        </xsl:element>
      </xsl:element>
      <xsl:element name="xsl:template">
        <xsl:attribute name="match" select="'comment()'"/>
        <xsl:attribute name="mode" select="'el'"/>
        <xsl:element name="xsl:element">
          <xsl:attribute name="name" select="'xsl:comment'"/>
          <xsl:element name="xsl:value-of">
            <xsl:attribute name="select" select="'.'"/>
          </xsl:element>
        </xsl:element>
      </xsl:element>
    </xsl:element>
  </xsl:variable>
  <xsl:template match="/">
    <xsl:apply-templates select="$code" mode="copy"/>
  </xsl:template>
  <xsl:template match="@*|node()" mode="copy">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()" mode="copy"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="foo" mode="copy">
    <xsl:apply-templates select="$code" mode="el"/>
  </xsl:template>
  <xsl:template match="*" mode="el">
    <xsl:element name="xsl:element">
      <xsl:attribute name="name" select="name()"/>
      <xsl:apply-templates select="@*|*|text()|comment()" mode="el"/>
    </xsl:element>
  </xsl:template>
  <xsl:template match="@*" mode="el">
    <xsl:element name="xsl:attribute">
      <xsl:attribute name="name" select="name()"/>
      <xsl:attribute name="select">'<xsl:value-of select="."/>'</xsl:attribute>
    </xsl:element>
  </xsl:template>
  <xsl:template match="text()" mode="el">
    <xsl:element name="xsl:text">
      <xsl:value-of select="."/>
    </xsl:element>
  </xsl:template>
  <xsl:template match="comment()" mode="el">
    <xsl:element name="xsl:comment">
      <xsl:value-of select="."/>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

Cheers,

Xmlizer

[1] http://blog.innovimax.fr/index.php/2011/08/11/38-xproc-quine
[2] http://www2.informatik.hu-berlin.de/~obecker/XSLT/#quine

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>