{"id":2028,"date":"2016-02-04T01:04:38","date_gmt":"2016-02-04T01:04:38","guid":{"rendered":"http:\/\/www.ericwhite.com\/home2\/bm8qcmjy\/public_html\/blog\/?page_id=2028"},"modified":"2018-05-31T21:40:06","modified_gmt":"2018-05-31T21:40:06","slug":"open-xml-element-and-attribute-names","status":"publish","type":"page","link":"https:\/\/www.ericwhite.com\/blog\/open-xml-element-and-attribute-names\/","title":{"rendered":"Open XML Element and Attribute Names"},"content":{"rendered":"<p class=\"apiPara\"><span class=\"Back\"><a class=\"Back\" href=\"https:\/\/www.ericwhite.com\/blog\/open-xml-sdk-for-javascript\/\">Return to the<br \/>Open XML SDK for JavaScript<br \/>Developer Center<\/a><\/span>There are hundreds of tag names in Open XML, in a wide variety of namespaces.&nbsp; One effective approach is to initialize many of these names and namespaces, so that instead of using strings, we can use properties of objects.<\/p>\n<p class=\"apiPara\">Every <b>XName<\/b> object in LINQ to XML for JavaScript is &lsquo;atomized&rsquo;, which means that if you initialize two variables to the same qualified tag name, they will both be references to the same object.&nbsp; 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.<\/p>\n<p class=\"apiPara\">The blog post, <a href=\"http:\/\/blogs.msdn.com\/b\/ericwhite\/archive\/2007\/12\/06\/atomized-xname-and-xnamespace-objects.aspx\" class=\"broken_link\">Atomized XName and XNamespace Objects<\/a>, while about LINQ to XML for the .NET<br \/>\nFramework, not JavaScript, gives an explanation of what atomization is.&nbsp; The blog post <a href=\"http:\/\/ericwhite.com\/blog\/linq-to-xml-for-javascript-gaining-performance-through-atomization\/\">LINQto XML for JavaScript &#8211; Gaining Performance through Atomization<\/a> discusses this from the JavaScript point of view.<\/p>\n<p class=\"apiPara\">If you look in <b>openxml.js<\/b>, you will see a number of initialized objects.&nbsp; They all follow the following pattern:<\/p>\n<pre class=\"prettyprint\">openXml.wNs = new XNamespace(&quot;http:\/\/schemas.openxmlformats.org\/wordprocessingml\/2006\/main&quot;);\r\nvar wNs = openXml.wNs;\r\nopenXml.W = {\r\n    abstractNum: new XName(wNs, &quot;abstractNum&quot;),\r\n    abstractNumId: new XName(wNs, &quot;abstractNumId&quot;),\r\n    accent1: new XName(wNs, &quot;accent1&quot;),\r\n    accent2: new XName(wNs, &quot;accent2&quot;),\r\n    accent3: new XName(wNs, &quot;accent3&quot;),\r\n    accent4: new XName(wNs, &quot;accent4&quot;),\r\n    accent5: new XName(wNs, &quot;accent5&quot;),\r\n    accent6: new XName(wNs, &quot;accent6&quot;),\r\n    activeRecord: new XName(wNs, &quot;activeRecord&quot;),\r\n    ....\r\n};<\/pre>\n<p class=\"apiPara\">The use of these objects is as follows:<\/p>\n<pre class=\"prettyprint\">\/\/ Open a blank document that is stored as a base64 string.\r\nvar doc = new openXml.OpenXmlPackage(blankDocument_base64);\r\n\r\n\/\/ Create a paragraph.\r\nvar p = new XElement(W.p,\r\n    new XElement(W.r,\r\n        new XElement(W.t, &quot;Hello Open XML World&quot;)));\r\n\r\n\/\/ Replace the first paragraph in the document with the new paragraph.\r\ndoc.mainDocumentPart()\r\n    .getXDocument()\r\n    .descendants(W.p)\r\n    .firstOrDefault()\r\n    .replaceWith(p);<\/pre>\n<p class=\"apiPara\">When writing the code to pre-atomize the <b>XName<\/b> and <b>XNamespace<\/b> objects, I elected to not to attempt to have all tag names for the entire Open XML standard.&nbsp; Instead, I opted for the most commonly used tag names.&nbsp; While I do not expect you to regularly encounter the need for <b>XName<\/b> objects other than the ones that I have initialized in <b>openxml.js<\/b>, if you do need a non-initialized <b>XName<\/b> object, you can initialize it in place.&nbsp; You use the <b>new<\/b> operator with the <b>XName <\/b>object:<\/p>\n<pre class=\"prettyprint\">var newName = new XName(wNs, &quot;newName&quot;);<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Return to theOpen XML SDK for JavaScriptDeveloper CenterThere are hundreds of tag names in Open XML, in a wide variety of namespaces.&nbsp; 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"_s2mail":"","footnotes":""},"class_list":["post-2028","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/2028","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/comments?post=2028"}],"version-history":[{"count":4,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/2028\/revisions"}],"predecessor-version":[{"id":7329,"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/pages\/2028\/revisions\/7329"}],"wp:attachment":[{"href":"https:\/\/www.ericwhite.com\/blog\/wp-json\/wp\/v2\/media?parent=2028"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}