Help / Examples / XSLT

Summary

This article talks about Agile Documents XSLT functionality and builds on what we learned in Word Basics - Start Here, Word Basics - Templates and Word Basics - Find and Insert. XSLT is a way to transform XML data into other formats.

Agile Documents provides multiple ways to control the selection of content, its assembly and the generation of new documents. As we previously discussed, you may create a template for Agile Document that defines its structure and style; you may drag content from SharePoint Explorer and drop it anywhere within your Agile Document; you may filter the selection of content such as selecting a range of cells in an Excel file; and then you may also save your document to several different output types. However, sometimes you may desire additional functionality and customization. It is because of these cases that we have integrated XSLT into our processes.

Many of the files Agile Document's interacts with--such as Microsoft Word, Excel, PowerPoint, and InDesign--are actually a ZIP file that houses many XML files. So, you can change the extension of a .DOCX file to .ZIP and browse it with WinZip, 7-Zip, or WinRAR or your favorite ZIP editor to see the underlying data. When you leverage the XSLT processes within Agile Documents you can read and update files within this ZIP archive. Really, the sky is the limit in terms of what you can do with XSLT and your files. You could reformat Word tables, you could copy data from several different worksheets in an Excel table and insert it as a completely reformatted Word table and you could also integrate Word or Excel data into an Adboe InDesign document.

You may wish to review the technical discussion of XSLT processing within Agile Documents.

Please note that the examples described here are easily installed onto your SharePoint site. That installation procedure is described in Installing Examples.


Example 1: Modifying a Word Document via XSLT

In this example, we eliminate empty table cells. With Agile Document's XSLT functionality this is a simple task.

Similar to previous examples, ensure that the folder XSLT/EX1 exists within your document library. Then create a new Agile Document through SharePoint Explorer. Go back into SharePoint and rename it EX1.AgileDocument.xml. Then, create the Word document detailed in Figure 1 and create the XSLT file shown in Figure 2.

Next, edit the Agile Document as shown in Figure 3 to pair the XSLT file with the Word file. Make sure you add the Word file first, then go to the code pane and then drop the XSLT file from SharePoint Explorer to the Drag files from SharePoint Explorer and drop them here rectangular box. Also, ensure you set the Agile Document output type to DOCX. Save, build and then review Figure 4.





In this example we applyied an XSLT file (Figure 2) to a Microsoft Word (DOCX) file to modify its content. Specificially, empty table cells were removed from the last table on Page 1 of the document.

The exact application of the XSLT is detailed in Line 3 of Figure 2. That processing instruction dictates that Agile Documents is to interact with the DOCX (aka ZIP) file by appling the XSLT to the word/document.xml (zipsource) and store its result to word/document.xml (zipdestination). The bc-agiledocuments Processing Instruction is further detailed in the Configuring Agile Document's processing of XSLT files section of XSLT documentation.

As you can see from Figure 2, the XSLT is rather short and straight forward. The important take away is that you can build-up a library XSLTs that could be applied for all sorts of circumstances. For instance, you could apply a "branding" XSLT that ensures, among other things, that all tables are shaded blue.


Example 2: Styling Word Tables

In Example 2, we apply a blue table style to all tables in the document. Through Agile Document's XSLT functionality, we are able to quickly and consistently automate what used to be a manual process. Thus, we avoided the time and error prone nature of manual processes.

Similar to previous examples, ensure that the folder XSLT/EX2 exists within your document library. Then create a new Agile Document through SharePoint Explorer. Go back into SharePoint and rename it EX2.AgileDocument.xml. Copy, the Word document you created in Example 1 (Figure 1) to our XSLT/EX2 folder. Create an empty Word document Template1.docx. Also, create the XSLT file shown in Figure 5.

Next, edit the Agile Document as shown in Figure 6 to pair the XSLT file with the Word file. Make sure you add the Word file first, then go to the code pane and then drop the XSLT file from SharePoint Explorer to the Drag files from SharePoint Explorer and drop them here rectangular box. Also, ensure you set the Agile Document output type to DOCX. Save, build and then review Figure 7.





This example applied an even smaller XSLT file (Figure 5) to the same Microsoft Word (DOCX) in Example 1 to have an even more substantial impact. As you verified in Figure 7, we applied a blue table format to all the tables.

In Figure 6, you've probably also noticed that when you select the XSLT in the Agile Document Editor, its paramTableStyle parameter is editable. When XSLT files have paramaters, those XSL params will be listed below the processing instruction overrides.

The exact application of the XSLT is detailed in Lines 3 - 6 of Figure 5. This processing instruction tells Agile Document's is to interact with the Figure 1 DOCX file by appling the XSLT to the word/document.xml (zipsource) and store its result to word/document.xml (zipdestination) within the Template1.DOCX (ziptemplate). Then, use the modified Template1.DOCX file into the Agile Document we are building. The bc-agiledocuments Processing Instruction is further detailed in the Configuring Agile Document's processing of XSLT files section of XSLT documentation.

