<?xml version="1.0"?>
<!-- Look in source code to see unformatted XML -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
    xmlns:xhtml="http://www.w3.org/1999/xhtml" exclude-result-prefixes="xhtml">
    <xsl:variable name="unparsed" select="unparsed-text(document-uri(.))"/>
    <!-- unparsed - comment -->
    <xsl:variable name="unparsed2" select="replace($unparsed,  '&lt;!--.*?--&gt;', '', 's')"/>
    <!-- unparsed - comment - cdata -->
    <xsl:variable name="unparsed3"
        select="replace($unparsed2,  '&lt;!\[CDATA\[.*?\]\]&gt;', '', 's')"/>
    <!-- unparsed - comment - cdata - pi -->
    <xsl:variable name="unparsed4"
        select="if (starts-with($unparsed, '&lt;?xml')) then concat(substring-before($unparsed3, '?&gt;'), '?&gt;', replace($unparsed3,  '&lt;\?.*?\?&gt;', '', 's')) else replace($unparsed3,  '&lt;\?.*?\?&gt;', '', 's')"/>

    <xsl:template match="/">
        <test>
           <!-- Validate if document starts with an XML declaration.  -->
            <xsl:if test="not(starts-with($unparsed, '&lt;?xml'))">
                <error>Your XHTML document must start with an XML declaration.</error>
            </xsl:if>
            <!-- Validate if XML declaration is version 1.0.  -->
            <xsl:variable name="declaration"
                select="if (starts-with($unparsed, '&lt;?xml')) then concat(substring-before($unparsed, '?>'), '?>') else ''"/>
            <xsl:if
                test="starts-with($unparsed, '&lt;?xml') and not(matches($declaration, '1.0'))">
                <error>You must use XML version 1.0.</error>
            </xsl:if>
            <!-- Validate if encoding="UTF-8" is used explicitly.  -->
            <xsl:if
                test="starts-with($unparsed, '&lt;?xml') and not(matches($declaration, 'utf-8|UTF-8'))">
                <error>You must use XML encoding="UTF-8" explicitly.</error>
            </xsl:if>
            <!-- Validate that the standalone pseudo attribute is not used.  -->
            <xsl:if
                test="starts-with($unparsed, '&lt;?xml') and matches($declaration, 'standalone')">
                <error>The standalone pseudo attribute must not be used.</error>
            </xsl:if>
            <!-- Validate that the XML declaration has no insignificant whitespace.  -->
            <xsl:if
                test="matches($declaration, 'utf-8|UTF-8') and not(matches($declaration, 'standalone')) and not(string-length($declaration) eq 38)">
                <error>The XML declaration must not contain insignificant whitespace.</error>
            </xsl:if>
            <!-- Validate that DTD is used. -->
            <xsl:if test="not(matches($unparsed4, '&lt;!DOCTYPE'))">
                <error>The DOCTYPE declaration is missing.</error>
            </xsl:if>
            <!-- Validate that there are no comments or processing instruction between XML declaration and DTD.  -->
            <xsl:if
                test="matches($declaration, 'utf-8|UTF-8') and not(matches($declaration, 'standalone')) and string-length($declaration) eq 38 and matches($unparsed4, '&lt;!DOCTYPE') and matches(substring-after(substring-before($unparsed, '&lt;!DOCTYPE'), '?&gt;'), '&lt;(!--|\?)')">
                <error>There must be no comments or PIs between XML declaration and DTD.</error>
            </xsl:if>
            <!-- Validate that there is exactly one newline between XML declaration and the DTD.  -->
            <xsl:if
                test="string-length($declaration) eq 38 and not(matches(substring-after(substring-before($unparsed, '&lt;!DOCTYPE'), '?&gt;'), '&lt;(!--|\?)')) and not(string-length(substring-before($unparsed4, '&lt;!DOCTYPE')) - string-length(replace(substring-before($unparsed4, '&lt;!DOCTYPE'), '&#xA;', '')) = 1)">
                <error>There must be exactly one newline between XML declaration and the DTD.</error>
            </xsl:if>
            <!-- Validate that DTD for XHTML 1.0 Strict! is used. -->
            <xsl:if
                test="matches($unparsed4, '&lt;!DOCTYPE') and not(matches($unparsed4, 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd')) and not(matches($unparsed4, '-//W3C//DTD XHTML 1.0 Strict//EN'))">
                <error>The DTD must be for XHTML 1.0 Strict!.</error>
            </xsl:if>
        </test>
    </xsl:template>
</xsl:stylesheet>