Search

Hi I’m trying to get figure out best practices for writing XSLT and cut down on repeated code. I was wondering if anyone could give me some advice on how to improve my Send CV form below:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template name="send-cv-form">

<xsl:choose>

    <xsl:when test="not(events/send-cv) or events/send-cv/@result = 'error'">
        <a name="send-cv-form-anchor" />
        <xsl:if test="events/send-cv/@result = 'error'">
          <p class="errors-overview">There was a problem while attempting to submit your message. Please check that all the required fields have been filled out correctly.</p>
        </xsl:if>
        <form id="send-cv-form" method="post" action="#send-cv-form-anchor" enctype="multipart/form-data">
          <fieldset>
            <div class="form-element clearfix">
            <xsl:if test="events/send-cv/name/@type = 'missing' or events/send-cv/name/@type = 'invalid'">
              <xsl:attribute name="class">form-element clearfix error</xsl:attribute>
            </xsl:if>
              <label for="name">Name: <span class="required">* Required</span></label>
              <input type="text" id="name" name="fields[name]" value="{events/send-cv/post-values/name}" class="textbox" />
              <xsl:if test="events/send-cv/name/@type = 'missing' or events/contact-us/name/@type = 'invalid'">
                <span class="error-message">This field is required.</span>
              </xsl:if>
            </div>
            <div class="form-element clearfix">
              <xsl:if test="events/send-cv/email/@type = 'missing' or events/send-cv/email/@type = 'invalid'">
                <xsl:attribute name="class">form-element clearfix error</xsl:attribute>
              </xsl:if>
              <label for="email">Email: <span class="required">* Required</span></label>
              <input type="text" id="email" name="fields[email]" value="{events/send-cv/post-values/email}" class="textbox" />
              <xsl:if test="events/send-cv/name/@type = 'missing' or events/contact-us/name/@type = 'invalid'">
                <span class="error-message">This field is required. Must be a valid email address.</span>
              </xsl:if>
            </div>
            <div class="form-element clearfix">
              <xsl:if test="events/send-cv/phone/@type = 'missing' or events/send-cv/phone/@type = 'invalid'">
                <xsl:attribute name="class">form-element clearfix error</xsl:attribute>
              </xsl:if>
              <label for="phone">Phone: <span class="required">* Required</span></label>
              <input type="text" id="phone" name="fields[phone]" value="{events/send-cv/post-values/phone}" class="textbox" />
              <xsl:if test="events/send-cv/name/@type = 'missing' or events/contact-us/name/@type = 'invalid'">
                <span class="error-message">This field is required. Must be an integer.</span>
              </xsl:if>
            </div>
            <div class="form-element clearfix">
              <xsl:if test="events/send-cv/file/@type = 'missing' or events/send-cv/file/@type = 'invalid'">
                <xsl:attribute name="class">form-element clearfix error</xsl:attribute>
              </xsl:if>
              <label for="file">File: <span class="required">* Required</span></label>
              <div class="cabinet"><input type="file" id="file" name="fields[file]" class="file" /></div>
              <xsl:if test="events/send-cv/name/@type = 'missing' or events/contact-us/name/@type = 'invalid'">
                <span class="error-message">This field is required. File type must be doc|docx|pdf. </span>
              </xsl:if>
            </div>
            <div class="form-element clearfix">
              <xsl:if test="events/send-cv/name/@type = 'missing' or events/send-cv/message/@type = 'invalid'">
                <xsl:attribute name="class">form-element clearfix error</xsl:attribute>
              </xsl:if>
              <label for="message">Message <span class="required">* Required</span></label>
              <textarea id="message" name="fields[message]" rows="15" cols="20"><xsl:value-of select="events/send-cv/post-values/message" /></textarea>
              <xsl:if test="events/send-cv/name/@type = 'missing' or events/contact-us/name/@type = 'invalid'">
                <span class="error-message">This field is required.</span>
              </xsl:if>
            </div>
            <input type="hidden" name="MAX_FILE_SIZE" value="5242880" />
            <input type="hidden" name="send-email[from]" value="fields[email]" />
            <input type="hidden" name="send-email[subject]" value="Send CV Form Submission" />
            <input type="hidden" name="send-email[body]" value="fields[name],fields[email],fields[phone],fields[file],fields[message]"  />
            <input type="hidden" name="send-email[recipient]" value="newmangreene" />
            <input type="hidden" name="canofspam" value="{$canofspam}" />
            <div class="form-send clearfix">
                <label class="instructions">Please read our <a href="{$root}/static/privacy-policy">privacy policy</a> and <a href="{$root}/static/terms-and-conditions">terms &#38; conditions</a> before submitting the form.</label>
                <input id="btn-send-your-cv" class="button" type="submit" name="action[send-cv]" value="" />
            </div>
          </fieldset>
        </form>
    </xsl:when>

    <xsl:otherwise>
        <xsl:if test="events/send-cv/@result = 'success'">
            <p class="success-overview">Your C.V. has been saved successfully. We will contact you shortly.<br /><em>Newman Greene</em></p>
        </xsl:if>
    </xsl:otherwise>

</xsl:choose>

</xsl:template>

</xsl:stylesheet>

The Form: The Form

Actually you can use the form id as your anchor. I think it’s ok with your XSL.

You can’t get much more succinct than what you have. You could possibly use a single template to create each form-element div, to which you would need to pass the name, type, error message etc; but I don’t think it would save much code.

This is why I wrote Form Controls. Writing form UI logic is boring!

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details