XSL Fun

I have been doing quite a bit of XSL lately and I must say that I like it more and more everytime I get into it. One of the latest things that I've learned is that you can actually pass a compiled class in as an argument and call methods from it within the XSL file.

I had an older format of a certain XML file that stored Fonts in some weird way. I actually created my own way of defining the font because I was young and dumb. :) Now that I'm older and obviously much more wiser, I've since changed my format.

Here is what it was:

<MyFont>MS Sans Serif:8:Bold</MyFont>

Notice that there are three parts to it (Font Name, Font Size, and Optional Bold, Italic, and/or Underline.

There was no real easy way to do this using strait XSL language, so I created a nested class that handles this through the use of the String parsing features of the .NET framework. Here is what it looks like:

Private Class XslFontParser

' default font size

Private Const DefaultFontSize As String = "8"

Public Function GetFontName(ByVal fontString As String) As String

Dim fontParts As String() = fontString.Split(":")

Return fontParts(0)

End Function

Public Function GetFontSize(ByVal fontString As String) As String

Dim fontParts As String() = fontString.Split(":")

If fontParts.Length >= 2 Then

Return fontParts(1)

End If

Return DefaultFontSize

End Function

Public Function IsBold(ByVal fontString As String) As Boolean

Return SearchFontPart(fontString, "Bold")

End Function

Public Function IsItalic(ByVal fontString As String) As Boolean

Return SearchFontPart(fontString, "Italic")

End Function

Public Function IsUnderline(ByVal fontString As String) As Boolean

Return SearchFontPart(fontString, "Underline")

End Function

Private Function SearchFontPart(ByVal fontString As String, ByVal fontPart As String) As Boolean

Dim fontParts As String() = fontString.Split(":")

If fontParts.Length <= 2 Then

Return False

End If

For looper As Integer = 2 To fontParts.Length - 1

If String.Compare(fontParts(looper), fontPart, True) = 0 Then

Return True

End If

Next

Return False

End Function

End Class

It's real simple and isn't doing any crazy stuff. Now I simply add a new instance of this class to an XSLTArgumentList like so: 

xslArgs.AddExtensionObject("urn:fontHelper", New XslFontParser)

In the definition of the XSL file, I simply have to define my namespace in the <xsl:transform> and then I can begin calling methods off of this object:

<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fontHelper="urn:fontHelper">

...

<xsl:template match="Font">

<xsl:param name="FontString" />

<Font>

<xsl:attribute name="Name">

<xsl:value-of select="fontHelper:GetFontName($FontString)" />

</xsl:attribute>

<xsl:attribute name="Size">

<xsl:value-of select="fontHelper:GetFontSize($FontString)" />

</xsl:attribute>

<xsl:attribute name="Bold">

<xsl:value-of select="fontHelper.IsBold($FontString)" />

</xsl:attribute>

<xsl:attribute name="Italic">

<xsl:value-of select="fontHelper.IsItalic($FontString)" />

</xsl:attribute>

<xsl:attribute name="Underline">

<xsl:value-of select="fontHelper.IsUnderline($FontString)" />

</xsl:attribute>

</Font>

</xsl:template>

How cool is that? XSL definitely has it's place when dealing wht XML and I've found it. We did a lot of transofmrations on Data Dynamics Product Pages. All of the information on the product pages c I simply have to define omes from a single XML file and simply uses various XSL files to transform the data into HTML.

Print | posted on Wednesday, July 06, 2005 1:12 AM