[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

XSLT 2.0 Quine

Subject: XSLT 2.0 Quine
From: Xmlizer <xmlizer+xsllist@xxxxxxxxx>
Date: Thu, 11 Aug 2011 19:21:15 +0200
 XSLT 2.0 Quine
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

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.