Showing posts with label XSL. Show all posts
Showing posts with label XSL. Show all posts

Tuesday, March 17, 2015

Exporting Crystal Report to XML

Saving a Crystal Report in XML format, based upon the default Crystal Report schema, along with an xsl transformation, is a great way to  create web-ready content.

Below is a snippet of xml generated by Crystal Reports. While in Crystal, go to Format Field to set a custom Name attribute as shown below:

<?xml version="1.0" encoding="UTF-8" ?>
<CrystalReport xmlns="urn:crystal-reports:schemas:report-detail"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:crystal-reports:schemas:report-detail http://www.businessobjects.com/products/xml/CR2008Schema.xsd">
<Group Level="1">
<GroupHeader>
<Section SectionNumber="0">
<Field Name="TERM" FieldName="GroupName ({Command.CLASS_TERM_CD})"><FormattedValue>Spring 2015</FormattedValue><Value>Spring 2015</Value></Field>
<Field Name="TERMCODE" FieldName="{Command.CLASS_TERM_CD}"><FormattedValue>4152</FormattedValue><Value>4152</Value></Field>
</Section>
</GroupHeader>
<Details Level="2">
<Section SectionNumber="0">
<Field Name="CLASSNAME" FieldName="{Command.CLASS_CLASS_NAME}"><FormattedValue>ENG102</FormattedValue><Value>ENG102</Value></Field>
<Field Name="CLASSNBR" FieldName="{Command.CLASS_CLASS_NBR}"><FormattedValue>38090</FormattedValue><Value>38090</Value></Field>
<Field Name="TITLE" FieldName="{Command.CRSE_COURSE_TITLE_LONG}"><FormattedValue>First-Year Composition</FormattedValue><Value>First-Year Composition</Value></Field>
<Field Name="STARTTIME" FieldName="{Command.CLASS_START_TIME1}"><FormattedValue> 4:00PM</FormattedValue><Value> 4:00PM</Value></Field>
<Field Name="ENDTIME" FieldName="{Command.CLASS_END_TIME1}"><FormattedValue> 6:40PM</FormattedValue><Value> 6:40PM</Value></Field>
<Field Name="STARTDATE" FieldName="{Command.CLASS_START_DATE}"><FormattedValue>3/24</FormattedValue><Value>2015-03-24T00:00:00</Value></Field>
<Field Name="ENDDATE" FieldName="{Command.CLASS_END_DATE}"><FormattedValue>5/15</FormattedValue><Value>2015-05-15T00:00:00</Value></Field>
<Field Name="DAYS" FieldName="{@FormattedDays}"><FormattedValue>Tu,Tr</FormattedValue><Value>Tu,Tr</Value></Field>
</Section>
</Details>

...


On the XSL side, make sure you setup your Crystal Reports namespace. In the example below I'm using a 'cr' prefix.

<?xml version="1.0"?><xsl:stylesheet xmlns:cr="urn:crystal-reports:schemas:report-detail" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:template match="/">

<xsl:for-each select="cr:CrystalReport/cr:Group">

<h2 class="ms-rteElement-H2Custom"><xsl:value-of select="cr:GroupHeader/cr:Section/cr:Field[@Name='TERM']/cr:FormattedValue"/></h2><table width="100%" class="courseTableWP">
<xsl:for-each select="cr:Details/cr:Section">

<tr class="courseTableRow"></tr></xsl:for-each></table>
<th width="10%">Course</th><th width="15%">Class Number</th><th width="35%">Title</th><th width="10%">Days</th><th width="15%">Times</th><th width="15%">Dates</th>
<td><xsl:value-of select="cr:Field[@Name='CLASSNAME']/cr:FormattedValue"/></td>
<td><xsl:value-of select="cr:Field[@Name='CLASSNBR']/cr:FormattedValue"/></td>
<td><xsl:value-of select="cr:Field[@Name='TITLE']/cr:FormattedValue"/></td>
<td><xsl:value-of select="cr:Field[@Name='DAYS']/cr:FormattedValue"/></td>
<td><xsl:value-of select="cr:Field[@Name='STARTTIME']/cr:FormattedValue"/>- <xsl:value-of select="cr:Field[@Name='ENDTIME']/cr:FormattedValue"/></td>
<td><xsl:value-of select="cr:Field[@Name='STARTDATE']/cr:FormattedValue"/> - <xsl:value-of select="cr:Field[@Name='ENDDATE']/cr:FormattedValue"/></td>

