Include Excel Content

THESE PAGES ARE STILL UNDER CONSTRUCTION AND DO NOT NECESSARELY REFLECT THE CURRENT VERSION OF TÓPICO

In this tutorial, you'll learn how to include an external spreadsheet saved as XML from Excel 2003. To complete this tutorial, you'll need a fair knowledge about XML namespaces and XSLT. You need the Professional version of Tópico to complete this tutorial.

You can include external data from an Excel spreadsheets saved as XML or any other format XML dialect with a transformation template for your particular output.

Create the XML spreadsheet file

Open Excel and create a spreadsheet with a simple table like this one.

Once you have completed editing the spreadsheet, save it as an XML Spreadsheet.

Here's a copy of the above spreadsheet in Excel and XML.

Once it is included in your topic. It will be displayed like any other table in your collection, according to the style defined in your publication stylesheet.

Excel Worksheet Data
Heading 1 Heading 2 Heading 3 Heading 4
Row 1.1 Row 1.2 Row 1.3 Row 1.4
Row 2.1 Row 2.2 Row 2.3 Row 2.4
Row 3.1 Row 3.2 Row 3.3 Row 3.4
Row 4.1 Row 4.2 Row 4.3 Row 4.4

Add the Include element

Back into Tópico, open the topic you want the spreadsheet data and add the following <include> element:

XML
<include href="include_excel_worksheet.xml" xmlns="http://www.w3.org/2001/XInclude"
            xpointer="xpath1(ss:Workbook)" />

About the above <include> element:

  • The XML Spreadsheet is included as the source document;
  • The Workbook element that is referenced is in the "ss" prefixed namespace.

Tópico's ini file contains the list of namespace entries you can use in your publication. By default, namspaces are defined for the following prefixes:

Prefix Namespace description
xml The xml namespace (for xml:lang attributes)
xhtml The XHTML namespace
xi The XInclude namespace
o The office namespace
x The Excel namespace
ss The Excel Spreadsheet namespace
html The HTML namespace that's part of the XML Office files

Here's how the ini file looks like:

XML
<section id="namespaces">
 <set key="ns" prefix="xml" uri="http://www.w3.org/XML/1998/namespace" />
 <set key="ns" prefix="xhtml" uri="http://www.w3.org/1999/xhtml" />
 <set key="ns" prefix="html" uri="http://www.w3.org/TR/REC-html40" />
 <set key="ns" prefix="xi" uri="http://www.w3.org/2001/XInclude" />
 <set key="ns" prefix="o" uri="urn:schemas-microsoft-com:office:office" />
 <set key="ns" prefix="x" uri="urn:schemas-microsoft-com:office:excel" />
 <set key="ns" prefix="ss" uri="urn:schemas-microsoft-com:office:spreadsheet" />
 <set key="ns" prefix="atom" uri="http://www.w3.org/2005/Atom" />
 <set key="ns" prefix="sch" uri="http://www.ascc.net/xml/schematron" />
</section>		

You can add namespaces to this list to accomodate your specific requirements.

Create an XSLT template for the spreadsheet

Since you'll be introducing foreign elements in your topic, you'll need to come up with the appropriate transformation for your imported content. This will be done by creating a new template. To make you life easier, you'll simply import the existing template and add only the new markup to this file.

Here's a complete template for the spreadsheet above:

XSLT
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      version="1.0" xmlns:o="urn:schemas-microsoft-com:office:office" 
      xmlns:x="urn:schemas-microsoft-com:office:excel" 
      xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" 
      xmlns:html="http://www.w3.org/TR/REC-html40">
 <!-- templates parameters -->
 <xsl:import href="template_user_guide.xsl"/>
 <!--xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-->
 <!-- excel workbook template -->
 <xsl:template match="ss:Workbook" mode="import">
  <div class="excel_workbook">
   <xsl:apply-templates select="ss:Worksheet[@ss:Name='Sheet1']" mode="import"/>
  </div>
 </xsl:template>
 <!--xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-->
 <!-- excel worksheet template -->
 <xsl:template match="ss:Worksheet" mode="import">
  <div class="excel_worksheet">
   <strong>Excel Worksheet Data</strong>
   <table>
    <tbody>
     <xsl:apply-templates select="ss:Table/ss:Row" mode="import"/>
    </tbody>
   </table>
  </div>
 </xsl:template>
 <!--xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-->
 <!-- excel row template -->
 <xsl:template match="ss:Row" mode="import">
  <tr>
  <xsl:choose>
   <xsl:when test="position()=1">
    <xsl:apply-templates select="ss:Cell" mode="import_title"/>
   </xsl:when>
   <xsl:otherwise>
     <xsl:apply-templates select="ss:Cell" mode="import"/>
   </xsl:otherwise>
   </xsl:choose>
  </tr>
 </xsl:template>
 <!--xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-->
 <!-- excel cell template (title) -->
 <xsl:template match="ss:Cell" mode="import_title">
  <th>
   <xsl:apply-templates select="ss:Data" mode="import"/>
  </th>
 </xsl:template>
 <!--xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-->
 <!-- excel cell template (data) -->
 <xsl:template match="ss:Cell" mode="import">
  <td>
   <xsl:apply-templates select="ss:Data" mode="import"/>
  </td>
 </xsl:template>
</xsl:stylesheet>

The above template, which includes all the appropriate namespaces, first imports the existing template to get it's default output and then defines a new template for the ss:Workbook element.

XSLT
<xsl:template match="ss:Workbook" mode="import">

The ss:Workbook template then call sub elements with the appropriate "ss" prefix. This approache makes editing multiple templates easier than if you're trying to keep everything in one template.

Tip

Please note that the ss:Worksheet could have been referenced in the <include> element, making the template a bit shorter. You do not necessarily need to include the root of an external document.

Select an alternate template for the topic

Now that you have a specific template for your topic, you can assign this new template by following these 2 steps:

  1. Select the Topic tab;
  2. From the Topic template drop down list, select the alternate template.

Your now ready to publish your collection with the Excel data. The table at the top of this topic was included this way.

Referencing Access and Word content would require about the same workflow but with different elements and namespaces.

As an exercise, you can add CSS classes to your Excel tables in your template and assign them stylesheet selectors to make you Excel spreadsheet stand out in your publication.

THESE PAGES ARE STILL UNDER CONSTRUCTION AND DO NOT NECESSARELY REFLECT THE CURRENT VERSION OF TÓPICO

67 / 194