home *** CD-ROM | disk | FTP | other *** search
/ Internet Magazine 2003 October / INTERNET108.ISO / pc / software / windows / building / xmlspy / xmlspyentcomplete5.exe / Data1.cab / _554BA644153E4F9B922F5CE7FED40DED < prev    next >
Encoding:
Extensible Markup Language  |  2002-09-04  |  9.7 KB  |  285 lines

  1. <?xml version="1.0"?>
  2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3.                 xmlns:exsl="http://exslt.org/common"
  4.                 xmlns:set="http://exslt.org/sets"
  5.                 exclude-result-prefixes="exsl set"
  6.                 version="1.0">
  7.  
  8. <!-- This module contains templates that match against HTML nodes. It is used
  9.      to post-process result tree fragments for some sorts of cleanup.
  10.      These templates can only ever be fired by a processor that supports
  11.      exslt:node-set(). -->
  12.  
  13. <!-- ==================================================================== -->
  14.  
  15. <!-- insert.html.p mode templates insert a particular RTF at the beginning
  16.      of the first paragraph in the primary RTF. -->
  17.  
  18. <xsl:template match="/" mode="insert.html.p">
  19.   <xsl:param name="mark" select="'?'"/>
  20.   <xsl:apply-templates mode="insert.html.p">
  21.     <xsl:with-param name="mark" select="$mark"/>
  22.   </xsl:apply-templates>
  23. </xsl:template>
  24.  
  25. <xsl:template match="*" mode="insert.html.p">
  26.   <xsl:param name="mark" select="'?'"/>
  27.   <xsl:copy>
  28.     <xsl:copy-of select="@*"/>
  29.     <xsl:apply-templates mode="insert.html.p">
  30.       <xsl:with-param name="mark" select="$mark"/>
  31.     </xsl:apply-templates>
  32.   </xsl:copy>
  33. </xsl:template>
  34.  
  35. <xsl:template match="p" mode="insert.html.p">
  36.   <xsl:param name="mark" select="'?'"/>
  37.   <xsl:copy>
  38.     <xsl:copy-of select="@*"/>
  39.     <xsl:if test="not(preceding::p)">
  40.       <xsl:copy-of select="$mark"/>
  41.     </xsl:if>
  42.     <xsl:apply-templates mode="insert.html.p">
  43.       <xsl:with-param name="mark" select="$mark"/>
  44.     </xsl:apply-templates>
  45.   </xsl:copy>
  46. </xsl:template>
  47.  
  48. <xsl:template match="text()|processing-instruction()|comment()" mode="insert.html.p">
  49.   <xsl:param name="mark" select="'?'"/>
  50.   <xsl:copy/>
  51. </xsl:template>
  52.  
  53. <!-- ==================================================================== -->
  54.  
  55. <!-- insert.html.text mode templates insert a particular RTF at the beginning
  56.      of the first text-node in the primary RTF. -->
  57.  
  58. <xsl:template match="/" mode="insert.html.text">
  59.   <xsl:param name="mark" select="'?'"/>
  60.   <xsl:apply-templates mode="insert.html.text">
  61.     <xsl:with-param name="mark" select="$mark"/>
  62.   </xsl:apply-templates>
  63. </xsl:template>
  64.  
  65. <xsl:template match="*" mode="insert.html.text">
  66.   <xsl:param name="mark" select="'?'"/>
  67.   <xsl:copy>
  68.     <xsl:copy-of select="@*"/>
  69.     <xsl:apply-templates mode="insert.html.text">
  70.       <xsl:with-param name="mark" select="$mark"/>
  71.     </xsl:apply-templates>
  72.   </xsl:copy>
  73. </xsl:template>
  74.  
  75. <xsl:template match="text()|processing-instruction()|comment()" mode="insert.html.text">
  76.   <xsl:param name="mark" select="'?'"/>
  77.  
  78.   <xsl:if test="not(preceding::text())">
  79.     <xsl:copy-of select="$mark"/>
  80.   </xsl:if>
  81.  
  82.   <xsl:copy/>
  83. </xsl:template>
  84.  
  85. <xsl:template match="processing-instruction()|comment()" mode="insert.html.text">
  86.   <xsl:param name="mark" select="'?'"/>
  87.   <xsl:copy/>
  88. </xsl:template>
  89.  
  90. <!-- ==================================================================== -->
  91.  
  92. <!-- unwrap.p mode templates remove blocks from HTML p elements (and
  93.      other places where blocks aren't allowed) -->
  94.  
  95. <xsl:template name="unwrap.p">
  96.   <xsl:param name="p"/>
  97.   <xsl:choose>
  98.     <xsl:when test="function-available('exsl:node-set')
  99.                     and function-available('set:leading')
  100.                     and function-available('set:trailing')">
  101.       <xsl:apply-templates select="exsl:node-set($p)" mode="unwrap.p"/>
  102.     </xsl:when>
  103.     <xsl:otherwise>
  104.       <xsl:copy-of select="$p"/>
  105.     </xsl:otherwise>
  106.   </xsl:choose>
  107. </xsl:template>
  108.  
  109. <xsl:template match="p" mode="unwrap.p">
  110.   <!-- xmlns:html is necessary for the xhtml stylesheet case -->
  111.   <xsl:variable name="blocks" xmlns:html="http://www.w3.org/1999/xhtml"
  112.                 select="address|blockquote|div|hr|h1|h2|h3|h4|h5|h6
  113.                         |layer|p|pre|table|dl|menu|ol|ul|form
  114.                         |html:address|html:blockquote|html:div|html:hr
  115.                         |html:h1|html:h2|html:h3|html:h4|html:h5|html:h6
  116.                         |html:layer|html:p|html:pre|html:table|html:dl
  117.                         |html:menu|html:ol|html:ul|html:form"/>
  118.   <xsl:choose>
  119.     <xsl:when test="$blocks">
  120.       <xsl:call-template name="unwrap.p.nodes">
  121.         <xsl:with-param name="wrap" select="."/>
  122.         <xsl:with-param name="first" select="1"/>
  123.         <xsl:with-param name="nodes" select="node()"/>
  124.         <xsl:with-param name="blocks" select="$blocks"/>
  125.       </xsl:call-template>
  126.     </xsl:when>
  127.     <xsl:otherwise>
  128.       <xsl:copy>
  129.         <xsl:copy-of select="@*"/>
  130.         <xsl:apply-templates mode="unwrap.p"/>
  131.       </xsl:copy>
  132.     </xsl:otherwise>
  133.   </xsl:choose>
  134. </xsl:template>
  135.  
  136. <xsl:template match="*" mode="unwrap.p">
  137.   <xsl:copy>
  138.     <xsl:copy-of select="@*"/>
  139.     <xsl:apply-templates mode="unwrap.p"/>
  140.   </xsl:copy>
  141. </xsl:template>
  142.  
  143. <xsl:template match="text()|processing-instruction()|comment()" mode="unwrap.p">
  144.   <xsl:copy/>
  145. </xsl:template>
  146.  
  147. <xsl:template name="unwrap.p.nodes">
  148.   <xsl:param name="wrap" select="."/>
  149.   <xsl:param name="first" select="0"/>
  150.   <xsl:param name="nodes"/>
  151.   <xsl:param name="blocks"/>
  152.   <xsl:variable name="block" select="$blocks[1]"/>
  153.  
  154.   <!-- This template should never get called if these functions aren't available -->
  155.   <!-- but this test is still necessary so that processors don't choke on the -->
  156.   <!-- function calls if they don't support the set: functions -->
  157.   <xsl:if test="function-available('set:leading')
  158.                 and function-available('set:trailing')">
  159.     <xsl:choose>
  160.       <xsl:when test="$blocks">
  161.         <xsl:variable name="leading" select="set:leading($nodes,$block)"/>
  162.         <xsl:variable name="trailing" select="set:trailing($nodes,$block)"/>
  163.  
  164.         <xsl:if test="($wrap/@id and $first = 1) or $leading">
  165.           <xsl:element name="{local-name($wrap)}" namespace="{namespace-uri($wrap)}">
  166.             <xsl:for-each select="$wrap/@*">
  167.               <xsl:if test="$first != 0 or local-name(.) != 'id'">
  168.                 <xsl:copy/>
  169.               </xsl:if>
  170.             </xsl:for-each>
  171.             <xsl:apply-templates select="$leading" mode="unwrap.p"/>
  172.           </xsl:element>
  173.         </xsl:if>
  174.  
  175.         <xsl:apply-templates select="$block" mode="unwrap.p"/>
  176.  
  177.         <xsl:if test="$trailing">
  178.           <xsl:call-template name="unwrap.p.nodes">
  179.             <xsl:with-param name="wrap" select="$wrap"/>
  180.             <xsl:with-param name="nodes" select="$trailing"/>
  181.             <xsl:with-param name="blocks" select="$blocks[position() > 1]"/>
  182.           </xsl:call-template>
  183.         </xsl:if>
  184.       </xsl:when>
  185.  
  186.       <xsl:otherwise>
  187.         <xsl:if test="($wrap/@id and $first = 1) or $nodes">
  188.           <xsl:element name="{local-name($wrap)}" namespace="{namespace-uri($wrap)}">
  189.             <xsl:for-each select="$wrap/@*">
  190.               <xsl:if test="$first != 0 or local-name(.) != 'id'">
  191.                 <xsl:copy/>
  192.               </xsl:if>
  193.             </xsl:for-each>
  194.             <xsl:apply-templates select="$nodes" mode="unwrap.p"/>
  195.           </xsl:element>
  196.         </xsl:if>
  197.       </xsl:otherwise>
  198.     </xsl:choose>
  199.   </xsl:if>
  200. </xsl:template>
  201.  
  202. <!-- ==================================================================== -->
  203. <!-- make.verbatim.mode replaces spaces and newlines -->
  204.  
  205. <xsl:template match="/" mode="make.verbatim.mode">
  206.   <xsl:apply-templates mode="make.verbatim.mode"/>
  207. </xsl:template>
  208.  
  209. <xsl:template match="*" mode="make.verbatim.mode">
  210.   <xsl:copy>
  211.     <xsl:copy-of select="@*"/>
  212.     <xsl:apply-templates mode="make.verbatim.mode"/>
  213.   </xsl:copy>
  214. </xsl:template>
  215.  
  216. <xsl:template match="processing-instruction()|comment()" mode="make.verbatim.mode">
  217.   <xsl:copy/>
  218. </xsl:template>
  219.  
  220. <xsl:template match="text()" mode="make.verbatim.mode">
  221.   <xsl:variable name="text" select="translate(., ' ', ' ')"/>
  222.  
  223.   <xsl:choose>
  224.     <xsl:when test="not(contains($text, ' '))">
  225.       <xsl:value-of select="$text"/>
  226.     </xsl:when>
  227.  
  228.     <xsl:otherwise>
  229.       <xsl:variable name="len" select="string-length($text)"/>
  230.  
  231.       <xsl:choose>
  232.         <xsl:when test="$len = 1">
  233.           <br/><xsl:text> </xsl:text>
  234.         </xsl:when>
  235.  
  236.         <xsl:otherwise>
  237.           <xsl:variable name="half" select="$len div 2"/>
  238.           <xsl:call-template name="make-verbatim-recursive">
  239.             <xsl:with-param name="text" select="substring($text, 1, $half)"/>
  240.           </xsl:call-template>
  241.           <xsl:call-template name="make-verbatim-recursive">
  242.             <xsl:with-param name="text"
  243.                             select="substring($text, ($half + 1), $len)"/>
  244.           </xsl:call-template>
  245.         </xsl:otherwise>
  246.       </xsl:choose>
  247.     </xsl:otherwise>
  248.   </xsl:choose>
  249. </xsl:template>
  250.  
  251. <xsl:template name="make-verbatim-recursive">
  252.   <xsl:param name="text" select="''"/>
  253.  
  254.   <xsl:choose>
  255.     <xsl:when test="not(contains($text, ' '))">
  256.       <xsl:value-of select="$text"/>
  257.     </xsl:when>
  258.  
  259.     <xsl:otherwise>
  260.       <xsl:variable name="len" select="string-length($text)"/>
  261.  
  262.       <xsl:choose>
  263.         <xsl:when test="$len = 1">
  264.           <br/><xsl:text> </xsl:text>
  265.         </xsl:when>
  266.  
  267.         <xsl:otherwise>
  268.           <xsl:variable name="half" select="$len div 2"/>
  269.           <xsl:call-template name="make-verbatim-recursive">
  270.             <xsl:with-param name="text" select="substring($text, 1, $half)"/>
  271.           </xsl:call-template>
  272.           <xsl:call-template name="make-verbatim-recursive">
  273.             <xsl:with-param name="text"
  274.                     select="substring($text, ($half + 1), $len)"/>
  275.           </xsl:call-template>
  276.         </xsl:otherwise>
  277.       </xsl:choose>
  278.     </xsl:otherwise>
  279.   </xsl:choose>
  280. </xsl:template>
  281.  
  282. <!-- ==================================================================== -->
  283.  
  284. </xsl:stylesheet>
  285.