In Example 1, we didn't use ziptemplate. However, this example needed it as the GridTable4-Accent5 table style wasn't defined in our original "unstyled" Word file. Please note that styles, including table styles, are defined in the word/styles.xml file within the DOCX (ZIP) file.

It should also be noted, that although we only applied one XSLT here, we could have applied multiple. For instance, we could have combined the XSLTs of Example 1 and 2 here in one step.


Example 3: Converting Excel Data to Word

In this example, we extract data from an Excel file (Figure 8) and turn it into a Word table via an XSLT. As you probably realize, this same activity could have been accomplished by using methods described in Example 2 of Word Basics - Start Here. The aforementioned Example 2 is better suited for simpler tasks. The application of an XSLT gives you tremendously more flexibility as it is a programmatic solution but requires time to author.

This example uses the XSLT method to parameterization the extraction of data from Excel. Thus, the user specifies an Excel start and end row to convert said data into a Word table. The advantage of this over the aforementioned Word Basics - Start Here - Example 2 is that it can be more readily applied to Excel files by essentially only specifiying two parameters (row start and row end). Admittedly, it does take time to author an XSLT. So, an XSLT will generally be advantageous if you have a number of repeatable tasks. Conversely, the Word Basics - Start Here - Example 2 method would be advantageous if you were not going to repeat that kind of mapping again.

Similar to previous examples, ensure that the folder XSLT/EX3 exists within your document library. Then create a new Agile Document through SharePoint Explorer. Go back into SharePoint and rename it EX3.AgileDocument.xml. Create and save, the Excel document ExcelToWord.XLSX shown in Figure 8 to our XSLT/EX3 folder. Create an empty Word document Template1.docx and an empty Word document Excel_CreateWordTable.DOCX. Also, create the XSLT file shown in Figure 9.

Next, edit the Agile Document as shown in Figure 10 to pair the XSLT file with the Excel file. Ensure that you have selected Exclude Insertion (Figure 11) for the Excel file and have added the blank Excel_CreateWordTable.DOCX below the Excel file. Also, ensure you set the Agile Document output type to DOCX. Save, build and then review the output (Figure 12).





As you review the output, you'll notice that it extracted data from rows 2 through 8 from the Excel document. You will also notice that it selected a subset of the column data in those rows. This extraction and manipulation into a Word table was completely done thorugh XSLT.

As detailed in processing instruction, lines 3 through 7, Agile Documents is to do the following. (1) Apply the XSLT to the Excel file's xl/worksheets/Sheet1.xml file (zipsource). (2) Output the results inside a copy of the Template1.DOCX file stored on SharePoint (ziptemplate). (3) Specifically, store the results to the word/document.xml (zipdestination) inside a copy of the Template1.DOCX file. (4) Upload the modified Template1.DOCX copy to SharePoint as Excel_CreateTable.DOCX (destination).

In this example, we only extracted Excel data from rows 2 thorugh 8. However, there were several other funds included in that Excel spreadsheet. That is why, we paramertized the start and end rows so that the defaults of 2 and 8 could be customized as needed. For simplicity, we only created one table. However, this XSLT allows for the creation of any number of other tables as needed -- such as rows 9 through 12 -- by customizing the pFundClassRowsStart, pFundClassRowsEnd and destination parameters. As we noted above, you can apply several XSLTs to a single content file. Thus, we could have added another XSLT instance under our existing one in Figure 10 and customized with pFundClassRowsStart set ot 9, pFundClassRowsEnd set to 12 and destination set to Excel_CreateTable2.DOCX. ...and add Excel_CreateTable2.DOCX immediately below Excel_CreateTable.DOCX in our Agile Document.

With that said, you may still be confused as to why we excluded the Excel file from Agile Documents, created an empty Excel_CreateTable.DOCX file and added it to the Agile Document. This was done because the XSLT created a completely new Word file from an Excel file. Because there were a multitude of things you could have done with that Word file, it was ambigious as to what Agile Documents should do with it. So, Agile Documents ignores these output files unless you specifically ask for it to do something with it. In our case, we wanted to append it to the Agile Document we were building. Thus, we told Agile Document's only to do XSLT processing with the Excel file and not include it in the Agile Document. The XSLT processing creates (copies over) the Excel_CreateWordTable.DOCX file. Since Agile Documents processes files in the order they are encountered, we needed to append the Excel_CreateWordTable.DOCX file after the XSLT processing. That is why we appended the Excel_CreateWordTable.DOCX to our Agile Document and why it is immediately below the Excel file.