<?xar XSLT?>

<!-- 
   OVERVIEW
   
   ASCC/Schematron.com Skeleton Module for ISO Schematron (for XSLT2 systems)
   
   ISO Schematron is a language for making assertion about the presence or absense
   of patterns in XML documents. It is typically used for as a schema language, or
   to augment existing schema languages, and to check business rules. It is very
   powerful, yet quite simple: a developer only need know XPath and about five other
   elements.
   
   This is an open source implementation of ISO Schematron in XSLT. Although ISO does
   not allow reference implementations which might compete with the text of the
   standard, this code has been compiled by Rick Jelliffe, inventor of Schematron
   and editor of the ISO standard; so developers can certainly use it as an 
   unofficial reference implementation for clarification. 
   
   This implementation is based on one by Oliver Becker. API documentation is 
   available separately; try www.schematron.com for this. Funding for this
   stylesheet over the years has come from Topologi Pty. Ltd., Geotempo Ltd.,
   and ASCC, Tapei.
   
   There are two versions of this skeleton: one is tailored for XSLT1 processors
   and the other is tailored for XSLT2 processors. Future versions of the
   XSLT2 skeleton may support more features than that the XSLT 1 skeleton.
-->
  
<!--
Open Source Initiative OSI - The MIT License:Licensing
[OSI Approved License]

This source code was previously available under the zlib/libpng license. 
Attribution is polite.

The MIT License

Copyright (c)  2000-2010 Rick Jellife and Academia Sinica Computing Centre, Taiwan.

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 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 THE
AUTHORS OR COPYRIGHT HOLDERS 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.
-->
<!--
   TIPS
      
   A tip for new users of Schematron: make your assertions contain positive messages
   about what is expected, rather than error messages. For example, use the form
   "An X should have a Y, because Z". 
   
   Another tip is that Schematron provides an
   element <iso:ns> for declaring the namespaces and prefixes used in Xpaths in 
   attribute values; it does not extend the XML Namespaces mechanism: if a name
   in an XPath has a prefix, there must be an <iso:ns> element for that prefix; if
   a name in an XPath does not have a prefix, it is always in no namespace.
   
   A tip for implementers of Schematron, either using this API or re-implementing it:
   make the value of the diagnostics, flags and richer features available if possible;
   Schematron has many of the optional richer features which, if implemented, provide
   a compelling alternative approach to validation and business-rules checking compared
   to other schema languages and programs. 
   
   If you create your own meta-stylesheet to override this one, it is a
   good idea to have both in the same directory and to run the stylesheet
   from that directory, as many XSLT implementations have ideosyncratic
   handling of URLs: keep it simple.
-->
 

<!--
  INVOCATION INFORMATION
  
  The following parameters are available
  
    phase           NMTOKEN | "#ALL" (default) Select the phase for validation
    allow-foreign   "true" | "false" (default)   Pass non-Schematron elements to the generated stylesheet 
    sch.exslt.imports semi-colon delimited string of filenames for some EXSLT implementations  
    message-newline "true" (default) | "false"   Generate an extra newline at the end of messages 
    debug	    "true" | "false" (default)  Debug mode lets compilation continue despite problems
    attributes "true" | "false"  (Autodetecting) Use only when the schema has no attributes as the context nodes
    only-child-elements "true" | "false" (Autodetecting) Use only when the schema has no comments
    or PI  as the context nodes
    langCode		ISO language code      language for skeleton errors, if available
    terminate= yes | no | true | false | assert  Terminate on the first failed assertion or successful report
                                         Note whether any output at all is generated depends on the XSLT implementation.
                                         
  The following parameters can be specified as Schematron variables in diagnostics, assertions and so on.
    fileNameParameter string	  
    fileDirParameter string				
    archiveNameParameter string	  In case of ZIP files
    archiveDirParameter string	  In case of ZIP files	 
    
 Experimental: USE AT YOUR OWN RISK   
    visit-text "true" "false"   Also visist text nodes for context. WARNING: NON_STARDARD.
    select-contents '' | 'key' | '//'   Select different implementation strategies
 
 Conventions: Meta-stylesheets that override this may use the following parameters
    generate-paths=true|false   generate the @location attribute with XPaths
    full-path-notation = 1|2|3  select the notation for the full paths: 1=computer, 2=human, 3=obsolescent
    diagnose= yes | no    Add the diagnostics to the assertion test in reports
 
-->

