6

I have a custom XSL file I linked in one my view. I would like to display the created field value as a date only.

By now I have this xsl template :

<xsl:template name="FieldRef_ValueOf.Created"
            ddwrt:dvt_mode="body"
            ddwrt:ghost="">
    <xsl:param name="thisNode"
           select="."/>
    <xsl:value-of select="ddwrt:FormatDate($thisNode/@*[name()=current()/@Name],1036,1)"/>
</xsl:template>

Unfortunately, the format date method does not seems to be applied. What is missing in my template?

Please note I did not forget to add this xml namespace: xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime"

[Edit]: I've changed a bit my template like this :

  <xsl:template name="FieldRef_ValueOf.Created"
                ddwrt:dvt_mode="body"
                ddwrt:ghost="">
    <xsl:param name="thisNode"
               select="."/>
    <span>here</span>
    <xsl:value-of select="ddwrt:FormatDate(string($thisNode/@*[name()=current()/@Name]),1036,1)"/>
  </xsl:template>

Especially, I added a "span" just to check if it's taken into account. And it seems the "here" word does not appears. Actually, I don't think the problem is due to the date formatting, but the template itself.

Is there any restriction related to this oob column?

2
  • With this code what is the output? i mean the date format
    – Diptarag
    Dec 19, 2012 at 11:09
  • @Diptarag: the output remains unchanged (no visible effect)
    – Steve B
    Dec 19, 2012 at 13:23

3 Answers 3

3

I believe it is related with invoking template for a field, it means that in your case this template is not invoked at all.

In order to render field using your template the following attributes should be specified in template:

a) mode DateTime_body

b) match(matches field by internal name in that case)

    <xsl:template name="FieldRef_DateTime_body.Created" ddwrt:dvt_mode="body" match="FieldRef[@Name='Created']" mode="DateTime_body" ddwrt:ghost="hide">
        <xsl:param name="thisNode" select="."/>
        <xsl:choose>
            <xsl:when test="$FreeForm">
                <xsl:call-template name="FieldRef_ValueOf.Created">
                    <xsl:with-param name="thisNode" select="$thisNode"/>
                        </xsl:call-template>
                   </xsl:when>
                 <xsl:otherwise>
                 <nobr>
                 <xsl:call-template name="FieldRef_ValueOf.Created">
                    <xsl:with-param name="thisNode" select="$thisNode"/>
                </xsl:call-template>
        </nobr>
      </xsl:otherwise>
      </xsl:choose>
  </xsl:template>

  <xsl:template name="FieldRef_ValueOf.Created"
                ddwrt:dvt_mode="body"
                ddwrt:ghost="">
    <xsl:param name="thisNode"
               select="."/>
    <span>here</span>
    <xsl:value-of select="ddwrt:FormatDate(string($thisNode/@*[name()=current()/@Name]),1036,1)"/>
  </xsl:template>

Note: Your template is invoked as child template.


Actually there is a more simpler way how to achieve this

<xsl:template name="FieldRef_ValueOf.Created"
                match ="FieldRef[@Name='Created']" 
                mode="DateTime_body">
    <xsl:param name="thisNode"
               select="."/>
    <xsl:value-of select="ddwrt:FormatDate(string($thisNode/@Created),1036,1)" />
  </xsl:template> 

Hope this helps,

Vadim

3
  • 1
    Your second suggestion is working exactly as expected. Thank you!
    – Steve B
    Jan 3, 2013 at 9:26
  • I had asked this question on Microsoft's Forums and the key for me was the mode attribute which doesn't seem to be documented anywhere. social.msdn.microsoft.com/Forums/en-SG/… . When the XSLT is made in designer and embedded directly in the web part's XML you don't need the mode however, in an external stylesheet you do. Jan 7, 2013 at 16:29
  • About the usage of mode attribute, you right. Thanks! Regarding documentation for mode attribute, you could find mapping information (mode <-> field type) in XSL file c:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\XSL\fldtypes.xsl. See template PrintField. Jan 7, 2013 at 20:45
0

Are you using this on ContentQuery XSL ? If so then Create outputs as a column "Created"

so in that case itwould be

<xsl:value-of select="ddwrt:FormatDateTime(string(Created), 1036, 1)" />

If you using a Dataview web part and that is the only way to get the date then you can try casting it to a string (I seem to recall that this needs to be passed as a string for the function to work (might be mistaken though)

In that case try this instead

<xsl:value-of select="ddwrt:FormatDate(string($thisNode/@*[name()=current()/@Name]),1036,1)"/>

Nothing to lose by trying anyway :)

Hope this helps

1
  • I'm customizing a list view in a webpart. Actually, my list schema defines a view where I put ` <XslLink Default="TRUE">some/path/myxsl.xsl</XslLink>`. In this XSL file, I've overwritten the global template to add a background color, and the template you saw in my question. As I state in my edit, I believe this is not the date formatting which is wrong, but the template selection.
    – Steve B
    Dec 19, 2012 at 13:37
0

Add xmlns:ddwrt2="urn:frontpage:internal" to the template node. So your template should now look like this:

<xsl:template name="FieldRef_ValueOf.Created"
                ddwrt:dvt_mode="body"
                ddwrt:ghost=""
                xmlns:ddwrt2="urn:frontpage:internal">
    <xsl:param name="thisNode"
               select="."/>
    <xsl:value-of select="ddwrt:FormatDate(string($thisNode/@*[name()=current()/@Name]),1036,1)"/>
</xsl:template>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.