Create an xslExtension for Lix for Sitecore

Pentia A/S‘ Lix for Sitecore module is a readability checker for Sitecore 5 and Sitecore 6 based on the Lix readability algorithm which is widely used for scandinavian languages (Danish, Swedish, Norwegian, …) and for Dutch. Having a low Lix is one of the criterias in the Bedst på Nettet benchmark evaluation.

The Lix module is a Sitecore shell extension allows you to see the Lix of your text in the Rich Text Editor. It is also possible to apply Lix for Sitecore as a field validator (Sitecore 6 only):

Lix for Sitecore embedded as a Shell Extension

Lix for Sitecore embedded as a Shell Extension

But what if you want to show the Lix number on your website? You will have to write your own Xsl extension for that. It’s not that hard though. I’ll show you an example where I serialize the calculation and uses it as an Xsl extension.

The lix calculation is returned as an ICalculationResult interface. As you might know, interfaces cannot be serialized in .net, so I’ll have to create a concrete class for the serialization. In this example I’ll call the class MyResult:

using System.Xml.XPath;
using PT.Lix.Core;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace MyNamespace
  public class MyResult
    public double AverageSentenceLength { get; set; }
    public int LixNumber { get; set; }
    public int NumberOfLongWords { get; set; }
    public int NumberOfSentences { get; set; }
    public int NumberOfWords { get; set; }
    public double PercentageLongWords { get; set; }
    public string Text { get; set; }

    public MyResult() { }

    public MyResult(ICalculationResult result)
      AverageSentenceLength = result.AverageSentenceLength;
      LixNumber = result.LixNumber;
      NumberOfLongWords = result.NumberOfLongWords;
      NumberOfSentences = result.NumberOfSentences;
      NumberOfWords = result.NumberOfWords;
      PercentageLongWords = result.PercentageLongWords;
      Text = result.Text;

The MyResult class takes the ICalculationResult as parameter to a constructor and I construct a new class using the same fields as in the interface. Also note the parameterless constructor that will alow me to XML serialize the class.

With my new class I can then make a class with a function that returns a XPathNodeIterator with the contents from my XML serialized MyResult class:

public class XsltHelper
  public XPathNodeIterator Calculate(string text)
    Calculator calculator = new Calculator(Sitecore.Context.Database);
    ICalculationResult result = calculator.Calculate(text);
    MyResult myResult = new MyResult(result);

    XmlDocument doc = new XmlDocument();
    MemoryStream stream = new MemoryStream();
      XmlSerializer serializer = new XmlSerializer(typeof(MyResult));
      serializer.Serialize(stream, myResult);
      stream.Seek(0, SeekOrigin.Begin);
    return doc.CreateNavigator().Select(".");

The next step is to apply my class as an XslExtention in web.config:

  <extension mode="on" type="MyNamespace.XsltHelper, MyAssembly" namespace="" singleInstance="true" />

I can then add my extension to any XSLT:

<xsl:stylesheet version="1.0"
  exclude-result-prefixes="dot sc sql lix">

And the extension is ready to be used:

<!-- entry point -->
<xsl:template match="*">
  <sc:dot />
<h1><sc:text field="title"/></h1>
<div><sc:text field="text"/></div>
  <!-- calculate the lix for the field "text" -->
  <xsl:variable name="lixResult" select="lix:Calculate(sc:field('text',.))" />
    <!-- Outout the lix value: -->
    Lix: <xsl:value-of select="$lixResult/MyResult/LixNumber"/>

My page will now display the Lix value of the current text on the current page. For further information and pricing of the Lix for Sitecore module, please contact Pentia A/S.


About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in Sitecore 5, Sitecore 6 and tagged , , , . Bookmark the permalink.

2 Responses to Create an xslExtension for Lix for Sitecore

  1. I never heard of it, but Sitecore 6 validator checking the text readability? Very very cool!

    Having the score, it is possible to setup a range of error levels, from warnings (giving a different visual representation) to critical error. You probably already have that :)

  2. briancaos says:

    Yes, that’s exactly what the validator does. You set up the Lix range fo reach level and the validator warns you if the lix exceedes the limits.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.