</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Thursday, October 24, 2013

SharePoint Custom List web part template

A recent project involved adding the same navigation links to a couple of SharePoint blog sites. The navigation links must appear on the default.aspx, categories.aspx and post.aspx pages. Using a custom list in the parent site, we created a web part using SP Designer. Now, if we need another navigation web part for another set of blogs, we can simply export the web part and make the necessary changes. Below is the content of web part. Included is the xml definition of the view as well as the xsl to create the links.

<?xml version="1.0" encoding="utf-8" ?>
<webParts>
 <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
  <metaData>
   <type name="Microsoft.SharePoint.WebPartPages.XsltListViewWebPart, Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" />
   <importErrorMessage>Cannot import this Web Part.</importErrorMessage>
  </metaData>
  <data>
   <properties>
    <property name="InitialAsyncDataFetch" type="bool">False</property>
    <property name="ChromeType" type="chrometype">None</property>
    <property name="Title" type="string" />
    <property name="Height" type="string" />
    <property name="CacheXslStorage" type="bool">True</property>
    <property name="XslLink" type="string" null="true" />
    <property name="AllowZoneChange" type="bool">True</property>
    <property name="AllowEdit" type="bool">True</property>
    <property name="XmlDefinitionLink" type="string" />
    <property name="DataFields" type="string" />
    <property name="Hidden" type="bool">False</property>
    <property name="ListName" type="string">{37683D39-B11D-4E11-8B1C-8C5421EDB458}</property>
    <property name="NoDefaultStyle" type="string">TRUE</property>
    <property name="ListDisplayName" type="string" null="true" />
    <property name="AutoRefresh" type="bool">False</property>
    <property name="ViewFlag" type="string">8388621</property>
    <property name="AutoRefreshInterval" type="int">60</property>
    <property name="AllowConnect" type="bool">True</property>
    <property name="Description" type="string" />
    <property name="AllowClose" type="bool">True</property>
    <property name="ShowWithSampleData" type="bool">False</property>
    <property name="ParameterBindings" type="string"></property>
     <property name="EnableOriginalValue" type="bool">False</property>
    <property name="CacheXslTimeOut" type="int">86400</property>
    <property name="WebId" type="System.Guid, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">ef069fb2-2b0b-4c00-ad1f-231d5485f963</property>
    <property name="ListUrl" type="string" null="true" />
    <property name="DataSourceID" type="string" />
    <property name="FireInitialRow" type="bool">True</property>
    <property name="ManualRefresh" type="bool">False</property>
    <property name="ViewFlags" type="Microsoft.SharePoint.SPViewFlags, Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">Html, TabularView, Hidden, Mobile</property>
    <property name="ChromeState" type="chromestate">Normal</property>
    <property name="AllowHide" type="bool">True</property>
    <property name="PageSize" type="int">-1</property>
    <property name="SampleData" type="string" null="true" />
    <property name="BaseXsltHashKey" type="string">/_layouts/xsl/vwstyles.xsl;#12/13/2011 02:25:36;#0;#/_layouts/xsl/fldtypes.xsl;#03/26/2010 21:24:40;#0;#/_layouts/xsl/fldtypes_Ratings.xsl;#03/14/2012 11:19:12;#0;#/_layouts/xsl/fldtypes_docicon.xsl;#09/07/2011 11:41:41;#0;# 1 100  14.0.6120.5000</property>
    <property name="AsyncRefresh" type="bool">False</property>
    <property name="HelpMode" type="helpmode">Modeless</property>
    <property name="ListId" type="System.Guid, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">37683d39-b11d-4e11-8b1c-8c5421edb458</property>
    <property name="DataSourceMode" type="Microsoft.SharePoint.WebControls.SPDataSourceMode, Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">List</property>
    <property name="AllowMinimize" type="bool">True</property>
    <property name="TitleUrl" type="string">/XXXXX/XXX/Lists/Navigation</property>
    <property name="CatalogIconImageUrl" type="string" />
    <property name="DataSourcesString" type="string" />
    <property name="PageType" type="Microsoft.SharePoint.PAGETYPE, Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">PAGE_NORMALVIEW</property>
    <property name="DisplayName" type="string">All Items</property>
    <property name="UseSQLDataSourcePaging" type="bool">True</property>
    <property name="Width" type="string" />
    <property name="ExportMode" type="exportmode">All</property>
    <property name="Direction" type="direction">NotSet</property>
    <property name="ViewContentTypeId" type="string">0x</property>
    <property name="HelpUrl" type="string" />
    <property name="XmlDefinition" type="string">
    &lt;View Name="{E9A835C7-4605-41BD-AA5E-50D9799DE2C9}" MobileView="TRUE" Type="HTML" Hidden="TRUE" DisplayName="All Items" Url="/academic-affairs/AFA/SitePages/Untitled_12.aspx" Level="1" BaseViewID="1" ContentTypeID="0x" ImageUrl="/_layouts/images/generic.png"&gt;
     &lt;Query/&gt;
     &lt;ViewFields&gt;
      &lt;FieldRef Name="LinkTitle"/&gt;
      &lt;FieldRef Name="URL"/&gt;
      &lt;FieldRef Name="Title" Explicit="TRUE"/&gt;
     &lt;/ViewFields&gt;
     &lt;/View&gt;
    </property>   
     <property name="Xsl" type="string">
    &lt;xsl:stylesheet xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:ddwrt2="urn:frontpage:internal" xmlns:o="urn:schemas-microsoft-com:office:office" ddwrt:ghost="show_all"&gt;
    &lt;xsl:template match="dsQueryResponse" xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:o="urn:schemas-microsoft-com:office:office" ddwrt:ghost="" xmlns:ddwrt2="urn:frontpage:internal" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime"&gt;       
     &lt;div class="AFANavigation"&gt;&lt;xsl:apply-templates select="/dsQueryResponse/Rows/Row"/&gt;&lt;/div&gt;
    &lt;/xsl:template&gt;
       &lt;xsl:template match="Row"&gt;
      &lt;xsl:choose&gt;
       &lt;xsl:when test="substring(@URL,2,3) = 'a h'"&gt;
        &lt;xsl:variable name="link" select="substring-after(substring-before(@URL,'&amp;lt;/a&amp;gt;'),'&amp;gt;')"/&gt;
        &lt;div&gt;&lt;a href="{$link}"&gt;&lt;xsl:value-of select="@Title"/&gt;&lt;/a&gt;&lt;/div&gt;
       &lt;/xsl:when&gt;
      &lt;xsl:otherwise&gt;
       &lt;div&gt;&lt;a href="{@URL}"&gt;&lt;xsl:value-of select="@Title"/&gt;&lt;/a&gt;&lt;/div&gt;
      &lt;/xsl:otherwise&gt;
     &lt;/xsl:choose&gt;
        &lt;/xsl:template&gt;
    &lt;/xsl:stylesheet&gt;
    </property>

    <property name="Default" type="string">FALSE</property>
    <property name="TitleIconImageUrl" type="string" />
    <property name="MissingAssembly" type="string">Cannot import this Web Part.</property>
    <property name="SelectParameters" type="string" />
   </properties>
  </data>
 </webPart>
