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 / XSD2Schtrn.xsl < prev    next >
Encoding:
Extensible Markup Language  |  2005-07-21  |  4.4 KB  |  76 lines

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3.         based on an original transform by Eddie Robertsson
  4.         2001/04/21      fn: added support for included schemas
  5.         2001/06/27      er: changed XMl Schema prefix from xsd: to xs: and changed to the Rec namespace
  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:xs="http://www.w3.org/2001/XMLSchema">
  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="xs:schema">
  15.                 <!-- call the schema definition template ... -->
  16.                 <xsl:call-template name="gatherSchema">
  17.                         <!-- ... with current current root 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" 
  21.                         select="document(/xs:schema/xs:*[self::xs:include or self::xs:import or self::xs:redefine]/@schemaLocation)"/>
  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" 
  38.                                         select="document($includes/xs:schema/xs:*[self::xs:include or self::xs:import or self::xs:redefine]/@schemaLocation)"/>
  39.                                 </xsl:call-template>
  40.                         </xsl:when>
  41.                         <xsl:otherwise>
  42.                                 <!-- we have the complete set of included schemas, 
  43.                                 so now let's output the embedded schematron -->
  44.                                 <xsl:call-template name="output">
  45.                                         <xsl:with-param name="schemas" select="$schemas"/>
  46.                                 </xsl:call-template>
  47.                         </xsl:otherwise>
  48.                 </xsl:choose>
  49.         </xsl:template>
  50.         <!-- -->
  51.         <!-- output the schematron information -->
  52.         <!-- -->
  53.         <xsl:template name="output">
  54.                 <xsl:param name="schemas"/>
  55.                 <!-- -->
  56.                 <sch:schema>
  57.                         <!-- get header-type elements - eg title and especially ns -->
  58.                         <!-- title (just one) -->
  59.                         <xsl:copy-of select="$schemas//xs:appinfo/sch:title[1]"/>
  60.                         <!-- get remaining schematron schema children -->
  61.                         <!-- get non-blank namespace elements, dropping duplicates -->
  62.                         <xsl:for-each select="$schemas//xs:appinfo/sch:ns">
  63.                                 <xsl:if test="generate-id(.) = 
  64.                                 generate-id($schemas//xs:appinfo/sch:ns[@prefix = current()/@prefix][1])">
  65.                                         <xsl:copy-of select="."/>
  66.                                 </xsl:if>
  67.                         </xsl:for-each>
  68.                         <xsl:copy-of select="$schemas//xs:appinfo/sch:phase"/>
  69.                         <xsl:copy-of select="$schemas//xs:appinfo/sch:pattern"/>
  70.                         <sch:diagnostics>
  71.                                 <xsl:copy-of select="$schemas//xs:appinfo/sch:diagnostics/*"/>
  72.                         </sch:diagnostics>
  73.                 </sch:schema>
  74.         </xsl:template>
  75.         <!-- -->
  76. </xsl:transform>