Cleanup of the Element and Processing Instruction parts of algorithm, plus some
authortravil
Sat, 22 Mar 2014 17:02:28 -0700
changeset 39 1860a8d798cf
parent 38 fb9edcfb8f5f
child 40 5fa98c6547a4
Cleanup of the Element and Processing Instruction parts of algorithm, plus some
miscellaneous editorical changes.
index.html
--- a/index.html	Fri Mar 21 17:46:08 2014 -0700
+++ b/index.html	Sat Mar 22 17:02:28 2014 -0700
@@ -265,28 +265,25 @@
                 for associating <code>namespaceURI</code> and namespace <code>prefix</code> pairs, where 
                 <code>namespaceURI</code> values are the map's keys, and <code>prefix</code> values are 
                 the map's key values. The <a title="concept-namespace-prefix-map">namespace prefix map</a>
-                will contain the history of assigned namespace prefixes [[XML-NAMES]] that have been 
-                serialized by the <a title="concept-serialize-xml">XML serialization</a> algorithm for a 
-                subtree. <span class="note"><strong>Note:</strong> the 
+                will be populated by previously seen namespaceURIs and their most recent prefix associations
+                for a subtree. <span class="note"><strong>Note:</strong> the 
                 <a title="concept-namespace-prefix-map">namespace prefix map</a> only associates a single
-                prefix value with a given namespaceURI. During serialization, where multiple prefixes are 
-                found to map to the same namespaceURI, the last one encountered "wins" by replacing the
+                prefix value with a given namespaceURI. During serialization, if different namespace prefixes 
+                are found that map to the same namespaceURI, the last one encountered "wins" by replacing the
                 existing key value in the map with the new prefix value.</span>
-            <li>Initialize the <a title="concept-namespace-prefix-map">namespace prefix map</a> with a key
-                whose string value is the <a title="xml-ns">XML namespace</a> and string <code>"xml"</code> as the 
-                key value.
+            <li>Initialize the <a title="concept-namespace-prefix-map">namespace prefix map</a> with the 
+                <a title="xml-ns">XML namespace</a> key and string <code>"xml"</code> as the key value.
             <li>Let <dfn title="concept-generated-prefix">generated namespace prefix index</dfn> be an integer
                 with a value of <code>1</code>. The <a title="concept-generated-prefix">generated namespace
-                prefix index</a> is used when an attribute not in the <code>null</code> namespace lacks
-                a suitable <a title="dom-attribute-prefix" data-spec="DOM4" class="externalDFN">prefix</a> to 
-                represent it. The <a title="concept-generated-prefix">generated namespace prefix index</a> is
-                appended to the string <code>"ns"</code> in these cases and added to the the 
-                <a title="concept-namespace-prefix-map">namespace prefix map</a>.
+                prefix index</a> is used to generate a new unique prefix value when no suitable existing 
+                namespace prefix is available to serialize a <var>node</var>'s namespaceURI (or the namespaceURI
+                of one of <var>node</var>'s attributes). <span class="note">See the 
+                <a title="concept-generate-prefix">generate a prefix</a> algorithm.</span>
             <li>Return an <a title="concept-serialize-xml">XML serialization</a> of <var>node</var>
-                providing to the algorithm <a title="concept-context-namespace">context namespace</a> 
-                as the <var>namespace</var>, <a title="concept-namespace-prefix-map">namespace prefix map</a>
-                as <var>prefixes</var>, and <a title="concept-generated-prefix">generated namespace prefix index</a>
-                as <var>prefix index</var>.
+                providing to the algorithm a <a title="concept-context-namespace">context namespace</a> 
+                <var>namespace</var>, a <a title="concept-namespace-prefix-map">namespace prefix map</a>
+                <var>prefixes</var>, and a <a title="concept-generated-prefix">generated namespace prefix index</a>
+                <var>prefix index</var>.
         </ol>
 
         <p>To produce an <dfn title="concept-serialize-html">HTML serialization</dfn> of a
@@ -298,33 +295,29 @@
         <p>An <a title="concept-serialize-xml">XML serialization</a> differs from an 
             <a title="concept-serialize-html">HTML serialization</a> in the following ways:
         <ul>
