]> granicus.if.org Git - docbook-dsssl/commitdiff
Expand recursively until finished
authorNorman Walsh <ndw@nwalsh.com>
Wed, 7 Jan 2004 14:26:09 +0000 (14:26 +0000)
committerNorman Walsh <ndw@nwalsh.com>
Wed, 7 Jan 2004 14:26:09 +0000 (14:26 +0000)
docbook/relaxng/tools/expand.xsl

index 004e7b6d04f920c3c52697699026133f4bb69075..46f12263c7437c48d6c422a78b9c6185aee45392 100644 (file)
   <xsl:key name="defs" match="rng:define" use="@name"/>
 
   <xsl:template match="/">
-    <xsl:apply-templates/>
+    <xsl:call-template name="expand">
+      <xsl:with-param name="root" select="/"/>
+    </xsl:call-template>
   </xsl:template>
 
-  <xsl:template match="rng:ref" priority="2">
+  <xsl:template name="expand">
+    <xsl:param name="root"/>
+
+    <xsl:variable name="canExpand">
+      <xsl:apply-templates select="$root" mode="expandTest"/>
+    </xsl:variable>
+
+    <xsl:message>Expand: <xsl:value-of select="string-length($canExpand)"/></xsl:message>
+
+    <xsl:choose>
+      <xsl:when test="contains($canExpand, '1')">
+       <xsl:variable name="expanded">
+         <xsl:apply-templates select="$root" mode="expand"/>
+       </xsl:variable>
+       <xsl:call-template name="expand">
+         <xsl:with-param name="root" select="exsl:node-set($expanded)"/>
+       </xsl:call-template>
+      </xsl:when>
+      <xsl:otherwise>
+       <xsl:copy-of select="$root"/>
+      </xsl:otherwise>
+    </xsl:choose>
+  </xsl:template>
+
+  <!-- ====================================================================== -->
+
+  <xsl:template match="/" mode="expandTest">
+    <xsl:for-each select="//rng:ref">
+      <xsl:variable name="name" select="@name"/>
+      <xsl:variable name="ref" select="key('defs', @name)"/>
+
+      <xsl:choose>
+       <xsl:when test="ancestor::rng:define[@name=$name]">
+         <!-- don't expand recursively -->
+       </xsl:when>
+       <xsl:when test="$ref and $ref/rng:element">
+         <!-- don't expand element patterns -->
+       </xsl:when>
+       <xsl:when test="$ref and $ref/rng:empty">
+         <!-- don't expand empty patterns -->
+       </xsl:when>
+       <xsl:otherwise>1</xsl:otherwise>
+      </xsl:choose>
+    </xsl:for-each>
+  </xsl:template>
+
+  <!-- ====================================================================== -->
+
+  <xsl:template match="/" mode="expand">
+    <xsl:apply-templates mode="expand"/>
+  </xsl:template>
+
+  <xsl:template match="rng:ref" priority="2" mode="expand">
     <xsl:variable name="name" select="@name"/>
     <xsl:variable name="ref" select="key('defs', @name)"/>
 
     <xsl:choose>
       <xsl:when test="$ref and ancestor::rng:define[@name=$name]">
-       <!-- don't expand it recursively... -->
+       <!-- don't expand recursively -->
        <xsl:copy>
          <xsl:copy-of select="@*"/>
-         <xsl:apply-templates/>
+         <xsl:apply-templates mode="expand"/>
        </xsl:copy>
       </xsl:when>
-      <xsl:when test="$ref and ($ref/rng:element or $ref/rng:attribute)">
+      <xsl:when test="$ref and $ref/rng:element">
+       <!-- don't expand element patterns -->
        <xsl:copy>
          <xsl:copy-of select="@*"/>
-         <xsl:apply-templates/>
+         <xsl:apply-templates mode="expand"/>
        </xsl:copy>
       </xsl:when>
-      <xsl:when test="$ref and ($ref/rng:empty)">
+      <xsl:when test="$ref and $ref/rng:empty">
        <!-- just discard it -->
       </xsl:when>
       <xsl:when test="$ref">
       <xsl:otherwise>
        <xsl:copy>
          <xsl:copy-of select="@*"/>
-         <xsl:apply-templates/>
+         <xsl:apply-templates mode="expand"/>
        </xsl:copy>
       </xsl:otherwise>
     </xsl:choose>
   </xsl:template>
 
-  <xsl:template match="*">
+  <xsl:template match="rng:element" mode="expand">
+    <!--
+    <xsl:message>Expanding <xsl:value-of select="@name"/></xsl:message>
+    -->
+    <xsl:copy>
+      <xsl:copy-of select="@*"/>
+      <xsl:apply-templates mode="expand"/>
+    </xsl:copy>
+  </xsl:template>
+
+  <xsl:template match="*" mode="expand">
     <xsl:copy>
       <xsl:copy-of select="@*"/>
-      <xsl:apply-templates/>
+      <xsl:apply-templates mode="expand"/>
     </xsl:copy>
   </xsl:template>
 
-  <xsl:template match="comment()|processing-instruction()|text()">
+  <xsl:template match="comment()|processing-instruction()|text()" mode="expand">
     <xsl:copy/>
   </xsl:template>