Using XSLT an Example

Using XSLT – An Example

I really like how I can use an XML document to separate the storage of application data from its presentation. The XML document contains a self describing set of data structures that I can define using my own vocabulary definitions. One of the major advantages of these documents is how easy they can be moved across the various process boundaries that exist within enterprises. This isolation of data into an XML allows a host processing the document to make the presentation decisions. An XML document by its very definition contains no formatting or presentation requirements. This is where the Extensible Stylesheet Language Transformation (XSLT) plays a major role. In this article I will show an example of how you can use XSLT to transform an XML InfoPath document in a Word 2003 document.

XSLT is a meta-language that consists of an XML based vocabulary that describes elements for transforming XML based content. This vocabulary consists of a specialized set of elements or formatting objects that define presentation and document based positional elements. Also, Built into this positional location service is a search specification called the XML Path Language (XPATH). The combination of these two forms a specialized vocabulary that enables the transformation of any XML based document into virtually any other document format. XSLT is designed as a transformation language. Starting with an XML based document and then through the application of templates generates a new output document. The XSLT processor accepts as input the XML tree represented in a well formed document and then produces as output a new transformed document.

The transformation process defines the use of three documents – the source, the XSLT stylesheet and the resulting document. The source document is a simply a well formed XML document. This document serves as the input of the transformation. The system sheet document is a XML document that uses the XSLT vocabulary for expressing transformation rules. Finally, the result document is a text document that is produced by applying the transformation defined in the XSLT stylesheet to the input document.

A transformation expressed in XSLT describes rules for transforming a source tree into a result tree. The transformation is achieved by associating a set of patters with templates. A patter is matched against elements in a source tree. A template is instantiated to create part of the result tree. It is important to remember that the result tree is a separate from the source tree. In constructing the result tree, elements from the source tree can be filtered and reordered, and any type of arbitrary structure can be added. Any transformation expressed in XSLT is called a stylesheet. This is because in the case of when XSLT is transforming into the XSL formatting vocabulary, the transformation functions as a stylesheet or template.

The InfoPath XML Document

The result of opening, completing and saving an InfoPath form is an XML document. For example, if we were to create an InfoPath form that contains the following data source.

Once the data source is completed and the form is designed it could look like the following.

All InfoPath solutions are saved into an XSN file. This solution file is nothing but a compressed cab file that itself uses XML for definition of an InfoPath form. If this file is expanded you can see that it contains a variety of file formats. Each of these is some variation of XML.

The main view of any InfoPath application is saved as an XSLT style sheet and by default is named View1.xsl. This style sheet is applied to data when an InfoPath solution is opened and through a series of rendering results in the view that is shown to a user.

Once a form is completed and saved it generates an XML document that matches the schema definition defined within the InfoPath solution and resembles the following.

<?xml version="1.0" encoding="UTF-8"?>

<?mso-infoPathSolution solutionVersion="1.0.0.1" productVersion="11.0.5531" PIVersion="1.0.0.0" href="file:///C:\Writings\Convert\NameList.xsn" ?>

<?mso-application progid="InfoPath.Document"?>

<my:myFields xmlns:my="https://schemas.microsoft.com/office/infopath/2003/myXSD/2004-03-28T01:27:16" xml:lang="en-us">

<my:NameList>

<my:FirstName>Joe</my:FirstName>

<my:LastName>Brown</my:LastName>

</my:NameList><my:NameList>

<my:FirstName>Thom</my:FirstName>

<my:LastName>Robbins</my:LastName>

</my:NameList>

</my:myFields>

Visual Studio and XSLT

Within the .NET Framework the System.Xml.Xsl namespace provides support for XSLT transformations. It supports the W3C XSL Transformations (XSLT) Version 1.0 Recommendation (www.w3.org/TR/xslt). This namespace provides several methods that enable developers to transform the document created by InfoPath to any other format. Within an application function a transformation can be done using the following code.

xslt.Load(stylefile)

xslt.Transform(filename, "out.xml", Nothing)

For example, using the following code we can apply an XSLT transformation to the InfoPath XML document and perform the transformation to a Word document.

Private Sub ConvertToWord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConvertToWord.Click

Dim filename As String

With OpenInfoPath

.Filter = "InfoPath files(*.xml)|*.xml"

.ShowDialog()

filename = .FileName

End With

If filename <> "" Then

Dim stylefile As String

Dim xslt As New System.Xml.Xsl.XslTransform

With OpenInfoPath

.Filter = "XSLT files(*.xslt)|*.xslt"

.ShowDialog()

stylefile = .FileName

End With

If stylefile <> "" Then

xslt.Load(stylefile)

xslt.Transform(filename, "out.xml", Nothing)

Else

MsgBox("Please select a style sheet file!")

End If

Else

MsgBox("Please select an InfoPath file")

End If

End Sub

One of the formats of Word 2003 conforms to XML with namespace support added to support WordML. The addition of this namespace within an XML document preserves Word's styles and formatting in an XML namespace. This doesn’t define presentation but an important part of any Word document is formatting. This namespace allows the inclusion of these formats. For example, the following XSLT transform can be used to transform the InfoPath XML document into a Word based document.

<xsl:stylesheet version="1.0" xmlns:my="https://schemas.microsoft.com/office/infopath/2003/myXSD/2004-03-28T01:27:16" xmlns:xsl="https://www.w3.org/1999/XSL/Transform" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="https://schemas.microsoft.com/office/word/2003/2/wordml">

<xsl:template match="my:myFields">

<xsl:processing-instruction name="mso-application">progid="Word.Document"</xsl:processing-instruction>

<w:wordDocument xmlns:w="https://schemas.microsoft.com/office/word/2003/2/wordml">

<o:DocumentProperties>

<o:Title>My Company Name List</o:Title>

</o:DocumentProperties>

<w:body>

<w:t>First Loop</w:t>

<xsl:for-each select="my:NameList">

<w:p>

<w:r>

<w:t>Inner Loop</w:t>

<w:t><xsl:value-of select="my:FirstName"/></w:t>

<w:t><xsl:value-of select="my:LastName"/></w:t>

</w:r>

</w:p>

</xsl:for-each>

</w:body>

</w:wordDocument>

 

</xsl:template>

</xsl:stylesheet>

Once transformed this XML document then becomes available as a Word document as can be seen when you look at it within File Explorer.

The processing instructions (PI) located within the header of the XML document identify the type of editor to the system. When opened in Word this document appears as a normal document that can be further transformed or even changed to match the format of the word document as shown below.

Of course this is a simple example of some of the things that can be done with XSLT. For this interested, I have published the sample code that was used her for download.