-            <li><a title="element" data-spec="DOM4" class="externalDFN">Element</a>s will always be
-                serialized with an explicitly defined namespace.
-            <li><a title="concept-attribute" data-spec="DOM4" class="externalDFN">Attribute</a>s with
-                non-null
-                <a title="dom-attr-namespaceuri" data-spec="DOM4" class="externalDFN">namespaceURI</a>s
-                other than those in the <a title="xml-ns">XML</a> or <a title="xmlns-ns">XMLNS</a> 
-                namespaces are explicitly serialized with their namespaces defined. (An 
-                <a title="concept-serialize-html">HTML serialization</a> will drop these namespaces.)
-            <li><a title="element" data-spec="DOM4" class="externalDFN">Element</a>s not in the
+            <li><a title="element" data-spec="DOM4" class="externalDFN">Elements</a> and
+                <a title="concept-attribute" data-spec="DOM4" class="externalDFN">attributes</a>
+                will always be serialized with their namespaceURI explicitly defined (if non-null).
+                An <a title="concept-serialize-html">HTML serialization</a> will drop these namespaces.
+            <li><a title="element" data-spec="DOM4" class="externalDFN">Elements</a> not in the
                 <a title="html-ns">HTML namespace</a> containing no children, are serialized using 
-                the self-closing tag syntax (<var>EmptyElemTag</var> production [[XML10]]).
+                the self-closing tag syntax (i.e., according to the <var>EmptyElemTag</var> 
+                production of [[XML10]]).
         </ul>
         
-        <p>Otherwise, the <a title="concept-serialize-xml">algorithm for serializing a node
-            belonging to an XML document</a> is designed to produce a serialization that is compatible 
-            with the <a title="html-parser" data-spec="HTML5" class="externalDFN">HTML parser</a>. For 
-            example, elements in the <a title="html-ns">HTML namespace</a> that contain no child nodes 
-            are serialized with an explicit begin and end tag rather than using the self-closing tag 
-            syntax [[XML10]].
+        <p>Otherwise, the algorithm for producing an <a title="concept-serialize-xml">XML serialization</a> 
+            is designed to produce a serialization that is compatible with the 
+            <a title="html-parser" data-spec="HTML5" class="externalDFN">HTML parser</a>. For example, 
+            elements in the <a title="html-ns">HTML namespace</a> that contain no child nodes are serialized 
+            with an explicit begin and end tag rather than using the self-closing tag syntax [[XML10]].
         
-        <p class="note">An <a title="concept-serialize-xml">XML serialization</a> may not always 
-            produce a well-formed [[XML10]] serialization. Notably, some DOM attribute operations 
-            (such as <a title="dom-element-setattribute" data-spec="DOM4" class="externalDFN">setAttribute</a>)
-            do not enforce XML validity constraints, such as disallowing use of attribute names that
-            resemble XMLNS namespace declarations or namespace prefix definitions. The 
-            <a title="concept-serialize-xml">XML serialization</a> algorithm defined in this document
-            does not escape, encode, or fail to serialize such attribute names.
+        <p class="note">An <a title="concept-serialize-xml">XML serialization</a> might not always 
+            produce a <a data-spec="XML10" title="sec-well-formed" class="externalDFN">well-formed</a>
+            serialization. For example, the <a title="dom-element-setattribute" data-spec="DOM4" 
+            class="externalDFN">setAttribute</a> method does not enforce XML validity constraints, 
+            like disallowing use of attribute names that resemble namespace declarations or prefix 
+            definitions. The <a title="concept-serialize-xml">XML serialization</a> algorithm defined 
+            in this document does not escape, encode, or fail to serialize such attribute names.
         
         <p>To produce an <dfn title="concept-serialize-xml">XML serialization</dfn> of a
             <a title="node" data-spec="DOM4" class="externalDFN">Node</a> <var>node</var> given a
@@ -345,18 +338,18 @@
                     <li>Let <var>markup</var> be an empty string.
                     <li>Let <var>qualified name</var> be an empty string.
                     <li>Let <var>generated attribute</var> be an empty string.
-                    <li>Let <var>default namespace found</var> be <code>false</code>.
-                    <li>Let <var>map</var> be a copy of the <var>prefixes</var> <a title="concept-namespace-prefix-map">namespace prefix map</a>.
+                    <li>Let <var>map</var> be a copy of the <var>prefixes</var> <a 
+                        title="concept-namespace-prefix-map">namespace prefix map</a>.
                     <li>Let <var>element prefixes list</var> be an empty list. <span class="note">This list is 
                         local to each element. Its purpose is to ensure that there are no conflicting prefixes 
                         should a new namespace prefix attribute need to be generated.</span>