<!-- 
  XSLT VERSION SUPPORT

  XSLT 1:
     A schema using the standard XSLT 1 query binding will have a /schema/@queryBinding='xslt' or 
     nothing.

       * Note: XT does not implement key() and will die if given it. 
       * Add all formal parameters to default templates
       * Fix missing apply-templates from process-ns and add params back

  EXSLT:  Experimental support
     A schema using the EXSLT query binding will have a /schema/@queryBinding='exslt'.
     It is built on XSLT 1. After experience is gained, this binding is expected to be 
     formalized as part of ISO Schematron, which currently reserves the "exslt" name for this purpose.

     Some EXSLT engines have the extra functions built-in. For these, there is no need to
     provide library locations. For engines that require the functions, either hard code
     them in this script or provide them on the command-line argument.
      
  XSLT 2:   Experimental support
     A schema using the XSLT 2 query binding will have a /schema/@queryBinding='xslt2'.
     This binding is expected to be formalized as part of ISO
     Schematron, which currently reserves the "xslt2" name for this purpose.
     The xsl:import-schema, xsl:key and xsl:function elements are allowed as top elements. 
     
  XPATH:    Experimental support
     A schema using the XPATH query binding will have a /schema/@queryBinding='xpath'.
     It can run with XSLT 1 and is a strict superset of default ISO Schematron. After
     experience is gained, this binding is expected to be formalized as part of ISO
     Schematron, which currently reserves the "xpath" name for this purpose.

     The intent of this query binding is to support minimal non-XSLT implementations of 
     Schematron that use simple XPath APIs. These not only have fewer functions available
     than the XSLT version of XPath, but some of them do not support variables. 
     Consequently, in this binding, the <let> element and command-line variables passed
     to the schema should not be used? 
     The xsl:import-schema element is not allowed.
     
-->
<!--
   PROCESS INFORMATION
   
   This stylesheet compiles a Schematron schema (*.sch) into XSLT code (*.xsl). 
   The generated XSLT code can then be run against an XML file (*.xml, etc) and
   will produce validation results.
   
   The output of validation results is performed using named templates (process-*). 
   These can be overridden easily by making a new XSLT stylesheet that imports this 
   stylesheet but has its own version of the relevant process-* templates. Several
   of these invoking stylesheets are available: "iso_svrl.xsl", for example generates
   ISO Schematron Validation Report Language format results.
   
   In this version of the stylesheet, the ISO feature called "abstract patterns" is
   implemented using macro processing: a prior XSLT stage to which converts uses
   of abstract patterns into normal patterns. If you do not use abstract patterns,
   it is not necessary to preprocess the schema.
   
   To summarize, a basic process flow for some commandline processor is like this:
     XSLT -input=xxx.sch  -output=xxx.xsl  -stylesheet=iso_schematron_skeleton.xsl
     XSLT -input=document.xml  -output=xxx-document.results  -stylesheet=xxx.xsl
   
   iso_svrl.xslt is an implementation of Schematron that can use this skeleton and
   generate ISO SVRL reports. A process flow for some commandline processor would
   be like this:
     XSLT -input=xxx.sch  -output=xxx.xsl  -stylesheet=iso_svrl.xsl
     XSLT -input=document.xml  -output=xxx-document.results  -stylesheet=xxx.xsl
     
   It is not impossible that ultimately a third stage, to handle macro-preprocessing
   and inclusion, might be necessary. (The trade-off is in making this XSLT more
   complex compared to making the outer process more complex.)
             
  This version has been developed to work with 
     Saxon 9
  For versions for XSLT 1 processors, see www.xml.com

 Please note that if you are using SAXON and JAXP, then you should use 
  System.setProperty("javax.xml.transform.TransformerFactory",
                          "net.sf.saxon.TransformerFactoryImpl");
 rather than 
  System.setProperty("javax.xml.xpath.TransformerFactory",
                           "net.sf.saxon.TransformerFactoryImpl");
 which is does not work, at least for the versions of SAXON we tried.
