<?xml version="1.0" encoding="UTF-8"?>
<!-- 

This Version: V 0.2 (24 March 2001) 
   http://examplotron.org/0/2/compile.xsl

Latest Version: 
   http://examplotron.org/compile.xsl

Latest version for this namespace: 
   http://examplotron.org/0/compile.xsl

Previous Version: V 0.2 (24 March 2001) 
   http://examplotron.org/0/2/compile.xsl

History:

V0.1: creation
V0.2: 
 - Added version and history information.
V0.3:
 - Added support for eg:assert

Copyright (c) 2001 Eric van der Vlist
                   
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
``Software''), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

The name of the authors when specified in the source files shall be 
kept unmodified.

THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL 4XT.ORG BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:x="fake" xmlns:eg="http://examplotron.org/0/" xmlns:saxon="http://icl.com/saxon">
  <xsl:namespace-alias stylesheet-prefix="x" result-prefix="xsl"/>
  <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  <!-- Get the list of namespaces in the instance document -->
  <xsl:variable name="ns-rtf">
    <namespaces>
      <xsl:for-each select="saxon:distinct(//namespace::*)">
        <ns orig-prefix="{name()}" uri="{.}" pos="{position()}"/>
      </xsl:for-each>
    </namespaces>
  </xsl:variable>
  <xsl:variable name="ns" select="saxon:node-set($ns-rtf)"/>
  <!-- Prefix rewriting -->
  <xsl:template name="rewritePrefixes">
    <xsl:choose>
      <xsl:when test="namespace-uri()">
        <xsl:value-of select="concat('ns', $ns/namespaces/ns[@uri=namespace-uri(current())]/@pos, ':', local-name())"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="local-name()"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <!-- Generation of a full XPath path for an element -->
  <xsl:template name="getPath">
    <xsl:param name="show-position" select="'no'"/>
    <xsl:for-each select="parent::*">
      <xsl:call-template name="getPath">
        <xsl:with-param name="show-position" select="$show-position"/>
      </xsl:call-template>
    </xsl:for-each>
    <xsl:variable name="name">
      <xsl:call-template name="rewritePrefixes"/>
    </xsl:variable>
    <xsl:variable name="position">
      <xsl:if test="$show-position = 'yes' and name()">
        <xsl:value-of select="concat('[', count(preceding-sibling::*[name()=$name])+1, ']')"/>
      </xsl:if>
    </xsl:variable>
    <xsl:value-of select="concat('/', $name, $position)"/>
  </xsl:template>
  <!-- Generation of a full XPath path for an attribute -->
  <xsl:template name="getAttPath">
    <xsl:param name="show-position"/>
    <xsl:for-each select="parent::*">
      <xsl:call-template name="getPath">
        <xsl:with-param name="show-position" select="$show-position"/>
      </xsl:call-template>
    </xsl:for-each>
    <xsl:variable name="name">
      <xsl:call-template name="rewritePrefixes"/>
    </xsl:variable>
    <xsl:value-of select="concat('/@', $name)"/>
  </xsl:template>
  <!-- Check the children of a node -->
  <xsl:template name="check-children">
    <xsl:apply-templates select="*|@*" mode="check-children"/>
      <xsl:if test="@eg:assert">
        <xsl:value-of select="concat('(', @eg:assert, ') and ')"/>
      </xsl:if>
    <xsl:text> 1=1</xsl:text>
  </xsl:template>
  <!-- Check a child element node -->
  <xsl:template match="*" mode="check-children">
    <xsl:variable name="name" select="name()"/>
    <xsl:variable name="pname">
      <xsl:call-template name="rewritePrefixes"/>
    </xsl:variable>
    <xsl:variable name="cnt" select="count(../*[$name=name()])"/>
    <xsl:variable name="star" select="../*[$name=name() and @eg:occurs = '*']"/>
    <xsl:variable name="plus" select="../*[$name=name() and @eg:occurs = '+']"/>
    <xsl:variable name="point" select="../*[$name=name() and @eg:occurs = '.']"/>
    <xsl:variable name="question" select="../*[$name=name() and @eg:occurs = '?']"/>
    <xsl:if test="not(preceding-sibling::*[name()=$name])">
      <xsl:choose>
        <xsl:when test="$star or $plus or $question or $point">
          <xsl:variable name="min">
            <xsl:choose>
              <xsl:when test="$star or $question">
                <xsl:value-of select="'1=1'"/>
              </xsl:when>
              <xsl:when test="$plus or $point">
                <xsl:value-of select="concat('count(', $pname, ')>0')"/>
              </xsl:when>
            </xsl:choose>
          </xsl:variable>
          <xsl:variable name="max">
            <xsl:choose>
              <xsl:when test="$star or $plus">
                <xsl:value-of select="'1=1'"/>
              </xsl:when>
              <xsl:when test="$question or $point">
                <xsl:value-of select="concat('1 >= count(', $pname, ')')"/>
              </xsl:when>
            </xsl:choose>
          </xsl:variable>
          <xsl:value-of select="concat($min, ' and ', $max, ' and ')"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="concat('count(', $pname, ')=', $cnt, ' and ')"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:if>
  </xsl:template>
  <!-- Ignore a child examplotron attribute node -->
  <xsl:template match="@eg:*" mode="check-children"/>
  <!-- Check a child attribute node -->
  <xsl:template match="@*" mode="check-children">
    <xsl:variable name="name" select="name()"/>
    <xsl:variable name="pname">
      <xsl:call-template name="rewritePrefixes"/>
    </xsl:variable>
    <xsl:if test="not(preceding-sibling::*[name()=$name])">
      <xsl:value-of select="concat('@', $pname, ' and ')"/>
    </xsl:if>
  </xsl:template>
  <xsl:template match="*" name="check-element">
    <!-- to do handle attributes, handle values, accepts directives, support namespaces...-->
    <xsl:variable name="name" select="name()"/>
    <xsl:variable name="pname">
      <xsl:call-template name="rewritePrefixes"/>
    </xsl:variable>
    <xsl:variable name="sub-cond">
      <xsl:if test="name()">[<xsl:call-template name="check-children"/>]</xsl:if>
    </xsl:variable>
    <xsl:variable name="path">
      <xsl:call-template name="getPath">
        <xsl:with-param name="show-position" select="'no'"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:if test="normalize-space($sub-cond)">
      <x:template match="{$path}">
        <error type="Element content mismatch" name="{$path}" expected="{$sub-cond}"/>
      </x:template>
    </xsl:if>
    <x:template>
      <xsl:if test="@eg:assert">
        <xsl:copy-of select="namespace::*"/>
      </xsl:if>
      <xsl:attribute name="priority">1</xsl:attribute>
      <xsl:attribute name="match"><xsl:value-of select="concat($path, $sub-cond)"/></xsl:attribute>
      <x:apply-templates select="*|@*"/>
    </x:template>
    <xsl:apply-templates select="*|@*"/>
  </xsl:template>
  <xsl:template match="@eg:*"/>
  <xsl:template match="@*">
    <xsl:variable name="name" select="name()"/>
    <xsl:variable name="path">
      <xsl:call-template name="getAttPath">
        <xsl:with-param name="show-position" select="'no'"/>
      </xsl:call-template>
    </xsl:variable>
    <x:template priority="1">
      <xsl:attribute name="match"><xsl:value-of select="$path"/></xsl:attribute>
    </x:template>
  </xsl:template>
  <xsl:template match="ns" mode="ns">
    <xsl:attribute name="ns{@pos}:dummy" namespace="{@uri}"/>
  </xsl:template>
  <xsl:template match="/">
    <x:stylesheet version="1.0">
      <xsl:apply-templates select="$ns/namespaces/ns" mode="ns"/>
      <x:template match="*">
        <error type="Unexpected element">
          <x:attribute name="path">
            <x:call-template name="getPath"/>
          </x:attribute>
        </error>
      </x:template>
      <x:template match="@*">
        <error type="Unexpected attribute">
          <x:attribute name="path">
            <x:call-template name="getAttPath"/>
          </x:attribute>
        </error>
      </x:template>
      <x:template name="getPath">
        <x:for-each select="parent::*">
          <x:call-template name="getPath"/>
        </x:for-each>
        <x:variable name="name" select="name()"/>
        <x:value-of select="concat('/', $name, '[', count(preceding-sibling::*[name()=$name])+1, ']')"/>
      </x:template>
      <x:template name="getAttPath">
        <x:for-each select="parent::*">
          <x:call-template name="getPath"/>
        </x:for-each>
        <x:variable name="name" select="name()"/>
        <x:value-of select="concat('/@', $name)"/>
      </x:template>
      <xsl:call-template name="check-element"/>
    </x:stylesheet>
  </xsl:template>
</xsl:stylesheet>
