Implementing 'Inheritance' in XML

Some XML vocabularies implement a powerful XML pattern that is analogous to inheritance in programming language type systems. Open XML WordprocessingML has these semantics around styles. When you define a new style, you can base this style on another style, and if the new style doesn't explicitly define some aspect of the style, the new style 'inherits' this aspect from its base style. This post presents some code that uses one approach for implementing inheritance semantics.

This is one in a series of posts on transforming Open XML WordprocessingML to XHtml. You can find the complete list of posts here.

The Heading1 style is based on the CompanyThemeHeading1 style, which is itself based on RootStyle. The programming task is to 'roll up' these styles, and assemble a new Style element that contains all inherited child elements as appropriate. Note that the order of the Style elements in the XML document is not significant. The method to 'roll up' these styles should work properly regardless of the document order of the Style elements.

In the case of Open XML styles, there are three varieties of inheritance semantics:

Replace an element with the same element and attributes in the derived style.

Merge child elements with the corresponding element in the derived style. In the case of WordprocessingML, when an element has these semantics, it will not have any attributes, so we can disregard them.

Merge attributes with the attributes of the same element in the derived style. In the case of WordprocessingML, when an element has these semantics, it will not have any child nodes, so we can disregard them.

We should note that this doesn't cover all possibilities of inheritance semantics. Other possibilities:

When implementing this in code, the first task is to write an iterator that will return a collection of styles. We pass the StyleId of the most derived style, and this method will follow the style chain, yielding up each style in order.

We've retrieved the collection of relevant styles (Heading1, CompanyThemeHeading1, and RootStyle), but they are in the reverse order to the order that we want to process them. That is easy enough to fix - we write a method that returns the collection in reverse:

staticXElement MergeChildElements(XElement mergedElement, XElement element) {// If, when in the process of merging, the source element doesn't have a// corresponding element in the merged element, then include the source element// in the merged element.if (mergedElement == null)return element;

This is a recursive method - when merging child elements, we need to implement appropriate semantics for each of the child elements. To test the method, we merge the first two styles in the style chain. When we run this on our sample XML document, we see:

This is what we expected. We see the Font element from the CompanyThemeHeading1 style. The attributes of the VisualProps element are merged. Positioning, which has 'merge child elements' semantics, contains the correct SpaceAfter element, which replaced the SpaceAfter element in the RootStyle.

Now we can write a one statement method that uses the Aggregate extension method to roll up all styles:

staticXElement MergeChildElements(XElement mergedElement, XElement element) {// If, when in the process of merging, the source element doesn't have a// corresponding element in the merged element, then include the source element// in the merged element.if (mergedElement == null)return element;

This is what we expected. VisualProps has merged attributes, Font is inherited from CompanyThemeHeading1, and Positioning has the SpaceAfter element from the CompanyThemeHeading1 style, and the Indent element from the Heading1 style.

Eric, I'm primarily working with PresentationML and DrawingML and VB.NET, but your examples both work perfectly for me to port over and are exactly what I'm always looking for in terms of transforming OOXML to another XML vocabulary. Thank you so much for this blog, it is incredibly helpful!

Hey Otaku, I'm really glad you like it. This has been a really fun project, and I'm especially glad when it is useful to others.

-Eric

Todd

10 Mar 2010 6:38 PM

Hi Eric,

I had a quick question about the (e.Name == "VisualProps") part of your code. This picks up attributes from both the element and the MergedElement - but what if you had the same attribute in both, like ForeColor? I'm wondering how one overwrites the other with this code (sorry, my C# skills are not strong). Could you explain a little about how that may work?

That last bit of code is what does what you are asking about. That call to the Where extension method is what filters out any attributes that already exist for the element being cloned. This means that the new element picks up all of the attributes of the element that is overriding, and then any attributes of the base element where those attributes have not been overridden. This code uses one of my favorite idioms for writing robust code. It uses the Attributes extension method, which returns all attributes for all elements in the source collection.

The following post discusses this technique, although the examples it gives use the Elements extension method, but the same approach works for attributes.

Eric - is it okay to ask questions on this blog related to my code? I've used your concepts/direction above for a merge of PowerPoint title placeholder shapes (but I'm merging the slide shape into the slideLayout shape). If it's not okay, that's cool, just wondering if you help look at other's code and comment.

Cheers,

Todd

Todd

11 Mar 2010 7:37 PM

Well, I'm a bit desperate here, so I thought I would take a chance. Almost all of this code is working as I need it, the issue is the <a:p> tags. I can see where it's wrong, but just can't figure out how to fix it.

Just curious, how big is it? I have sometimes very large XML documents - more than 5 million elements, and still can process in-memory fairly easily. I'm wondering what is the max size that people encounter.