XSLT Renderer

From Sense/Net Wiki
Jump to: navigation, search
  •  
  •  
  •  
  •  
  • 100%
  • 6.0
  • Enterprise
  • Community
  • Planned

Overview

Content as XML
An XSLT Renderer is an .xslt file in the Content Repository that can be used to render Content and Content Collections in browse mode. It's a convenient alternative to Content Views and Collection Views for those builders who are familiar with XSLT.

Details

The most common portlets used to render Content (especially in browse mode) also support XSLT rendering. This means that instead of presenting a Content with a Content View - which is an ASP.Net control - you can use an XSLT file. When rendered with XSLT the portlet creates the XML markup for the Content or the Content Collection and passes it over to the XSLT renderer. The generated XML contains the Content's basic info, the Fields and their values, and also the list of actions with generated links for the Content. This technology does not allow editing or saving of the Content and may primarily be used when presenting Content or Collections in browse mode. It is possible however to create an XSLT that generates special HTML controls or Action links that communicate with the CMS via MVC services. Renderers are placed in the Content Repository under the folder /Root/Global/renderers.

Portlets supporting XSLT rendering

Sample generated XML

Below you can see a shortened example of the generated XML for a Content. Children Content markup is also included.

<Content>
  <ContentType>Folder</ContentType>
  <ContentTypePath>/Root/System/Schema/ContentTypes/GenericContent/Folder</ContentTypePath>
  <ContentName>infos</ContentName>
  <Icon>Folder</Icon>
  ...
  <Fields>
    <DisplayName>infos</DisplayName>
    <Id>3345</Id>
    <ParentId>3008</ParentId>
    <Path>/Root/Sites/Default_Site/infos</Path>
    ...
  </Fields>
  <Actions>
    <Search>/infos?action=Search&back=%252f(apps)%252fThis%252fBrowse%253fcontext%253d%252fRoot%252fSites%252fDefault_Site%2526back%253d%25252Fdefault.aspx</Search>
    <Explore>/infos?action=Explore&back=%252f(apps)%252fThis%252fBrowse%253fcontext%253d%252fRoot%252fSites%252fDefault_Site%2526back%253d%25252Fdefault.aspx</Explore>
    <Add>/infos?action=Add&back=%252f(apps)%252fThis%252fBrowse%253fcontext%253d%252fRoot%252fSites%252fDefault_Site%2526back%253d%25252Fdefault.aspx</Add>
    ...
  </Actions>
  <Children>
    <Content>
      <ContentType>WebContentDemo</ContentType>
      <ContentTypePath>/Root/System/Schema/ContentTypes/GenericContent/ListItem/WebContent/WebContentDemo</ContentTypePath>
      ...
      <Fields>
         ...
      </Fields>
    </Content>
    ...
  </Children>

XSLT Tools

Besides presenting any data from the generated XML builders may use a handful of server-side tools that can be useful when creating XSLT renderers. There are a couple of helper functions defined in the following namespaces:

xmlns:snf="sn://SenseNet.Portal.UI.XmlFormatTools"
  • string FormatDate(string xmlDate) - returns the given date converted to the dateformat of the current culture
  • string FormatDate(string xmlDate, string format) - returns the given date formatted according to the given format string
  • string ShortDate(string xmlDate) - returns the given date formatted to the default short date format
  • string ShortTime(string xmlDate) - returns the given date formatted to the default short time format
  • string LongDate(string xmlDate) - returns the given date formatted to the default long date format
  • string LongTime(string xmlDate) - returns the given date formatted to the default long time format
  • decimal Abs(decimal number) - returns the absolute value of the given number
xmlns:snsec="sn://SenseNet.Portal.Helpers.Security"
  • bool IsInRole(string role) - returns true if the current user is a member of the group located at /Root/IMS/BuiltIn/Portal/role, where role is a provided parameter
  • bool IsUserInRole(string rolePath, string userPath) - returns true if the user provided with userPath is a member of the group located at rolePath
xmlns:snact="sn://SenseNet.Portal.Helpers.Actions"
  • string BrowseAction(string contentPath) - returns the HTML markup of the Browse action link for the Content at the provided path
  • string ActionMenu(string contentPath, string innerContent, string scenario) - returns the HTML markup (script tag included) of an ActionMenu of the provided scenario for the Content at the provided path. You can specify an HTML markup with the innerContent parameter that will be enclosed by the menu.
