Normalizing Excel’s SpreadsheetML using XSLT

Spreadsheet tables are full of holes and spreadsheet processors such as OpenOffice or Excel have implemented hacks to avoid having to store empty cells.

In the case of Excel, that’s done using ss:Index and ss:MergeAcross attributes.

While these attributes are easy enough to understand, they add a great deal of complexity to XSLT transformations that need to access to a specific cell since you can’t any longer index directly your target.

The traditional way to work around this kind of issue is to pre-process your spreadsheet document to get an intermediary result that lets you index your target cells.

Having already encountered this issue with OpenOffice, I needed something to do the same with Excel when Google led me to a blog entry proposing a transformation something similar.

The transformation needed some adaptation to be usable as I wanted to use it, ie as a transformation that does not modify your SpreadsheetML document except for inserting an ss:Index attribute to every cell.

Here is the result of this adaptation:

This version is buggy. An updated one is available here

<?xml version="1.0"?>

Adapted from

This product may incorporate intellectual property owned by Microsoft Corporation. The terms
and conditions upon which Microsoft is licensing such intellectual property may be found at
<xsl:stylesheet version="1.0" xmlns:xsl=""
    <xsl:output method="xml" indent="no" encoding="UTF-8"/>
    <xsl:template match="@*|node()">
            <xsl:apply-templates select="@*|node()"/>
    <xsl:template match="ss:Cell/@ss:Index"/>
    <xsl:template match="ss:Cell">
            <xsl:apply-templates select="@*"/>
            <xsl:variable name="prevCells" select="preceding-sibling::ss:Cell"/>
            <xsl:attribute name="ss:Index">
                    <xsl:when test="@ss:Index">
                        <xsl:value-of select="@ss:Index"/>
                    <xsl:when test="count($prevCells) = 0">
                        <xsl:value-of select="1"/>
                    <xsl:when test="count($prevCells[@ss:Index]) > 0">
                        <xsl:value-of select="($prevCells[@ss:Index][1]/@ss:Index) +
                            ((count($prevCells) + 1) -
                            + 1)) + sum($prevCells/@ss:MergeAcross)"/>
                        <xsl:value-of select="count($prevCells) + 1 +
            <xsl:apply-templates select="node()"/>

This version is buggy. An updated one is available here

Share and Enjoy:
  • StumbleUpon
  • Facebook
  • Twitter
  • Add to favorites

3 thoughts on “Normalizing Excel’s SpreadsheetML using XSLT”

  1. this code has the same problem as the source it is derived from, it will fail for all cells lacking an ss:Index that are preceded by two or more missing cells

    for instance, the input cells (two missing cells, each followed by a cell with an ss:Index)…


    will result in output cells with indices…


    i haven’t tested fully, but in practice i think the output index count is one too low for each one or more contiguous cells with an ss:Index attribute

    i just wish my xsl skills were sufficient to fix it!

  2. I have rendered some XML data into spreadsheetML however I am llooking for a way to include some vba/vb script when i open this file in Excel, does anyone know how to do this?

Leave a Reply

Your email address will not be published. Required fields are marked *

Enter your OpenID as your website to log and skip name and email validation and moderation!