-                    <li><a title="concept-find-defined-prefixes">Find the defined namespace prefixes</a> for 
-                        <var>node</var> given <var>map</var>, <var>element prefixes list</var>, and the 
-                        <var>default namespace found</var> flag.
+                    <li>Let <var>default namespace</var> get the result of <a title="concept-record-namespace-info">recording 
+                        the namespace information</a> for <var>node</var> given <var>map</var> and 
+                        <var>element prefixes list</var>.
                     <p class="note">This above step will update the <var>map</var> with any found namespace prefix 
-                        definitions, add the found prefix definitions to the <var>element prefixes list</var>, and set 
-                        the <var>default namespace found</var> flag to <code>true</code> if a default namespace declaration 
-                        is found.</p>
+                        definitions, add the found prefix definitions to the <var>element prefixes list</var>, and return 
+                        the namespace value defined by a default namespace attribute if one exists. Otherwise it returns
+                        an empty string.</p>
                     <li>Let <var>prefix</var> be the value of <var>node</var>'s 
                         <code><a title="dom-element-prefix" data-spec="DOM4" class="externalDFN">prefix</a></code>
                         attribute.
@@ -365,19 +358,49 @@
                         attribute.
                     <li>Let a <var>skip end tag</var> flag have the value <code>false</code>.
                     <li>Append "<code>&lt;</code>" (U+003C LESS-THAN SIGN) to <var>markup</var>.
-                    <li>If <var>namespace</var> is not equal to <var>ns</var> (the <var>node</var>'s
-                        own namespace is different from its parent), <!-- Then signal this in the serialization by way of either 1) a namespace prefix if one is available (this is shorter) or 2) a default namespace otherwise) -->
-                        then run these sub-steps:
+                    <li>If <var>namespace</var> is equal to <var>ns</var>, then:
+                    <ol>
+                        <li>If <var>ns</var> is the <a title="xml-ns">XML namespace</a>, then let <var>qualified name</var> 
+                            be the concatenation of the string "<code>xml:</code>" and the value of <var>node</var>'s 
+                            <a title="dom-element-localname" data-spec="DOM4" class="externalDFN">localName</a>.
+                        <li>Otherwise, if <var>node</var>'s <a title="dom-element-prefix" data-spec="DOM4" 
+                            class="externalDFN">prefix</a> is not <code>null</code>, the let <var>qualified name</var> 
+                            be the concatenation of the value of <var>node</var>'s <a title="dom-element-prefix" 
+                            data-spec="DOM4" class="externalDFN">prefix</a>, "<code>:</code>" (U+003A COLON), and 
+                            the value of <var>node</var>'s <a title="dom-element-localname" data-spec="DOM4" 
+                            class="externalDFN">localName</a>.
+                        <li>Otherwise, <var>node</var>'s <a title="dom-element-prefix" data-spec="DOM4" 
+                            class="externalDFN">prefix</a> is <code>null</code>. Let <var>qualified name</var> be
+                            the value of <var>node</var>'s <a title="dom-element-localname" data-spec="DOM4" 
+                            class="externalDFN">localName</a>.
+                    </ol>
+                    <li>Otherwise, <var>namespace</var> is not equal to <var>ns</var> (the <var>node</var>'s
+                        own namespace is different from its parent). Run these sub-steps:
+                    <!-- The serialization algorithm must differentiate this node's namespace from it's parent. There
+                        are two ways to do this: (1) [preferred due to assumed minimum length] use a namespace prefix if one 
+                        is available or (2) use a default namespace declaration. Both cases can run into conflicts
+                        with existing attributes on the element and are be handled accordingly. -->
                     <ol>
                         <li>Let <var>candidate prefix</var> be a value from <var>map</var> where there exists a key in 
-                            <var>map</var> that matches the value of <var>ns</var> or if there is no such key, then 
-                            <code>null</code>.
+                            <var>map</var> that matches the value of <var>ns</var> or if there is no such key, then let
+                            <var>candidate prefix</var> be <code>null</code>.
+                        <!-- Found a suitable prefix to use, either locally, or inherited through a parent node that 
+                            matches the node's namespaceURI. This prefix will be used in serialization even if the node 
+                            doesn't have a real prefix. -->
                         <li>If <var>candidate prefix</var> is not <code>null</code>, then let <var>qualified name</var>
                             be the concatenation of <var>candidate prefix</var>, "<code>:</code>" (U+003A COLON), and
                             <code><a title="dom-element-localname" data-spec="DOM4" class="externalDFN">localName</a></code>