--> 
<!--
  VERSION INFORMATION
     2010-04-14
     	* RJ Reorder call-template in exslt case only, report by BD
        * Add command line parameter 'terminate' which will terminate on first failed 
        assert and (optionally) successful report. 
     2010-01-24
     	* RJ Allow let elements to have direct content instead of @value 
     2009-08020 
        * RJ Give better scoping to resolution of abstract rules
     2009-07-07
     	* RJ Fix up warning on looking for @* on root  TODO CHECK!!!!
     2009-05-10 
     	* RJ Fix up incorrect use of tunnel
     2009-02-25 
        * RJ Fix up variable names so none are used twice in same template
      2009-02-19
        * RJ add experimental support for pattern/@documents 
        This takes an XPath that returns a sequence of strings, treats them as 
        relative URI references, and goes through these. It may need to be expanded
        to allow absolute paths.
    2008-09-19 RJ
        * Add mode schematron-select-full-path and param full-path-notation 
        
   2008-08-19
   		* RJ Add experimental property element.
   		This acts like the diagnostics element. An attribute rule/@properties,
   		assert/@properties and report/@properties can contain a list of ids
   		which reference a /schema/proporties/property/@id. This is a property
   		which will be carried over to the output, eg SVRL. It can have @name,
   		and the value is @value or the contents. Properties on rules are 
   		properties regardless of validation: they should apply to the subject
   		element. Properties on asserts and reports only make it through in the
   		negative case, at the moment, so they are really just a place to hold
   		structured info, eg. for diagnostics. The properties mechanism addresses
   		a bother with Schematron, that you have to post-process the document to
    	get any kind of structured data that might be nice in the output report. 
   		 
   		
   2008-08-14
   		* RJ move all messages into localization strings, add langCode parameter, 
   		named template to call external file in same directory as this xslt.
   		The file should be called sch-message-$langCode.xhtml ($langCode example "en")
  
   2008-08-11
   		* TT report/@flag was missing
   2008-08-06
   		* TT Top-level lets need to be implemented using xsl:param not xsl:variable
   		* TT xsl:param/@select must have XPath or not be specified
   		
   2008-08-04 
   		* RJ add saxon namespace to output to allow extension functions
   Version: 2008-07-28
   		* KH schematron-get-full-path-3 has [index] even on top step
   Version: 2008-07-24
   		* RJ clean out commented out namespace handling code 
   		* RJ allow schema/@queryBinding='xpath2' and warn if variables are
   		used
   		
   Version: 2008-07-14 update for XSLT2 and inclusion experiments
   		* RJ Clean up zero-length fragment test on include
   		* RJ Add experimental support for include containers
   		* RJ Add support for xsl:import-schema (request Paul Hermans)
   		* RJ Add support for xsl:function
   		* RJ For path generation, test for //iso:schema not just /iso:schema, for potential embedded Schematron support
   		* RJ Don't generate double error messages for old namespace elements
   		* RJ Experimental iso:rule/iso:title just kept as comment (bigger request Uche Ogbuji)
   		* RJ Fix bug that prevented including patterns in this (report Roger
   	Costello)
   Version: 2007-10-17
     Forked out version just to support SAXON 8 and potentially other XSLT2 processors.
       * RJ use xsl:namespace element
       * RJ use schold as namespace for old schematron, to prevent SAXON complaining
         when validating the Schematron schema for Schematron
       * RJ fix FULL-PATH for attributes

   Version: 2007-07-19
     Accept most changes in David Carlisle's fork, but continue as XSLT1 script: 
    	http://dpcarlisle.blogspot.com/search/label/schematron
    	* DPC Remove "optimize" parameter
    	* DPC Add autodetecting optimize parameter attribute to skip checking attribute
    	context
    	* DPC Add autodetecting optimize parameter only-child-elements turn off checking for 
    	comments and PIs
    	* DPC (Experimental: NON_STANDARD DANGER!) Add param visit-text to viist text
    	nodes too for context 
    	* DPC Fix inclusion syntax to allow #
    	* DPC Priorities count up from 1000 not down from 4000 to allow more rules
        * RJ Add new template for titles of schemas, with existing behaviour.  
        Override process-schema-title for custom processing of title
    		
    
   Version: 2007-04-04
   	* RJ debug mode param
	* RJ alter mixed test to only test mixed branches, so the same document
	could have old and new namespaces schemas in it, but each schema must
	be distinct, just so as not to overconstrain things.
   	* KH zero-length include/@href is fatal error, but allow debug mode
	* SB add hint on SAXON and JAXP
	* DC generate-full-path-1 generates XLST1 code by default
   Version: 2007-03-05
      	* AS Typo for EXSLT randome, improve comment
      	* KH get-schematron-full-path-2 needs to apply to attributes too
      	* DP document policy on extensions better
      	* DC use copy-of not copy for foreign elements
      	* DC add generate-path-2
      	* DC don't try to apply templates to attribute axis on attribute nodes, to
      	stop SAXON warning.
      	* RJ improve reporting of typos 
   
   Version: 2007-02-08
   		* KH Schematron fullpath implementation: @* handled twice and / missing
   		* KH Change stylesheetbody from named template to mode to allow implementers more flexibility.
   		  Move process-ns to outside the stylesheet body.
   		* DP, FG, fix handling of xslt:key
   		* FG no iso:title/@class
   		* Experimental optimization 'visit-no-attributes'
   		* KH Experimental added schematron-get-full-path-2 which gives prefixed version for humans
 		* DC Move stylesheet/@version generation to after namespace handling
 		* DC, FG EXSLT namespace handling code
 		* FG add ref and commented code from FG's page on namespaces
 		* Start adding normalize-space() to parameter code
 		* Add a space between diagnostics
   		   		 
   Version: 2007-01-22
   	* DP change = ($start) to = $start and =($phase) to =$phase 
   	to run under Saxon 8.8j
	* FG better title section using ( @id | iso:title)[last()]
	* Default query language binding is "xslt" not "xslt1"
  
   Version: 2007-01-19
   		* Simplify message newline code
   		* Remove termination and xpath appending to message options: 
   		   factor out as  iso_schematron_terminator.xsl
   		* Comment out XSLT2 namespace fix temporarily
  
   Version: 2007-01-18 (First beta candidate for comment)
          * DC remove xml:space="preserve"
          * FG improve comment on import statement
          * DC improve comments on invocation section
          * Add exploratory support for iso:schema[@queryBinding='xpath']
             by allowing it and warning as lets are found
          * Be strict about queryBinding spelling errors
          * Extra comments on the different queryBindings
          * KH Add option "message-paths" to generate XPath from output 
          * KH Add option "terminate" to halt with an error after the first assertion
          * KH refactor paths in schematron-full-path
          * Improve (?) namespace handling: no dummy attributes for prefix "xsl" generated
   
   Version: 2007-01-15
          * FG fix for calling templates
          * Add formal parameters to default templates: may help XSLT 2
          * Fix get-schematron-full-path
          * Include skeleton1-6 is commented out by default

   Version:2007-01-12 (Pre-beta release to Schematron-love-in maillist)
           * Add many extra parameters to the process-* calls, so that almost
           all the information in the schema can be provided to client programs.
           Also, rearrange the parameters to fit in with the ISO schema, which
           has "rich" and "linkable" attribute groups.
           * Warn on diagnostics with no ID once only
           * Improved path reporting, to handle for namespaces
           * Add process-title dummy template for API
           * Add command-line parameter allow-foreign (true|false) to suppress
            warnings one foreign elements and pass them through to the generated
            stylesheet
           * remove legacy templates for the old ASCC namespace and no namespace, 
              and use an import statement instead. Much cleaner now!
           * patterns use @id not @name
           * titles can contain sub-elements
           * start change iso:rule to allow attributes, PIs and comments 
           * the default process-* for inline elements add a leading and trailing 
             space, to reduce the chance of concatenation.
           * add comments to make the generated code clearer
           
   Version:2006-11-07 (ISO: first release private to schematron-love-in maillist for review)
           * Duplicate pattern templates, for handling ISO namespace
           * Add priority onto default and paragraph templates
           * Add namespace checks
           * Handle key in xsl namespace not iso
           * Add include
           * Improve namespace handling
           * Preliminary XSLT2 and EXSLT support
	       * Refactor iso:schema for clarity

    Version: 2003-05-26 
    	    * Fix bug with key 
    Version: 2003-04-16
    	   * handle 1.6 let expressions
    	   * make key use XSLT names, and allow anywhere
    Version: 2001-06-13
           * same skeleton now supports namespace or no namespace
           * parameters to handlers updated for all 1.5 attributes 
           * diagnostic hints supported: command-line option diagnose=yes|no
           * phases supported: command-line option phase=#ALL|...
           * abstract rules
           * compile-time error messages  
	   * add utility routine generate-id-from-path
          
    Contributors: Rick Jelliffe (original), Oliver Becker (architecture, XSLT2), 
             Miloslav Nic (diagnostic, phase, options), Ludwig Svenonius (abstract)
             Uche Ogbuji (misc. bug fixes), Jim Ancona (SAXON workaround),
	 	     Francis Norton (generate-id-from-path), Robert Leftwich, Bryan Rasmussen,
             Dave Pawson (include, fallback), Florent Georges (namespaces, exslt, attribute
             context), Benoit Maisonny (attribute context), John Dumps (process-message newline),
             Cliff Stanford (diagnostics and other newlines)

    
      
    
    KNOWN TYPICAL LIMITATIONS:
      * Don't use <iso:ns prefix="xsl" .../> with a namespace other than the standard
      XSLT one. This would be a bizarre thing to do anyway. 
      * Don't use other prefixes for the XSLT namespace either; some implementations will
      not handle it correctly.
     
     EXTENSIONS:
      ISO Schematron is designed as a framework with some standard query language
      bindings. If you need to support other features, please do so safely by making
      up your own @queryLanguage name: this makes it clear that your schema requires
      special features. For example, default ISO Schematron does not support user
      defined functions; so if you want to use the user defined function feature
      in XSLT, you need to have a schema with some queryBinding attribute name like
      "XSLT-with-my-functions" or whatever.
-->




