Added by Arjé Cahn, last edited by Jasha Joachimsthal on May 30, 2007  (view change)

Labels:

needs-refactoring needs-refactoring Delete
templates templates Delete
Enter labels to add to this page:
Wait Image 
Looking for a label? Just start typing.

Inserting today's date in a new document

In the CMS at the location editing/sitemap.xmap you will find a matcher that provides the default content for a newly created document:

<map:match pattern="templates/*.xml">
  <map:generate src="{model://resource[@name='{1}']/template}"/>
  <map:select type="resource-exists">
    <map:when test="{repository:files}/configuration/editing/transformers/insertcontent.xsl">
      <map:transform src="{repository:files}/configuration/editing/transformers/insertcontent.xsl">
        <map:parameter name="mode" value="create"/>
      </map:transform>
    </map:when>
    <map:otherwise>
      <map:transform src="transformers/insertcontent.xsl"/>
    </map:otherwise>
  </map:select>
  <map:serialize type="xml"/>
</map:match>

Most authors find it convenient to have the current date automatically filled when creating a new document. This can be done using an XSL transformation using transformers/insertcontent.xsl. This XSL can be overridden by placing a copy in the repository at {repository:files}/configuration/editing/transformers/insertcontent.xsl. In this XSL you can match any element in the document and do with it whatever you like.

The insertion of the current date can be done using the EXSLT date functions. To use them, you need to declare the following extension and namespace:

xmlns:date="http://exslt.org/dates-and-times"
extension-element-prefixes="date"

First put the current date in some variables:

<xsl:variable name="nYear" select="date:year()"/>
<xsl:variable name="nMonth" select="date:month-in-year()"/> <!-- +1 may be necessary in older XSLT processors -->
<xsl:variable name="nDay" select="date:day-in-month()"/>

Then we do an apply-templates on all elements in the document:

<xsl:template match="/">
  <xsl:apply-templates select="*"/>
</xsl:template>

The date element in the default content can be replaced using this template:

<xsl:template match="date">
  <date day="{$nDay}" month="{$nMonth}" year="{$nYear}"/>
</xsl:template>

All other elements are just copied:

<xsl:template match="@*|node()">
  <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy>
</xsl:template>