-                            and stop running these sub-steps.
-                        <!-- Didn't find an existing (or locally defined) mapping for node's namespace -->
-                        <!-- Try to use the existing prefix? -->
+                            and stop running these sub-steps. <span class="note">(There exists on this <var>node</var>
+                            or the <var>node</var>'s ancestry a namespace prefix definition that defines the <var>node</var>'s
+                            namespace.)</span>
+                        <li>If the value of <var>default namespace</var> is equal to <var>ns</var>, then let
+                            <var>qualified name</var> be the <var>node</var>'s <code><a title="dom-element-localname" 
+                            data-spec="DOM4" class="externalDFN">localName</a></code> and stop running these sub-steps.
+                            <span class="note">(This <var>node</var>'s existing default namespace declaration defines
+                            the <var>node</var>'s namespace.)</span>
+                        <!-- At this point, no pre-existing (or locally defined) prefix or default namespace has been 
+                            declared to map to node's namespaceURI. Something will need to be synthesized... -->
+                        <!-- (1) Try to use node's existing prefix -->
                         <li>If <var>prefix</var> is not <code>null</code> and the <var>element prefixes list</var> does 
                             not contain the value of <var>prefix</var>, then let <var>qualified name</var> be the concatenation
                             of <var>prefix</var>, "<code>:</code>" (U+003A COLON), and 
@@ -390,16 +413,33 @@
                             <li>The value of <var>ns</var>;
                             <li>"<code>"</code>" (U+0022 QUOTATION MARK);
                         </ol>
-                        <li>If <var>prefix</var> is not <code>null</code> and the <var>element prefixes list</var>
-                            contains the value of <var>prefix</var>, then: <!-- The element's prefix is used locally to define a different namespace! The node's prefix can't be used to generate a duplicate attribute name, so... -->
+                        <!-- (2) Node doesn't have a prefix; try generating a default namespace -->
+                        <li>If prefix is <code>null</code> and <var>default namespace</var> is the empty string,
+                            then let <var>qualified name</var> be <var>node</var>'s
+                            <code><a title="dom-element-localname" data-spec="DOM4" class="externalDFN">localName</a></code>
+                            and let <var>generated attribute</var> be the concatenation of:
+                        <ol>
+                            <li>the string "<code>xmlns</code>";
+                            <li>"<code>="</code>" (U+003D EQUALS SIGN, U+0022 QUOTATION MARK);
+                            <li>The value of <var>ns</var>;
+                            <li>"<code>"</code>" (U+0022 QUOTATION MARK);
+                        </ol>
+                        <!-- (3) Trouble: At this point, either (1) the node HAS a prefix, but another local namespace 
+                            prefix definition has hijacked the node's prefix to refer to a different namespaceURI, or
+                            (2) the node has NO prefix, but does have an existing default namespace declaration which
+                            unfortunately is defining a namespace which is not node's namespace. Break out the big guns
+                            and force a new [generated] prefix on the node to workaround these issues. -->
+                        <li>If <var>generated attribute</var> is still the empty string, then run the following steps to
+                            serialize a new generated prefix for <var>node</var>:
                         <ol>
                             <li>Let <var>generated prefix</var> be the result of 
                                 <a title="concept-generate-prefix">generating a prefix</a> providing as input the
                                 <a title="concept-namespace-prefix-map">namespace prefix map</a> <var>map</var>, 
                                 <var>node</var>'s <var>ns</var> string, and the <var>prefix index</var> integer.
                             <li>Let <var>qualified name</var> be the concatenation of <var>generated prefix</var>, 
-                                "<code>:</code>" (U+003A COLON), and 
-                                <code><a title="dom-element-localname" data-spec="DOM4" class="externalDFN">localName</a></code>.
+                                "<code>:</code>" (U+003A COLON), and <var>node</var>'s
+                                <code><a title="dom-element-localname" data-spec="DOM4" 
+                                class="externalDFN">localName</a></code> value.
                             <li>Let <var>generated attribute</var> be the concatenation of:
                             <ol>
                                 <li>the string "<code>xmlns:</code>";
@@ -409,25 +449,15 @@
                                 <li>"<code>"</code>" (U+0022 QUOTATION MARK);
                             </ol>
                         </ol>
-                        <!-- prefix is null -- generate a default namespace -->
-                        <li>If prefix is <code>null</code> then let <var>qualified name</var> be <var>node</var>'s
-                            <code><a title="dom-element-localname" data-spec="DOM4" class="externalDFN">localName</a></code>
-                            and let <var>generated attribute</var> be the concatenation of:
-                        <ol>
-                            <li>the string "<code>xmlns</code>";
-                            <li>"<code>="</code>" (U+003D EQUALS SIGN, U+0022 QUOTATION MARK);
-                            <li>The value of <var>ns</var>;
-                            <li>"<code>"</code>" (U+0022 QUOTATION MARK);
-                        </ol>
                     </ol>
                     <li>Append the value of <var>qualified name</var> to <var>markup</var>.
-                    <li>Append a "<code> </code>" (U+0020 SPACE) to <var>markup</var>.
-                    <li>Append the value of <var>generated attribute</var> to <var>markup</var>.
+                    <li>If <var>generated attribute</var> is not the empty string, append the concatenation of 
+                        "<code> </code>" (U+0020 SPACE) with the value of <var>generated attribute</var> to <var>markup</var>.
                     <li>Append to <var>markup</var> the result of the 
                         <a title="concept-serialize-xml-attributes">XML serialization of <var>node</var>'s 
-                        attributes</a> given a 
-                        <a title="concept-namespace-prefix-map">namespace prefix map</a> <var>map</var>.
-
+                        attributes</a> given the
+                        <a title="concept-namespace-prefix-map">namespace prefix map</a> <var>map</var> and
+                        the <a title="concept-generated-prefix">generated prefix index</a> <var>prefix index</var>.
                     <li>If <var>ns</var> is the <a title="html-ns">HTML namespace</a>,
                         and the <var>node</var>'s list of 
                         <a title="concept-tree-child" data-spec="DOM4" class="externalDFN">children</a>
@@ -495,8 +525,8 @@
                     <li>The string produced by an <a title="concept-serialize-xml">XML serialization</a>
                         of <var>node</var>'s 
                         <a title="dom-document-documentelement" data-spec="HTML5" class="externalDFN">documentElement</a>
-                        attribute, providing <code>null</code> as the <var>namespace</var> and an 
-                        empty list as <var>prefixes</var>. 
+                        attribute, providing <code>null</code> as the <var>namespace</var>, <var>map</var>
+                        for <var>prefixes</var>, and <var>prefix index</var>. 
                 </ol>
 
             <dt><code><a title="comment" data-spec="DOM4" class="externalDFN">Comment</a></code>
@@ -508,17 +538,8 @@
             <dt><code><a title="cdata" data-spec="DOML2" class="externalDFN">CDATASection</a></code>
             <dd><ol>
                 <li>Let <var>markup</var> be the concatenation of "<code>&lt;![CDATA[</code>", 
-                <var>node</var>'s
-                <code><a title="dom-characterdata-data" data-spec="DOM4" class="externalDFN">data</a></code>, 
-                and "<code>]]></code>".
-
-                <!-- This does not actually appear to be the case in implementations today.
-                <p>If <var>data</var> doesn't match the
-                  <code><a title="NT-CData" data-spec="XML10" class="externalDFN">CDSect</a></code> production, throw a
-                  <code><a title="domexception" data-spec="DOM4" class="externalDFN">DOMException</a></code>
-                  with name <code>InvalidStateError</code> and terminate the entire algorithm.
-                -->
-                
+                    <var>node</var>'s <code><a title="dom-characterdata-data" data-spec="DOM4" 
+                    class="externalDFN">data</a></code>, and "<code>]]></code>".
                 <li>Return <var>markup</var>.
               </ol>
 
@@ -561,24 +582,18 @@
             
             <dt><code><a title="processinginstruction" data-spec="DOM4" class="externalDFN">ProcessingInstruction</a></code>
             <dd><ol>
-                <li>Let <var>markup</var> be the concatenation of "<code>&lt;?</code>", 
-                <var>node</var>'s
-                <code><a title="dom-characterdata-data" data-spec="DOM4" class="externalDFN">data</a></code>, 
-                and "<code>?></code>".
-
-                <!-- This does not actually appear to be the case in implementations today.
-                <p>If <var>data</var> doesn't match the
-                  <code><a title="NT-PI" data-spec="XML10" class="externalDFN">PI</a></code> production, throw a
-                  <code><a title="domexception" data-spec="DOM4" class="externalDFN">DOMException</a></code>
-                  with name <code>InvalidStateError</code> and terminate the entire algorithm.
-                -->
-                
+                <li>Let <var>markup</var> be the concatenation of the following, in order:
+                <ol>
+                    <li>"<code>&lt;?</code>" (U+003C LESS-THAN SIGN, U+003F QUESTION MARK);
+                    <li>The value of <var>node</var>'s <code><a title="dom-characterdata-target" 
+                        data-spec="DOM4" class="externalDFN">target</a></code>;
+                    <li>"<code> </code>" (U+0020 SPACE);
+                    <li>The value of <var>node</var>'s <code><a title="dom-characterdata-data" 
+                        data-spec="DOM4" class="externalDFN">data</a></code>;
+                    <li>"<code>?></code>" (U+003F QUESTION MARK, U+003E GREATER-THAN SIGN).
+                </ol>
                 <li>Return <var>markup</var>.
               </ol>
-            <p class=note>ProcessingInstruction objects may be created by the historical 
-            <code>document.createProcessingInstruction</code> API, or as a result of parsing an
-            <a title="xml-document" data-spec="DOM4" class="externalDFN">XML document</a>.
-         
         </dl>
 
         <p>To <dfn title="concept-serialize-doctype">produce a DocumentType serialization</dfn> of a
@@ -654,45 +669,53 @@
             <li>Append "<code>&gt;</code>" (U+003E GREATER-THAN SIGN) to <var>markup</var>.
         </ol>
 
-        <p>To <dfn title="concept-find-defined-prefixes">find the defined namespace prefixes</dfn> for an 
+        <p>To <dfn title="concept-record-namespace-info">record the namespace information</dfn> for an 
             <a title="element" data-spec="DOM4" class="externalDFN">Element</a> <var>element</var>, given a 
-            <a title="concept-namespace-prefix-map">namespace prefix map</a> <var>map</var>, a list of strings
-            <var>element prefixes list</var> (initially empty), and a boolean <var>default namespace found</var>
-            flag, the user agent must run the following steps:
+            <a title="concept-namespace-prefix-map">namespace prefix map</a> <var>map</var> and an
+            <var>element prefixes list</var> (initially empty), the user agent must run the following steps:
         <ol>
-            <li>Let <var>default namespace found</var> have the value <code>false</code>.
+            <li>Let <var>default namespace</var> be an empty string.
             <li>For each <a title="concept-attribute" data-spec="DOM4" class="externalDFN">attribute</a>
                 <var>attr</var> in <var>element</var>'s
                 <a title="concept-element-attribute" data-spec="DOM4" class="externalDFN">attributes</a>,
                 in order:
             <ol>
-                <p class="note">The following conditional steps add namespace prefix declarations
+                <p class="note">The following conditional steps add namespace prefixes
                     into the <var>element prefixes list</var> and add or replace them in the <var>map</var>. 
                     Only attributes in the <a title="xmlns-ns">XMLNS namespace</a> are
-                    collected (e.g., attributes made to look like namespace declarations via 
-                    <code><a title="dom-element-setattribute" data-spec="DOM4" class="externalDFN">setAttribute</a>(<em>"xmlns:name"</em>,
-                    <em>"value"</em>)</code> are not included).</p>
+                    considered (e.g., attributes made to look like namespace declarations via 
+                    <code><a title="dom-element-setattribute" data-spec="DOM4" 
+                    class="externalDFN">setAttribute</a>(<em>"xmlns:pretend-prefix"</em>,
+                    <em>"pretend-namespace"</em>)</code> are not included).</p>
                 <li>Let <var>attribute namespace</var> be the <var>attr</var>'s  
                     <a title="dom-attribute-namespaceuri" data-spec="DOM4" class="externalDFN">namespaceURI</a>
                     value.
                 <li>Let <var>attribute prefix</var> be the value of <var>attr</var>'s 
                         <a title="dom-attribute-prefix" data-spec="DOM4" class="externalDFN">prefix</a>.
-                <li>If the <var>attribute namespace</var> is the <a title="xmlns-ns">XMLNS namespace</a> and
-                    the <var>attribute prefix</var> is not <code>null</code>, then:
+                <li>If the <var>attribute namespace</var> is the <a title="xmlns-ns">XMLNS namespace</a>, then:
                 <ol>
-                    <li>Let <var>prefix definition</var> be the value of <var>attr</var>'s 
-                        <a title="dom-attribute-localname" data-spec="DOM4" class="externalDFN">localName</a>.
-                    <li>Let <var>namespace definition</var> be the value of <var>attr</var>'s 
-                        <a title="dom-attribute-value" data-spec="DOM4" class="externalDFN">value</a>.
-                    <li>If a key matching the value of <var>namespace definition</var> already exists in
-                        <var>map</var>, then update the key's value to be <var>prefix definition</var>.
-                    <li>Otherwise, append to <var>map</var> a new key <var>namespace definition</var>
-                        whose key value is the <var>prefix definition</var>.
-                    <li>Append the value of <var>prefix definition</var> to <var>element prefixes list</var>.
+                    <li>If <var>attribute prefix</var> is <code>null</code>, then <var>attr</var> is a 
+                        default namespace declaration. Set the value of <var>default namespace</var> to 
+                        <var>attr</var>'s <a title="dom-attribute-value" data-spec="DOM4"
+                        class="externalDFN">value</a> and stop running the these steps, returning to the 
+                        top of the loop to visit the next attribute.
+                    <li>Otherwise, the <var>attribute prefix</var> is not <code>null</code> and <var>attr</var>
+                        is a namespace prefix definition. Run the following steps:
+                    <ol>
+                        <li>Let <var>prefix definition</var> be the value of <var>attr</var>'s 
+                            <a title="dom-attribute-localname" data-spec="DOM4" class="externalDFN">localName</a>.
+                        <li>Let <var>namespace definition</var> be the value of <var>attr</var>'s 
+                            <a title="dom-attribute-value" data-spec="DOM4" class="externalDFN">value</a>.
+                        <li>If a key matching the value of <var>namespace definition</var> already exists in
+                            <var>map</var>, then update the key's value to be <var>prefix definition</var>.
+                        <li>Otherwise, no key matching the value of <var>namespace definition</var> exists;
+                            append to <var>map</var> a new key <var>namespace definition</var>
+                            whose key value is the <var>prefix definition</var>.
+                        <li>Append the value of <var>prefix definition</var> to <var>element prefixes list</var>.
+                    </ol>
                 </ol>
-                <li>Otherwise, this is a default namespace declaration. Set the value of <var>default namespace
-                    found</var> to be true.
             </ol>
+            <li>Return the value of <var>default namespace</var>.
         </ol>
         
         <p>To <dfn title="concept-generate-prefix">generate a prefix</dfn> given a 
@@ -710,57 +733,70 @@
         </ol>
         
         <p>The <dfn title="concept-serialize-xml-attributes">XML serialization of the attributes</dfn>
-        of an <a title="concept-element" data-spec="DOM4" class="externalDFN">Element</a>
-        <var>element</var> together with a <a title="concept-namespace-prefix-map">namespace prefix map</a>
-        <var>map</var> is the result of the following algorithm:
+            of an <a title="concept-element" data-spec="DOM4" class="externalDFN">Element</a>
+            <var>element</var> together with a <a title="concept-namespace-prefix-map">namespace prefix 
+            map</a> <var>map</var> and a <a title="concept-generated-prefix">generated prefix index</a> 
+            <var>prefix index</var> is the result of the following algorithm:
         <ol>
             <li>Let <var>result</var> be the empty string.
-            <li>For each
-                <a title="concept-attribute" data-spec="DOM4" class="externalDFN">attribute</a>
+            <li>For each <a title="concept-attribute" data-spec="DOM4" class="externalDFN">attribute</a>
                 <var>attr</var> in <var>element</var>'s
                 <a title="concept-element-attribute" data-spec="DOM4" class="externalDFN">attributes</a>,
                 in order:
+            <ol>
+                <li>Let <var>attribute namespace</var> be the <var>attr</var>'s  
+                    <a title="dom-attribute-namespaceuri" data-spec="DOM4" class="externalDFN">namespaceURI</a>
+                    value.
+                <!-- Check for an unregistered attribute namespace, and if so, serialize a definition for it -->
+                <li>Let <var>namespace prefix</var> be the empty string.
+                <li>If <var>attribute namespace</var> is not <code>null</code> and there is no 
+                    key matching <var>attribute namespace</var> in <var>map</var>, then:
                 <ol>
-                    <li>Let <var>attribute namespace</var> be the <var>attr</var>'s  
-                        <a title="dom-attribute-namespaceuri" data-spec="DOM4" class="externalDFN">namespaceURI</a>
-                        value.
-                    <li>Append the following strings to <var>result</var>:
-                        <ol>
-                            <li>"<code> </code>" (U+0020 SPACE);
-                            <li><var>attr</var>'s
-                            <a title="concept-attribute-name" data-spec="DOM4" class="externalDFN">name</a>;
-                            <!--<p class="issue">escaping / throwing  NO issues here to my knowledge -->
-                            <li>"<code>="</code>" (U+003D EQUALS SIGN, U+0022 QUOTATION MARK);
-                            <li><var>attr</var>'s
-                            <a title="concept-attribute-value" data-spec="DOM4" class="externalDFN">value</a>,
-                            replacing any occurrences of the following:
-                            <ol>
-                                <li>"<code>&quot;</code>" with "<code>&amp;quot;</code>"
-                                <li>"<code>&amp;</code>" with "<code>&amp;amp;</code>"
-                                <li>"<code>&lt;</code>" with "<code>&amp;lt;</code>"
-                                <li>"<code>&gt;</code>" with "<code>&amp;gt;</code>"
-                            </ol>
-                            <p class=note>This matches behavior present in browsers, and goes above
-                            and beyond the grammar requirement in the XML specification's AttValue
-                            production [[XML10]] by also replacing "<code>&gt;</code>" characters.</p>
-                            <li>"<code>"</code>" (U+0022 QUOTATION MARK).
-                        </ol>
-                    <p class="note">The following conditional steps add any namespace prefix declarations
-                        into the <var>prefixes</var> list. Only attributes in the XMLNS namespace are
-                        collected (e.g., attributes made to look like namespace declarations via 
-                        <code><a title="dom-element-setattribute" data-spec="DOM4" class="externalDFN">setAttribute</a>(<em>"xmlns:name"</em>,
-                        <em>"value"</em>)</code> are not included).
-                    <li>If the value of <var>attribute namespace</var> is equal to 
-                        <code>http://www.w3.org/2000/xmlns/</code> (the XMLNS namespace), then:
-                        <ol>
-                            <li>Let <var>prefix definition</var> be the value of <var>attr</var>'s 
-                                <a title="dom-attribute-localname" data-spec="DOM4" class="externalDFN">localName</a>.
-                            <li>Add the value of <var>prefix definition</var> to <var>prefixes</var>. 
-                                Since this namespace prefix definition has been serialized, it is
-                                unnecessary to serialize it again if subsequently encountered in 
-                                <var>element</var>'s children.
-                        </ol>
+                    <li>Let <var>namespace prefix</var> be the result of 
+                        <a title="concept-generate-prefix">generating a prefix</a> providing <var>map</var>, 
+                        <var>attribute namespace</var>, and <var>prefix index</var> as input.
+                    <li>Append the following to <var>result</var>:
+                    <ol>
+                        <li>"<code> </code>" (U+0020 SPACE);
+                        <li>The string "<code>xmlns:</code>";
+                        <li>The value of <var>namespace prefix</var>;
+                        <li>"<code>="</code>" (U+003D EQUALS SIGN, U+0022 QUOTATION MARK);
+                        <li>The value of <var>attribute namespace</var>;
+                        <li>"<code>"</code>" (U+0022 QUOTATION MARK).
+                    </ol>
                 </ol>
+                <li>Otherwise, if <var>attribute namespace</var> is not <code>null</code> and there exists 
+                    a key in <var>map</var> that matches the value of <var>attribute namespace</var>, then
+                    let <var>namespace prefix</var> be that key's value from the <var>map</var>.
+                <!-- For an attribute with a namespace defined, it's either defined elsewhere by another
+                    attribute (last step), or I need to generate a new prefix (second-to-last step). 
+                    The above two cases handle this, replacing the built-in prefix with an alternate prefix
+                    if needed (see below). -->
+                <li>Append a "<code> </code>" (U+0020 SPACE) to <var>result</var>.
+                <li>If <var>namespace prefix</var> is not the empty string, then append to <var>result</var>
+                    the concatenation of <var>namespace prefix</var> with "<code>:</code>" (U+003A COLON).
+                <!-- Now I've handled the two cases where an attribute has a namespace. After this, the 
+                    attribute does not have a namespace.... -->
+                <li>Append the following strings to <var>result</var>:
+                <ol>
+                    <li>The value of <var>attr</var>'s
+                        <a title="concept-attribute-localname" data-spec="DOM4" class="externalDFN">localName</a>;
+                    <li>"<code>="</code>" (U+003D EQUALS SIGN, U+0022 QUOTATION MARK);
+                    <li>The value of <var>attr</var>'s
+                        <a title="concept-attribute-value" data-spec="DOM4" class="externalDFN">value</a>,
+                        replacing any occurrences of the following:
+                    <ol>
+                        <li>"<code>&quot;</code>" with "<code>&amp;quot;</code>"
+                        <li>"<code>&amp;</code>" with "<code>&amp;amp;</code>"
+                        <li>"<code>&lt;</code>" with "<code>&amp;lt;</code>"
+                        <li>"<code>&gt;</code>" with "<code>&amp;gt;</code>"
+                    </ol>
+                    <p class=note>This matches behavior present in browsers, and goes above
+                    and beyond the grammar requirement in the XML specification's AttValue
+                    production [[XML10]] by also replacing "<code>&gt;</code>" characters.</p>
+                    <li>"<code>"</code>" (U+0022 QUOTATION MARK).
+                </ol>
+            </ol>
             <li>Return <var>result</var>.
         </ol>
     </section>