home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-cocoon-addon-1.4.9-installer.exe / forms-field-styling.xsl < prev    next >
Encoding:
Extensible Markup Language  |  2004-07-12  |  17.6 KB  |  523 lines

  1. <?xml version="1.0"?>
  2. <!--
  3.   Copyright 1999-2004 The Apache Software Foundation
  4.  
  5.   Licensed under the Apache License, Version 2.0 (the "License");
  6.   you may not use this file except in compliance with the License.
  7.   You may obtain a copy of the License at
  8.  
  9.       http://www.apache.org/licenses/LICENSE-2.0
  10.  
  11.   Unless required by applicable law or agreed to in writing, software
  12.   distributed under the License is distributed on an "AS IS" BASIS,
  13.   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.   See the License for the specific language governing permissions and
  15.   limitations under the License.
  16. -->
  17. <xsl:stylesheet version="1.0"
  18.                 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  19.                 xmlns:fi="http://apache.org/cocoon/forms/1.0#instance"
  20.                 exclude-result-prefixes="fi">
  21.   <!--+
  22.       | This stylesheet is designed to be included by 'forms-samples-styling.xsl'.
  23.       | Version CVS $Id: forms-field-styling.xsl,v 1.8 2004/04/22 14:27:58 mpo Exp $
  24.       +-->
  25.  
  26.   <!-- Location of the resources directory, where JS libs and icons are stored -->
  27.   <xsl:param name="resources-uri">resources</xsl:param>
  28.  
  29.   <xsl:template match="head" mode="forms-field">
  30.     <script src="{$resources-uri}/forms-lib.js" type="text/javascript"/>
  31.     <link rel="stylesheet" type="text/css" href="{$resources-uri}/forms.css"/>
  32.   </xsl:template>
  33.  
  34.   <xsl:template match="body" mode="forms-field">
  35.     <xsl:copy-of select="@*"/>
  36.     <xsl:attribute name="onload">forms_onload(); <xsl:value-of select="@onload"/></xsl:attribute>
  37.   </xsl:template>
  38.  
  39.   <!--+
  40.       | Generic fi:field : produce an <input>
  41.       +-->
  42.   <xsl:template match="fi:field">
  43.     <input name="{@id}" id="{@id}" value="{fi:value}" title="{fi:hint}" type="text">
  44.       <xsl:apply-templates select="." mode="styling"/>
  45.     </input>
  46.     <xsl:apply-templates select="." mode="common"/>
  47.   </xsl:template>
  48.  
  49.   <!--+
  50.       | Common stuff like fi:validation-message, @required.
  51.       +-->
  52.   <xsl:template match="fi:*" mode="common">
  53.     <!-- validation message -->
  54.     <xsl:apply-templates select="fi:validation-message"/>
  55.     <!-- required mark -->
  56.     <xsl:if test="@required='true'">
  57.       <span class="forms-field-required"> * </span>
  58.     </xsl:if>
  59.   </xsl:template>
  60.  
  61.   <!--+
  62.       | Handling the common styling. You may only add attributes to the output
  63.       | in this template as later processing might add attributes too, for
  64.       | example @checked or @selected
  65.       +-->
  66.   <xsl:template match="fi:*" mode="styling">
  67.     <xsl:apply-templates select="fi:styling/@*" mode="styling"/>
  68.  
  69.     <!--+ 
  70.         | @listbox-size needs to be handled separately as even if it is not
  71.         | specified some output (@size) must be generated.
  72.         +-->
  73.     <xsl:if test="self::fi:field[fi:selection-list][fi:styling/@list-type = 'listbox'] or
  74.                   self::fi:multivaluefield[not(fi:styling/@list-type = 'checkbox')]">
  75.       <xsl:variable name="size">
  76.         <xsl:value-of select="fi:styling/@listbox-size"/>
  77.         <xsl:if test="not(fi:styling/@listbox-size)">5</xsl:if>
  78.       </xsl:variable>
  79.       <xsl:attribute name="size">
  80.         <xsl:value-of select="$size"/>
  81.       </xsl:attribute>
  82.     </xsl:if>
  83.   </xsl:template>
  84.  
  85.   <xsl:template match="fi:styling/@*" mode="styling">
  86.     <xsl:copy-of select="."/>
  87.   </xsl:template>
  88.  
  89.   <xsl:template match="fi:styling/@submit-on-change" mode="styling">
  90.     <xsl:if test=". = 'true'">
  91.       <xsl:attribute name="onchange">forms_submitForm(this)</xsl:attribute>
  92.     </xsl:if>
  93.   </xsl:template>
  94.  
  95.   <xsl:template match="fi:styling/@list-type | fi:styling/@list-orientation |
  96.                        fi:styling/@listbox-size | fi:styling/@format | fi:styling/@layout"
  97.                 mode="styling">
  98.     <!--+
  99.         | Ignore marker attributes so they don't go into the resuling HTML.
  100.         +-->
  101.   </xsl:template>
  102.  
  103.   <xsl:template match="fi:styling/@type" mode="styling">
  104.     <!--+ 
  105.         | Do we have a duplicate semantic usage of @type?
  106.         | @type is only a marker for the stylesheet in general, but some of the
  107.         | types must/should be in the HTML output too.
  108.         +-->
  109.     <xsl:variable name="validHTMLTypes"
  110.                   select="'text hidden textarea checkbox radio password image reset submit'"/>
  111.     <xsl:if test="normalize-space(.) and
  112.                   contains(concat(' ', $validHTMLTypes, ' '), concat(' ', ., ' '))">
  113.       <xsl:copy-of select="."/>
  114.     </xsl:if>
  115.   </xsl:template>
  116.  
  117.   <!--+
  118.       |
  119.       +-->
  120.   <xsl:template match="fi:validation-message">
  121.     <a href="#" class="forms-validation-message" onclick="alert('{normalize-space(.)}');return false;"> ! </a>
  122.   </xsl:template>
  123.  
  124.   <!--+
  125.       | Hidden fi:field : produce input with type='hidden'
  126.       +-->
  127.   <xsl:template match="fi:field[fi:styling/@type='hidden']" priority="2">
  128.     <input type="hidden" name="{@id}" id="{@id}" value="{fi:value}">
  129.       <xsl:apply-templates select="." mode="styling"/>
  130.     </input>
  131.   </xsl:template>
  132.  
  133.   <!--+
  134.       | fi:field with a selection list and @list-type 'radio' : produce
  135.       | radio-buttons oriented according to @list-orientation
  136.       | ("horizontal" or "vertical" - default)
  137.       +-->
  138.   <xsl:template match="fi:field[fi:selection-list][fi:styling/@list-type='radio']" priority="2">
  139.     <xsl:variable name="id" select="@id"/>
  140.     <xsl:variable name="value" select="fi:value"/>
  141.     <xsl:variable name="vertical" select="string(fi:styling/@list-orientation) != 'horizontal'"/>
  142.     <xsl:choose>
  143.       <xsl:when test="$vertical">
  144.         <table cellpadding="0" cellspacing="0" border="0" title="{fi:hint}">
  145.           <xsl:for-each select="fi:selection-list/fi:item">
  146.             <tr>
  147.               <td>
  148.                 <input type="radio" id="{generate-id()}" name="{$id}" value="{@value}">
  149.                   <xsl:if test="@value = $value">
  150.                     <xsl:attribute name="checked">checked</xsl:attribute>
  151.                   </xsl:if>
  152.                   <xsl:apply-templates select="." mode="styling"/>
  153.                 </input>
  154.               </td>
  155.               <td>
  156.                 <label for="{generate-id()}"><xsl:copy-of select="fi:label/node()"/></label>
  157.               </td>
  158.               <xsl:if test="position() = 1">
  159.                 <td rowspan="{count(../fi:item)}">
  160.                   <xsl:apply-templates select="../.." mode="common"/>
  161.                 </td>
  162.               </xsl:if>
  163.             </tr>
  164.           </xsl:for-each>
  165.         </table>
  166.       </xsl:when>
  167.       <xsl:otherwise>
  168.         <span title="{fi:hint}">
  169.           <xsl:for-each select="fi:selection-list/fi:item">
  170.             <input type="radio" id="{generate-id()}" name="{$id}" value="{@value}">
  171.               <xsl:if test="@value = $value">
  172.                 <xsl:attribute name="checked">checked</xsl:attribute>
  173.               </xsl:if>
  174.               <xsl:apply-templates select="." mode="styling"/>
  175.             </input>
  176.             <label for="{generate-id()}"><xsl:copy-of select="fi:label/node()"/></label>
  177.           </xsl:for-each>
  178.         </span>
  179.         <xsl:apply-templates select="." mode="common"/>
  180.       </xsl:otherwise>
  181.     </xsl:choose>
  182.   </xsl:template>
  183.  
  184.   <!--+
  185.       | fi:field with a selection list (not 'radio' style)
  186.       | Rendering depends on the attributes of fi:styling :
  187.       | - if @list-type is "listbox" : produce a list box with @listbox-size visible
  188.       |   items (default 5)
  189.       | - otherwise, produce a dropdown menu
  190.       +-->
  191.   <xsl:template match="fi:field[fi:selection-list]" priority="1">
  192.     <xsl:variable name="value" select="fi:value"/>
  193.  
  194.     <!-- dropdown or listbox -->
  195.     <select title="{fi:hint}" id="{@id}" name="{@id}">
  196.       <xsl:apply-templates select="." mode="styling"/>
  197.       <xsl:for-each select="fi:selection-list/fi:item">
  198.         <option value="{@value}">
  199.           <xsl:if test="@value = $value">
  200.             <xsl:attribute name="selected">selected</xsl:attribute>
  201.           </xsl:if>
  202.           <xsl:copy-of select="fi:label/node()"/>
  203.         </option>
  204.       </xsl:for-each>
  205.     </select>
  206.     <xsl:apply-templates select="." mode="common"/>
  207.   </xsl:template>
  208.  
  209.   <!--+
  210.       | fi:field with a selection list and @type 'output'
  211.       +-->
  212.   <xsl:template match="fi:field[fi:selection-list][fi:styling/@type='output']" priority="3">
  213.     <xsl:variable name="value" select="fi:value"/>
  214.     <xsl:variable name="selected" select="fi:selection-list/fi:item[@value = $value]"/>
  215.     <xsl:choose>
  216.       <xsl:when test="$selected/fi:label">
  217.         <xsl:apply-templates select="$selected/fi:label"/>
  218.       </xsl:when>
  219.       <xsl:otherwise>
  220.         <xsl:value-of select="$value"/>
  221.       </xsl:otherwise>
  222.     </xsl:choose>
  223.   </xsl:template>
  224.  
  225.   <!--+
  226.       | fi:field with @type 'textarea'
  227.       +-->
  228.   <xsl:template match="fi:field[fi:styling/@type='textarea']">
  229.     <textarea id="{@id}" name="{@id}" title="{fi:hint}">
  230.       <xsl:apply-templates select="." mode="styling"/>
  231.       <!-- remove carriage-returns (occurs on certain versions of IE and doubles linebreaks at each submit) -->
  232.       <xsl:copy-of select="translate(fi:value/node(), ' ', '')"/>
  233.     </textarea>
  234.     <xsl:apply-templates select="." mode="common"/>
  235.   </xsl:template>
  236.  
  237.   <!--+
  238.       | fi:field with @type 'output' and fi:output are both rendered as text
  239.       +-->
  240.   <xsl:template match="fi:output | fi:field[fi:styling/@type='output']" priority="2">
  241.     <xsl:copy-of select="fi:value/node()"/>
  242.   </xsl:template>
  243.  
  244.   <!--+
  245.       | Labels for form elements.
  246.       +-->
  247.   <xsl:template match="fi:*" mode="label">
  248.     <label for="{@id}" title="{fi:hint}">
  249.       <xsl:copy-of select="fi:label/node()"/>
  250.     </label>
  251.   </xsl:template>
  252.  
  253.   <!--+
  254.       | Labels for pure outputs must not contain <label/> as there is no element to point to.
  255.       +-->
  256.   <xsl:template match="fi:output | fi:field[fi:styling/@type='output']" mode="label">
  257.     <xsl:copy-of select="fi:label/node()"/>
  258.   </xsl:template>
  259.  
  260.   <!--+
  261.       | fi:booleanfield : produce a checkbox
  262.       +-->
  263.   <xsl:template match="fi:booleanfield">
  264.     <input id="{@id}" type="checkbox" value="true" name="{@id}" title="{fi:hint}">
  265.       <xsl:apply-templates select="." mode="styling"/>
  266.       <xsl:if test="fi:value = 'true'">
  267.         <xsl:attribute name="checked">checked</xsl:attribute>
  268.       </xsl:if>
  269.     </input>
  270.     <xsl:apply-templates select="." mode="common"/>
  271.   </xsl:template>
  272.  
  273.   <!--+
  274.       | fi:booleanfield with @type 'output' : rendered as text
  275.       +-->
  276.   <xsl:template match="fi:booleanfield[fi:styling/@type='output']">
  277.     <xsl:choose>
  278.       <xsl:when test="fi:value = 'true'">
  279.         yes
  280.       </xsl:when>
  281.       <xsl:otherwise>
  282.         no
  283.       </xsl:otherwise>
  284.     </xsl:choose>
  285.   </xsl:template>
  286.  
  287.   <!--+
  288.       | fi:action
  289.       +-->
  290.   <xsl:template match="fi:action">
  291.     <input id="{@id}" type="submit" name="{@id}" title="{fi:hint}">
  292.       <xsl:attribute name="value"><xsl:value-of select="fi:label/node()"/></xsl:attribute>
  293.       <xsl:apply-templates select="." mode="styling"/>
  294.     </input>
  295.   </xsl:template>
  296.  
  297.   <!--+
  298.       | fi:continuation-id : produce a hidden "continuation-id" input
  299.       +-->
  300.   <xsl:template match="fi:continuation-id">
  301.     <xsl:variable name="name">
  302.       <xsl:value-of select="@name"/>
  303.       <xsl:if test="not(@name)">continuation-id</xsl:if>
  304.     </xsl:variable>
  305.     <input name="{$name}" type="hidden" value="{.}"/>
  306.   </xsl:template>
  307.  
  308.   <!--+
  309.       | fi:multivaluefield : produce a list of checkboxes
  310.       +-->
  311.   <xsl:template match="fi:multivaluefield[fi:styling/@list-type='checkbox']">
  312.     <xsl:variable name="id" select="@id"/>
  313.     <xsl:variable name="values" select="fi:values/fi:value/text()"/>
  314.  
  315.     <span title="{fi:hint}">
  316.       <xsl:for-each select="fi:selection-list/fi:item">
  317.         <xsl:variable name="value" select="@value"/>
  318.         <input id="{generate-id()}" type="checkbox" value="{@value}" name="{$id}">
  319.           <xsl:if test="$values[. = $value]">
  320.             <xsl:attribute name="checked">checked</xsl:attribute>
  321.           </xsl:if>
  322.         </input>
  323.         <label for="{generate-id()}"><xsl:copy-of select="fi:label/node()"/></label>
  324.         <br/>
  325.       </xsl:for-each>
  326.     </span>
  327.     <xsl:apply-templates select="." mode="common"/>
  328.   </xsl:template>
  329.  
  330.   <!--+
  331.       | fi:multivaluefield : produce a multiple-selection list
  332.       +-->
  333.   <xsl:template match="fi:multivaluefield">
  334.     <xsl:variable name="id" select="@id"/>
  335.     <xsl:variable name="values" select="fi:values/fi:value/text()"/>
  336.  
  337.     <span title="{fi:hint}">
  338.       <select id="{@id}" name="{$id}" multiple="multiple">
  339.         <xsl:apply-templates select="." mode="styling"/>
  340.         <xsl:for-each select="fi:selection-list/fi:item">
  341.           <xsl:variable name="value" select="@value"/>
  342.           <option value="{$value}">
  343.             <xsl:if test="$values[. = $value]">
  344.               <xsl:attribute name="selected">selected</xsl:attribute>
  345.             </xsl:if>
  346.             <xsl:copy-of select="fi:label/node()"/>
  347.           </option>
  348.         </xsl:for-each>
  349.       </select>
  350.     </span>
  351.     <xsl:apply-templates select="." mode="common"/>
  352.   </xsl:template>
  353.  
  354.   <!--+
  355.       | fi:upload
  356.       +-->
  357.   <xsl:template match="fi:upload">
  358.     <xsl:choose>
  359.       <xsl:when test="fi:value">
  360.         <!-- Has a value (filename): display it with a change button -->
  361.         <span title="{fi:hint}">
  362.           [<xsl:value-of select="fi:value"/>] <input type="submit" id="{@id}" name="{@id}" value="..."/>
  363.         </span>
  364.       </xsl:when>
  365.       <xsl:otherwise>
  366.         <input type="file" id="{@id}" name="{@id}" title="{fi:hint}" accept="{@mime-types}"/>
  367.       </xsl:otherwise>
  368.     </xsl:choose>
  369.     <xsl:apply-templates select="." mode="common"/>
  370.   </xsl:template>
  371.  
  372.   <!--+
  373.       | fi:repeater
  374.       +-->
  375.   <xsl:template match="fi:repeater">
  376.     <input type="hidden" name="{@id}.size" value="{@size}"/>
  377.     <table border="1">
  378.       <tr>
  379.         <xsl:for-each select="fi:headings/fi:heading">
  380.           <th><xsl:value-of select="."/></th>
  381.         </xsl:for-each>
  382.       </tr>
  383.       <xsl:apply-templates select="fi:repeater-row"/>
  384.     </table>
  385.   </xsl:template>
  386.  
  387.   <!--+
  388.       | fi:repeater-row
  389.       +-->
  390.   <xsl:template match="fi:repeater-row">
  391.     <tr>
  392.       <xsl:for-each select="*">
  393.         <td>
  394.           <xsl:apply-templates select="."/>
  395.         </td>
  396.       </xsl:for-each>
  397.     </tr>
  398.   </xsl:template>
  399.  
  400.   <!--+
  401.       | fi:repeater-size
  402.       +-->
  403.   <xsl:template match="fi:repeater-size">
  404.     <input type="hidden" name="{@id}.size" value="{@size}"/>
  405.   </xsl:template>
  406.  
  407.   <!--+
  408.       | fi:form-template|fi:form-generated 
  409.       +-->
  410.   <xsl:template match="fi:form-template|fi:form-generated">
  411.     <form>
  412.       <xsl:copy-of select="@*"/>
  413.       <xsl:attribute name="onsubmit">forms_onsubmit(); <xsl:value-of select="@onsubmit"/></xsl:attribute>
  414.       <!-- hidden field to store the submit id -->
  415.       <div><input type="hidden" name="forms_submit_id"/></div>
  416.       <xsl:apply-templates/>
  417.       
  418.       <!-- TODO: consider putting this in the xml stream from the generator? -->
  419.       <xsl:if test="self::fi:form-generated">
  420.         <input type="submit"/>
  421.       </xsl:if>
  422.     </form>
  423.   </xsl:template>
  424.  
  425.   <!--+
  426.       | fi:form
  427.       +-->
  428.   <xsl:template match="fi:form">
  429.     <table border="1">
  430.       <xsl:for-each select="fi:widgets/*">
  431.         <tr>
  432.           <xsl:choose>
  433.             <xsl:when test="self::fi:repeater">
  434.               <td colspan="2">
  435.                 <xsl:apply-templates select="."/>
  436.               </td>
  437.             </xsl:when>
  438.             <xsl:when test="self::fi:booleanfield">
  439.               <td> </td>
  440.               <td>
  441.                 <xsl:apply-templates select="."/>
  442.                 <xsl:text> </xsl:text>
  443.                 <xsl:copy-of select="fi:label"/>
  444.               </td>
  445.             </xsl:when>
  446.             <xsl:otherwise>
  447.               <td>
  448.                 <xsl:copy-of select="fi:label"/>
  449.               </td>
  450.               <td>
  451.                 <xsl:apply-templates select="."/>
  452.               </td>
  453.             </xsl:otherwise>
  454.           </xsl:choose>
  455.         </tr>
  456.       </xsl:for-each>
  457.     </table>
  458.   </xsl:template>
  459.  
  460.   <xsl:template match="fi:aggregatefield">
  461.     <input id="{@id}" name="{@id}" value="{fi:value}" title="{fi:hint}">
  462.       <xsl:apply-templates select="." mode="styling"/>
  463.     </input>
  464.     <xsl:apply-templates select="." mode="common"/>
  465.   </xsl:template>
  466.  
  467.   <xsl:template match="fi:messages">
  468.     <xsl:if test="fi:message">
  469.       <xsl:copy-of select="fi:label/node()"/>:
  470.       <ul>
  471.         <xsl:for-each select="fi:message">
  472.           <li><xsl:apply-templates/></li>
  473.         </xsl:for-each>
  474.       </ul>
  475.     </xsl:if>
  476.   </xsl:template>
  477.  
  478.   <xsl:template match="fi:validation-errors">
  479.     <xsl:variable name="header">
  480.       <xsl:choose>
  481.         <xsl:when test="header">
  482.           <xsl:copy-of select="header"/>
  483.         </xsl:when>
  484.         <xsl:otherwise>
  485.           <p class="forms-validation-errors">The following errors have been detected (marked with !):</p>
  486.         </xsl:otherwise>
  487.       </xsl:choose>
  488.     </xsl:variable>
  489.     <xsl:variable name="footer">
  490.       <xsl:choose>
  491.         <xsl:when test="footer">
  492.           <xsl:copy-of select="footer"/>
  493.         </xsl:when>
  494.         <xsl:otherwise>
  495.           <p class="forms-validation-errors">Please, correct them and re-submit the form.</p>
  496.         </xsl:otherwise>
  497.       </xsl:choose>
  498.     </xsl:variable>
  499.     <xsl:variable name="frm" select="ancestor::fi:form-template"/>
  500.     <xsl:if test="$frm and $frm//fi:validation-message">
  501.       <xsl:copy-of select="$header"/>
  502.       <ul>
  503.         <xsl:for-each select="$frm//fi:validation-message">
  504.           <li class="forms-validation-error">
  505.             <xsl:if test="../fi:label">
  506.               <xsl:value-of select="../fi:label"/><xsl:text>: </xsl:text>
  507.             </xsl:if>
  508.             <xsl:value-of select="."/>
  509.           </li>
  510.         </xsl:for-each>
  511.       </ul>
  512.       <xsl:copy-of select="$footer"/>
  513.     </xsl:if>
  514.   </xsl:template>
  515.  
  516.   <xsl:template match="@*|node()" priority="-1">
  517.     <xsl:copy>
  518.       <xsl:apply-templates select="@*|node()"/>
  519.     </xsl:copy>
  520.   </xsl:template>
  521.  
  522. </xsl:stylesheet>
  523.