</webParts>

Friday, September 20, 2013

Using the following-sibling to compare node-sets in a SharePoint list


In a table I need to display items from a SharePoint custom list grouped by a column. For styling purposes, I need to identify the last row of each grouping. Using the following-sibling axis, I can look ahead to see if the group value will change. If so, I apply a class to the current table row.

<xsl:template match='Row'>
<tr>
<xsl:if test="@Group != following-sibling::Row[1]/@Group or position() = last()">
     <xsl:attribute name="class">lastItem</xsl:attribute>
 </xsl:if>
....
</tr>
...
</xsl:template>

In my stylesheet, I can then apply rules to the lastItem class:

table  tr { border-bottom: 2px #707070 dotted; }
table tr.lastItem { border-bottom: none; }

Tuesday, July 2, 2013

Including a link to a javascript file in XSLT

In SharePoint 2010, I was updating a custom version of the ContentQueryMain.xsl file, needing to include javascript for the page. When I added the following code, the layout of the page changed, the ribbon stopped responding and the page would not respond when in edit mode.

<script type="text/javascript" src="/Script%20Files/myjavascript.js"></script> 

Turns out, the XSLT processor didn't care for the empty script tag. Adding the space entity &#160; fixed the problem.

<script type="text/javascript" src="/Script%20Files/myjavascript.js">&#160;</script>

 

Thursday, February 21, 2013

Content Query Web Part using Custom ItemStyle.xsl


To set an existing Content Query Web part to use a custom ItemStyle.xsl file for custom styling list entries, we need to update the following property in the Content Query Web Part.webpart file after exporting to the Desktop.
<property name="ItemXslLink" type="string">/Style Library/XSL Style Sheets/ItemStyleCustom.xsl</property>
After uploading back to the page, the web part now allows you to choose custom item templates in the ItemStyleCustom.xsl file you've created.

Friday, November 16, 2012

XSL for SharePoint List webparts

For out-of-the-box SharePoint List webparts, the 'XSL Link' property allows editors to customize the display of their list data.

Below is an example of an xsl stylesheet for list webparts.

<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"
  xmlns:ddwrt2="urn:frontpage:internal">
<xsl:output method="html" indent="yes"/>

<xsl:template match="dsQueryResponse" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime">
  <div class="resourcesList" >
      <xsl:apply-templates select="/dsQueryResponse/Rows/Row"/>
  </div> 
</xsl:template>

<xsl:template match="Row">
    <xsl:if test="position()=1">
    <h2><xsl:value-of select="@Category" disable-output-escaping="yes" /></h2>
    </xsl:if>
    <a href="{substring-before(substring-after(@URL,'&gt;'),'&lt;')}">
     <xsl:value-of select="@Title" disable-output-escaping="yes" />
    </a> <br/>
</xsl:template>

</xsl:stylesheet>

Using XSLT gives a much cleaner and customized look to your list webparts.


Thursday, October 18, 2012

Mod operator to control Table Rows in XSL


The Mod operator returns the remainder left after division is performed. If you have a table and you need to have a certain amounts of columns before another row is created, use the mod operator.

The following example will create a new row after 3 columns are created.

<xsl:if test="position() mod 3 = 1">
<xsl:text disable-output-escaping="yes"><![CDATA[ <tr> ]]></xsl:text>
</xsl:if>
   <td valign="top">
    <img alt="" src="{@ImgURL}"/>
    <xsl:call-template name="dvt_2" />
   </td>
<xsl:if test="position() mod 3 = 3">
<xsl:text disable-output-escaping="yes"><![CDATA[ </tr> ]]></xsl:text>
</xsl:if>

Tuesday, March 13, 2012

Comparing Publishing Dates of Blog Posts

When displaying the list of published posts from a SharePoint Blog, you may want to hide certain elements such as the Publish Date div box if there are multiple posts on the same day. The XSL code below will compare the publish date of the post to the previous post and display or not display the div accordingly.

<xsl:variable name="PrevPosition" select="count(./preceding-sibling::*)" />
<xsl:if test="($Rows/Row[$PrevPosition]/@PublishedDate.MonthDayOnly !=
$thisNode/@PublishedDate.MonthDayOnly) or ($PrevPosition+1 = $FirstRow)">
...
</xsl:if>