Open XML Element and Attribute Names

Return to the
Open XML SDK for JavaScript
Developer Center
There are hundreds of tag names in Open XML, in a wide variety of namespaces.  One effective approach is to initialize many of these names and namespaces, so that instead of using strings, we can use properties of objects.

Every XName object in LINQ to XML for JavaScript is ‘atomized’, which means that if you initialize two variables to the same qualified tag name, they will both be references to the same object.  A key performance advantage is that if we want to compare a tag name of an element to a given qualified tag name, then instead of doing string comparisons, which might tend to be slow, we can simply compare object identities.

The blog post, Atomized XName and XNamespace Objects, while about LINQ to XML for the .NET
Framework, not JavaScript, gives an explanation of what atomization is.  The blog post LINQ
to XML for JavaScript – Gaining Performance through Atomization
discusses this from the JavaScript point of view.

If you look in openxml.js, you will see a number of initialized objects.  They all follow the following pattern:

openXml.wNs = new XNamespace("");
var wNs = openXml.wNs;
openXml.W = {
    abstractNum: new XName(wNs, "abstractNum"),
    abstractNumId: new XName(wNs, "abstractNumId"),
    accent1: new XName(wNs, "accent1"),
    accent2: new XName(wNs, "accent2"),
    accent3: new XName(wNs, "accent3"),
    accent4: new XName(wNs, "accent4"),
    accent5: new XName(wNs, "accent5"),
    accent6: new XName(wNs, "accent6"),
    activeRecord: new XName(wNs, "activeRecord"),

The use of these objects is as follows:

// Open a blank document that is stored as a base64 string.
var doc = new openXml.OpenXmlPackage(blankDocument_base64);

// Create a paragraph.
var p = new XElement(W.p,
    new XElement(W.r,
        new XElement(W.t, "Hello Open XML World")));

// Replace the first paragraph in the document with the new paragraph.

When writing the code to pre-atomize the XName and XNamespace objects, I elected to not to attempt to have all tag names for the entire Open XML standard.  Instead, I opted for the most commonly used tag names.  While I do not expect you to regularly encounter the need for XName objects other than the ones that I have initialized in openxml.js, if you do need a non-initialized XName object, you can initialize it in place.  You use the new operator with the XName object:

var newName = new XName(wNs, "newName");