home *** CD-ROM | disk | FTP | other *** search
/ MacFormat UK 160 - Disc 2 / MF_UK_160_2.iso / pc / DiscContent / Trials / oxygen / lib / oxygen.jar / builtin / RNG2Schtrn.xsl < prev    next >
Encoding:
Extensible Markup Language  |  2005-07-21  |  3.1 KB  |  74 lines

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3.     Stylesheet for extracting Schematron information from a RELAX-NG schema.
  4.     Based on the stylesheet for extracting Schematron information from W3C XML Schema.
  5.     Created by Eddie Robertsson 2002/06/01
  6. -->
  7. <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  8. xmlns:sch="http://www.ascc.net/xml/schematron" xmlns:rng="http://relaxng.org/ns/structure/1.0">
  9.     <!-- Set the output to be XML with an XML declaration and use indentation -->
  10.     <xsl:output method="xml" omit-xml-declaration="no" indent="yes" standalone="yes"/>
  11.     <!-- -->
  12.     <!-- match schema and call recursive template to extract included schemas -->
  13.     <!-- -->
  14.     <xsl:template match="/rng:grammar | /rng:element">
  15.         <!-- call the schema definition template ... -->
  16.         <xsl:call-template name="gatherSchema">
  17.             <!-- ... with current node as the $schemas parameter ... -->
  18.             <xsl:with-param name="schemas" select="."/>
  19.             <!-- ... and any includes in the $include parameter -->
  20.             <xsl:with-param name="includes" select="document(/rng:grammar/rng:include/@href
  21. | //rng:externalRef/@href)"/>
  22.         </xsl:call-template>
  23.     </xsl:template>
  24.     <!-- -->
  25.     <!-- gather all included schemas into a single parameter variable -->
  26.     <!-- -->
  27.     <xsl:template name="gatherSchema">
  28.         <xsl:param name="schemas"/>
  29.         <xsl:param name="includes"/>
  30.         <xsl:choose>
  31.             <xsl:when test="count($schemas) < count($schemas | $includes)">
  32.                 <!-- when $includes includes something new, recurse ... -->
  33.                 <xsl:call-template name="gatherSchema">
  34.                     <!-- ... with current $includes added to the $schemas parameter ... -->
  35.                     <xsl:with-param name="schemas" select="$schemas | $includes"/>
  36.                     <!-- ... and any *new* includes in the $include parameter -->
  37.                     <xsl:with-param name="includes" select="document($includes/rng:grammar/rng:include/@href
  38. | $includes//rng:externalRef/@href)"/>
  39.                 </xsl:call-template>
  40.             </xsl:when>
  41.             <xsl:otherwise>
  42.                 <!-- we have the complete set of included schemas, so now let's output the embedded schematron -->
  43.                 <xsl:call-template name="output">
  44.                     <xsl:with-param name="schemas" select="$schemas"/>
  45.                 </xsl:call-template>
  46.             </xsl:otherwise>
  47.         </xsl:choose>
  48.     </xsl:template>
  49.     <!-- -->
  50.     <!-- output the schematron information -->
  51.     <!-- -->
  52.     <xsl:template name="output">
  53.         <xsl:param name="schemas"/>
  54.         <!-- -->
  55.         <sch:schema>
  56.             <!-- get header-type elements - eg title and especially ns -->
  57.             <!-- title (just one) -->
  58.             <xsl:copy-of select="$schemas//sch:title[1]"/>
  59.             <!-- get remaining schematron schema children -->
  60.             <!-- get non-blank namespace elements, dropping duplicates -->
  61.             <xsl:for-each select="$schemas//sch:ns">
  62.                 <xsl:if test="generate-id(.) = generate-id($schemas//sch:ns[@prefix = current()/@prefix][1])">
  63.                     <xsl:copy-of select="."/>
  64.                 </xsl:if>
  65.             </xsl:for-each>
  66.             <xsl:copy-of select="$schemas//sch:phase"/>
  67.             <xsl:copy-of select="$schemas//sch:pattern"/>
  68.             <sch:diagnostics>
  69.                 <xsl:copy-of select="$schemas//sch:diagnostics/*"/>
  70.             </sch:diagnostics>
  71.         </sch:schema>
  72.     </xsl:template>
  73.     <!-- -->
  74. </xsl:transform>