xmlns:snct="sn://SenseNet.Portal.UI.ContentTools"
  • XPathNodeIterator GetContent(string path) - returns the XML for the Content under path
  • XPathNodeIterator GetContent(string path, bool withChildren) - returns the XML for the Content under path, with its children included in the markup (see #Example/Tutorials for example usage)
  • XPathNodeIterator GetReferences(string path, string referenceName) - returns references of the content under path, provide the reference property name with referenceName parameter (see #Example/Tutorials for example usage)
  • XPathNodeIterator GetContextNode() - returns the XML for the current context node
  • string CreateThumbnailPath(string path, string imageModeStr, int width, int height) - returns the generated thumbnail path for an image field. For example: <xsl:value-of select="snc:CreateThumbnailPath(/Content/Fields/Image, /Content/Fields/Image/@imageMode, 10, 20)" />
  • string CurrentUrlEncoded() - returns the current url encoded (ie: "/Root/Default_Site/targetfolder?action=Edit&back=%2ftargetfolder")
  • string UserIsLoggedIn() - checks whether the current user is logged in or not and returns 'true' or 'false' respectively
  • string GetUserNameByPath(string path) - gets the full name of the user under the given path
  • string GetParentFromPath(string path) - gets the name of the parent of the provided path
  • string GetParentPath(string path) - gets the site-relative parent path of the provided path
  • 'string GetActionUrl(string url, string actionName, string backUrl) - returns the action url for the specified actionName and backUrl. url is the path of the Content, and can be site-relative.
  • string GetCurrentUserPath() - returns the path of the current user
  • string Guid() - returns a new Guid
xmlns:sn="http://www.sensenet.com/2010"

Example/Tutorials

Displaying generated XML using Xml.xslt action

To check the generated XML for a specific content, simply call the Xml.xslt action on it by typing ?action=xml.xslt in the browser when viewing the content (for example: http://localhost/infos?action=xml.xslt This link works only if you have a live demo install on your localhost! (Don't forget! Some feature needs authentication and/or admin rights!)). The Xml.xslt Application is a simple XsltApplication located at /Root/(apps)/GenericContent/Xml.xslt. The requested Application will render the corresponding XML right into the response. You can view the XML by viewing the page source in the browser.

You will need to set See, Open and Run application permissions on the Application for the group Everyone in order to be able to use the Xml.xslt application

Displaying generated XML using XmlDumper

XmlDumper.xslt is an XSLT renderer at /Root/Global/renderers/XmlDumper.xslt that renders the Content XML into an HTML textarea:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:template match="/">
    <textarea>
      <xsl:copy-of select="*"/>
    </textarea>
  </xsl:template>
</xsl:stylesheet>

You can use it as an XSLT renderer to check the generated XML output of the portlet. For example add a Content collection Portlet to a page, set the context to CustomRoot, set the CustomRoot to /Root/Sites/Default_Site/infos and give /Root/Global/renderers/XmlDumper.xslt to the Renderer property. The portlet will generate the XML for the infos folder including its children into a textarea within the portlet's displayed area.

Listing children collection

This following example shows how to create an XSLT Renderer that lists the current Content's children with links to their browse views. The renderer can be used with the Content collection Portlet.

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    <xsl:output method="html" indent="yes"/>
    <xsl:template match="/">
      <div class="sn-contentlist">
        <xsl:for-each select="/Content/Children/Content">
          <div class="sn-content sn-contentlist-item">
            <h2 class="sn-content-title">
              <a href="{Actions/Browse}">
                <xsl:value-of select="Fields/DisplayName" />
              </a>
            </h2>
            <div class="sn-content-header"></div>
            <div class="sn-more">
              <a class="sn-link sn-content-link" href="{Actions/Browse}">more>></a>
            </div>
          </div>        
        </xsl:for-each>
      </div>
    </xsl:template>
</xsl:stylesheet>

Using XSLT Tools

The following source shows an example of how to use the XSLT Tools from XSLT. This XSLT renders children of a Content with remove links. The back url of the links is generated using an XSLT Tools function. The creation date of the Content is also displayed using another function.

<?xml version="1.0" encoding="utf-8"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="xsl"
   xmlns:sn="sn://SenseNet.Portal.UI.ContentTools"
   xmlns:snf="sn://SenseNet.Portal.UI.XmlFormatTools">
   <xsl:output method="html" indent="yes" omit-xml-declaration="yes" />
 
   <xsl:template match="/">
      Creation date:  <xsl:value-of select="snf:FormatDate(/Content/Fields/CreationDate)" />
      <div>
         <table>
            <tr>
               <td width="150px"><b>Content</b></td>
               <td />
            </tr>
            <xsl:for-each select="/Content/Children/Content">
               <tr>
                  <td width="150px"><xsl:value-of select="Fields/Title" /></td>
                  <td><a href="{Actions/Delete}&amp;back={sn:CurrentUrlEncoded()}">remove</a></td>
               </tr>
            </xsl:for-each>
         </table>
      </div>
   </xsl:template>
</xsl:transform>

This next example shows how to use XSLT Tools functions that return with an XPathNodeIterator object:

<?xml version="1.0" encoding="utf-8"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="xsl"
   xmlns:sn="sn://SenseNet.Portal.UI.ContentTools">  
     <xsl:output method="html" indent="yes" omit-xml-declaration="yes" />
 
   <xsl:template match="/">
      'YourContents' was created by: <xsl:value-of select="sn:GetContent('/Root/YourContents')/Fields/CreatedBy" />
   </xsl:template>
</xsl:transform>

You can also iterate through Content Collections. This next example when applied to a Group will list the members of the group with CreatedBy info, and also lists the children collection of an arbitrary folder (in this case /Root/YourContents):

<?xml version="1.0" encoding="utf-8"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="xsl"
   xmlns:sn="sn://SenseNet.Portal.UI.ContentTools">  
     <xsl:output method="html" indent="yes" omit-xml-declaration="yes" />
 
   <xsl:template match="/">
     <xsl:variable name="children" select="sn:GetReferences(/Content/Fields/Path, 'Members')"/> 
     <xsl:for-each select="$children/Members/Content">
       name: <xsl:value-of select="ContentName" />
       created by: <xsl:value-of select="Fields/CreatedBy" />
       <br />
     </xsl:for-each>
     <hr />
     <xsl:variable name="children2" select="sn:GetContent('/Root/YourContents', true())"/> 
     <xsl:for-each select="$children2/Children/Content">
       name: <xsl:value-of select="ContentName" />
       created by: <xsl:value-of select="Fields/CreatedBy" />
       <br />
     </xsl:for-each>
   </xsl:template>
 
</xsl:transform>

Related links

References