Introduction to Markup Compatibility

Applies to: Excel 2010 | Office 2010 | PowerPoint 2010 | Word 2010

In this article
Introduction
What is Markup Compatibility?
Markup Compatibility in the Open XML File Formats Specification
Open XML SDK 2.0 Support for Markup Compatibility
Setting the stage when you Open
What Happens During Preprocessing
Understanding Process Mode
Setting the Target File Format Version

This topic introduces the markup compatibility features included in the Open XML SDK 2.0 for Microsoft Office.

Introduction

Suppose you have a Microsoft Word 2010 document that employs a feature introduced in Microsoft Office 2010. When you open that document in Microsoft Office Word 2007, an earlier version, what should happen? Ideally, you want the document to remain interoperable with Office Word 2007, even though Office Word 2007 will not understand the new feature.

Consider also what should happen if you open that document in a hypothetical later version of Office, for example, Office 2012. Here too, you want the document to work as expected. That is, you want the later version of Office to understand and support a feature employed in a document produced by Word 2010.

Open XML anticipates these scenarios. The Office Open XML File Formats specification describes facilities for achieving the above desired outcomes in ECMA-376, Second Edition, Part 3 – Markup Compatibility and Extensibility.

The Open XML SDK 2.0 supports markup compatibility in a way that makes it easy for you to achieve the above desired outcomes for Office 2007 and Office 2010 without having to necessarily become an expert in the specification details.

What is Markup Compatibility?

Open XML defines formats for word-processing, spreadsheet and presentation documents in the form of specific markup languages, namely WordprocessingML, SpreadsheetML, and PresentationML. With respect to the Open XML file formats, markup compatibility is the ability for a document expressed in one of the above markup languages to facilitate interoperability between applications, or versions of an application, with different feature sets. This is supported through the use of a defined set of XML elements and attributes in the Markup Compatibility namespace of the Open XML specification. Notice that while the markup is supported in the document format, markup producers and consumers, such as Microsoft Word, must support it as well. In other words, interoperability is a function of support both in the file format and by applications.

Markup Compatibility in the Open XML File Formats Specification

Markup compatibility is discussed in ECMA-376, Second Edition, Part 3 – Markup Compatibility and Extensibility, which is recommended reading to understand markup compatibility. The specification defines XML attributes to express compatibility rules, and XML elements to specify alternate content. For example, the Ignorable attribute specifies namespaces that can be ignored when they are not understood by the consuming application. Alternate-Content elements specify markup alternatives that can be chosen by an application at run time. For example, Office Word 2007 can choose only the markup alternative that it recognizes. The complete list of compatibility-rule attributes and alternate-content elements and their details can be found in the specification.

Open XML SDK 2.0 Support for Markup Compatibility

The work that the Open XML SDK 2.0 does for markup compatibility is detailed and subtle. However, the goal can be summarized as: using settings that you assign when you open a document, preprocess the document to:

  1. Filter or remove any elements from namespaces that will not be understood (for example, Office 2010 document opened in Office 2007 context)

  2. Process any markup compatibility elements and attributes as specified in the Open XML specification.

The preprocessing performed is in accordance with ECMA-376, Second Edition: Part 3.13.

The Open XML SDK 2.0 support for markup compatibility comes primarily in the form of two classes and in the manner in which content is preprocessed in accordance with ECMA-376, Second Edition. The two classes are OpenSettings and MarkupCompatibilityProcessSettings. Use the former to provide settings that apply to SDK behavior overall. Use the latter to supply one part of those settings, specifically those that apply to markup compatibility.

Setting the stage when you Open

When you open a document using the Open XML SDK 2.0, you have the option of using an overload with a signature that accepts an instance of the OpenSettings class as a parameter. You use the open settings class to provide certain important settings that govern the behavior of the SDK. One set of settings in particular, stored in the MarkupCompatibilityProcessSettings property, determines how markup compatibility elements and attributes are processed. You set the property to an instance of the MarkupCompatibilityProcessSettings class prior to opening a document.

The class has the following properties:

By default, documents are not preprocessed. If however you do specify open settings and provide markup compatibility process settings, then the document is preprocessed in accordance with those settings.

The following code example demonstrates how to call the Open method with an instance of the open settings class as a parameter. Notice that the ProcessMode and TargetFileFormatVersions properties are initialized as part of the MarkupCompatiblityProcessSettings constructor.

// Create instance of OpenSettings
OpenSettings openSettings = new OpenSettings();

// Add the MarkupCompatibilityProcessSettings
openSettings.MarkupCompatibilityProcessSettings =
    new MarkupCompatibilityProcessSettings(
        MarkupCompatibilityProcessMode.ProcessAllParts, 
        FileFormatVersions.Office2007);

// Open the document with OpenSettings
using (WordprocessingDocument wordDocument = 
    WordprocessingDocument.Open(filename, 
        true,
        openSettings))
{
    // ... more code here
}

What Happens During Preprocessing

During preprocessing, the Open XML SDK 2.0 removes elements and attributes in the markup compatibility namespace, removing the contents of unselected alternate-content elements, and interpreting compatibility-rule attributes as appropriate. This work is guided by the process mode and target file format versions properties.

The ProcessMode property determines the parts to be preprocessed. The content in those parts is filtered to contain only elements that are understood by the application version indicated in the TargetFileFormatVersions property.

Warning

Preprocessing affects what gets saved. When you save a file, the only markup that is saved is that which remains after preprocessing.

Understanding Process Mode

The process mode specifies which document parts should be preprocessed. You set this property to a member of the MarkupCompatibilityProcessMode enumeration. The default value, NoProcess, indicates that no preprocessing is performed. Your application must be able to understand and handle any elements and attributes present in the document markup, including any of the elements and attributes in the Markup Compatibility namespace.

You might want to work on specific document parts while leaving the rest untouched. For example, you might want to ensure minimal modification to the file. In that case, specify ProcessLoadedPartsOnly for the process mode. With this setting, preprocessing and the associated filtering is only applied to the loaded document parts, not the entire document.

Finally, there is ProcessAllParts, which specifies what the name implies. When you choose this value, the entire document is preprocessed.

Setting the Target File Format Version

The target file format versions property lets you choose to process markup compatibility content in either Office 2007 or Office 2010 context. Set the TargetFileFormatVersions property to a member of the FileFormatVersions enumeration.

The default value, Office2007, means the SDK will assume that namespaces defined in Office 2007 are understood, but not namespaces defined in Office 2010. Thus, during preprocessing, the SDK will ignore the namespaces defined in Office 2010 and choose the Office 2007 compatible alternate-content.

When you set the target file format versions property to Office2010, the Open XML SDK 2.0 assumes that all of the namespaces defined in Office 2007 and Office 2010 are understood, does not ignore any content defined under Office 2010, and will choose the Office 2010 compatible alternate-content.

See Also

Other Resources

Open XML SDK 2.0 Class Library Reference