-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.html
1503 lines (1148 loc) · 74.2 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html>
<head><meta charset=UTF-8>
<title>DOM Parsing and Serialization</title>
<style>
/* Switch statement */
dl.switch dt::before {
content: "↪ ";
margin-left: 1em;
}
/* Better spacing around various lists (implied paragraph children) */
ol > li, section:not(#toc) ul > li, section dl > dt {
margin: 1em 0;
}
var { color: maroon; }
/* domintro styling */
dl.domintro {
background-color: rgb(221, 255, 221);
padding: 1em 0.5em 1em 2em;
clear: both;
}
dl.domintro dt {
color: black;
}
dl.domintro > dd {
color: green;
}
dl.domintro::before {
float: right;
background-color: white;
display: block;
border: 2px solid black;
color: green;
margin-top: -20px;
padding: 2px;
content: "This box is non-normative. Implementation requirements are given below this box.";
}
</style>
<script src="https://www.w3.org/Tools/respec/respec-w3c" class="remove" defer></script>
<script type="text/javascript" src="respecConfig.js" class='remove'></script>
</head>
<body>
<section id="abstract">
This specification defines APIs for the parsing and serializing of HTML and XML-based DOM nodes
for web applications.
</section>
<section id="sotd"></section>
<section id="crec" class="introductory">
<h2>Candidate Recommendation Exit Criteria</h2>
This specification will not advance to Proposed Recommendation before the spec's
<a href="http://w3c-test.org/domparsing/">test suite</a> is completed and two or more independent
implementations pass each test, although no single implementation must pass each test. We expect
to meet this criteria no sooner than 24 October 2014. The group will also create an
<a href="https://dvcs.w3.org/hg/innerhtml/raw-file/tip/implementationReport.html">Implementation
Report</a>.
</section>
<section id="conformance" class=introductory>
<p>The IDL fragments in this specification must be interpreted as required for conforming IDL
fragments, as described in the Web IDL specification. [[!WEBIDL]]
<p>Requirements phrased in the imperative as part of algorithms (such as "strip any leading space
characters" or "return false and terminate these steps") are to be interpreted with the meaning of
the key word ("must", "should", "may", etc) used in introducing the algorithm.
<p>Conformance requirements phrased as algorithms or specific steps may be implemented in any
manner, so long as the end result is equivalent. (In particular, the algorithms defined in this
specification are intended to be easy to follow, and not intended to be performant.)
<p>User agents may impose implementation-specific limits on otherwise unconstrained inputs, e.g.
to prevent denial of service attacks, to guard against running out of memory, or to work around
platform-specific limitations.
<p>When a method or an attribute is said to call another method or attribute, the user agent must
invoke its internal API for that attribute or method so that e.g. the author can't change the
behavior by overriding attributes or methods with custom properties or functions in ECMAScript.
[[ECMA-262]]
<p>Unless otherwise stated, string comparisons are done in a <a>case-sensitive</a> manner.
<p>If an algorithm calls into another algorithm, any exception that is thrown by the latter
(unless it is explicitly caught), must cause the former to terminate, and the exception to be
propagated up to its caller.
</section>
<section class=introductory>
<h2>Extensibility</h2>
<p>Vendor-specific proprietary extensions to this specification are strongly discouraged. Authors
must not use such extensions, as doing so reduces interoperability and fragments the user base,
allowing only users of specific user agents to access the content in question.
<p>If vendor-specific extensions are needed, the members should be prefixed by vendor-specific
strings to prevent clashes with future versions of this specification. Extensions must be defined
so that the use of extensions neither contradicts nor causes the non-conformance of functionality
defined in the specification.
<p>When vendor-neutral extensions to this specification are needed, either this specification can
be updated accordingly, or an extension specification can be written that overrides the
requirements in this specification. Such an extension specification becomes an
<dfn>applicable specification</dfn> for the purposes of conformance requirements in this
specification.
<!-- http://www.w3.org/mid/[email protected] -->
</section>
<section><h2>Introduction</h2>
<p>A document object model (DOM) is an in-memory representation of various types of <a>Node</a>s
where each <a>Node</a> is connected in a tree. The [[HTML5]] and [[DOM4]] specifications describe
DOM and its <a>Node</a>s is greater detail.
<p><dfn>Parsing</dfn> is the term used for converting a string representation of a DOM into an
actual DOM, and <dfn>Serializing</dfn> is the term used to transform a DOM back into a string.
This specification concerns itself with defining various APIs for both parsing and serializing a
DOM.
<div class=example>For example: the <a data-link-for="Element">innerHTML</a> API is a common way to both
parse and serialize a DOM (it does both). If a particular <a>Node</a>, has the following in-memory
DOM:
<pre>
HTMLDivElement (nodeName: "div")
┃
┣━ HTMLSpanElement (nodeName: "span")
┃ ┃
┃ ┗━ Text (data: "some ")
┃
┗━ HTMLElement (nodeName: "em")
┃
┗━ Text (data: "text!")
</pre>
And the <code>HTMLDivElement</code> node is stored in a variable <code><var>myDiv</var></code>,
then to serialize <code><var>myDiv</var></code>'s children simply <em>get</em> (read) the
<a>Element</a>'s <a data-link-for="Element">innerHTML</a> property (this triggers the serialization):
<pre class=javascript>
var serializedChildren = myDiv.innerHTML;
// serializedChildren has the value:
// "<span>some </span><em>text!</em>"
</pre>
<p>To parse new children for <code><var>myDiv</var></code> from a string (replacing its existing
children), simply <em>set</em> the <a data-link-for="Element">innerHTML</a> property (this triggers
parsing of the assigned string):
<pre class=javascript>
myDiv.innerHTML = "<span>new</span><em>children!</em>";
</pre>
</div>
<p>This specification describes two flavors of <a>parsing</a> and <a>serializing</a>: HTML and
XML (with XHTML being a type of XML). Each follows the rules of its respective markup language.
The above example shows HTML parsing and serialization. The specific algorithms for HTML parsing
and serializing are defined in the [[HTML5]] specification. This specification contains the
algorithm for XML serializing. The grammar for XML parsing is described in the [[XML10]]
specification.
<p><dfn>Round-tripping</dfn> a DOM means to serialize and then immediately parse the serialized
string back into a DOM. Ideally, this process does not result in any data loss with respect to the
identity and attributes of the <a>Node</a> in the DOM.
<a>Round-tripping</a> is especially tricky for an XML serialization, which must be concerned with
preserving the <a>Node</a>'s namespace identity in the serialization (wereas namespaces are
ignored in HTML).
<div class=example>Consider the XML serialization of the following in-memory DOM:
<pre>
Element (nodeName: "root")
┃
┗━ HTMLScriptElement (nodeName: "script")
┃
┗━ Text (data: "alert('hello world')")
</pre>
An XML serialization must include the <code>HTMLScriptElement</code> <a>Node</a>'s
<a data-lt="concept namespace">namespace</a> in order to preserve the identity of the
<code>script</code> element, and to allow the serialized string to
<a data-lt="round-tripping">round-trip</a> through an XML parser. Assuming that <code>root</code>
is in a variable named <code><var>root</var></code>:
<pre class=javascript>
var xmlSerialization = new XMLSerializer().serializeToString(root);
// xmlSerialization has the value:
// "<root><script xmlns="http://www.w3.org/1999/xhtml">alert('hello world')</script></root>"
</pre>
</div>
<p>The term <dfn>context object</dfn> means the object on which the API being discussed was
called.
<p>The following terms are understood to represent their respective namespaces in this
specification (and makes it easier to read):
<ul>
<li>The <dfn>HTML namespace</dfn> is <code>http://www.w3.org/1999/xhtml</code>
<li>The <dfn>XML namespace</dfn> is <code>http://www.w3.org/XML/1998/namespace</code>
<li>The <dfn>XMLNS namespace</dfn> is <code>http://www.w3.org/2000/xmlns/</code>
</ul>
</section><!-- end introduction -->
<section><h1>APIs for parsing and serializing DOM</h1>
<section><h2>The <code>DOMParser</code> interface</h2>
<span id="dom-domparser"></span>
<span id="dom-domparser-parsefromstring"></span>
<span id="idl-def-supportedtype"></span>
<span id="dom-supportedtype-text/html"></span>
<span id="dom-supportedtype-text/xml"></span>
<span id="dom-supportedtype-application/xml"></span>
<span id="dom-supportedtype-application/xhtml+xml"></span>
<span id="dom-supportedtype-image/svg+xml"></span>
<p>The definition of <code>DOMParser</code> has moved to <a href="https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#domparser">the HTML Standard</a>.</p>
</section><!-- end DOMParser interface -->
<section><h2>The <code>XMLSerializer</code> interface</h2>
<pre class="idl">
[Exposed=Window]
interface XMLSerializer {
constructor();
DOMString serializeToString(Node root);
};
</pre>
<dl class=domintro>
<dt><var>xmlserializer</var> = new <a data-link-for=XMLSerializer>XMLSerializer</a> ()</var>
<dd>Constructs a new XMLSerializer object.
<dt><var>string</var> = <var>xmlserializer</var> . <a data-link-for=XMLSerializer>serializeToString</a> ( <var>root</var> )
<dd>Serializes <var>root</var> into a string using an XML serialization. Throws a
<a>TypeError</a> exception if <var>root</var> is not a <a>Node</a> or an <a>Attr</a> object.
</dl>
<p>The <dfn><code>XMLSerializer</code></dfn>() constructor must return a new <a>XMLSerializer</a>
object.
<p>The <dfn data-dfn-for="XMLSerializer"><code>serializeToString</code></dfn>(<var>root</var>) method must
produce an <a>XML serialization</a> of <var>root</var> passing a value of <code>false</code> for
the <a><var>require well-formed</var></a> parameter, and return the result.
</section><!-- end XMLSerializer interface -->
<section><h2>The <code>InnerHTML</code> mixin</h2>
<span id="dom-innerhtml"></span>
<p>The definition of <code>InnerHTML</code> has moved to <a href="https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#the-innerhtml-property">the HTML Standard</a>.</p>
</section><!-- end InnerHTML mixin -->
<section><h2>Extensions to the <code><a>Element</a></code> interface</h2>
<!-- outerHTML -->
<span id="dom-element-outerhtml"></span>
<p>The definition of <code>outerHTML</code> has moved to <a href="https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#the-outerhtml-property">the HTML Standard</a>.</p>
<!-- insertAdjacentHTML -->
<span id="dom-element-insertadjacenthtml"></span>
<p>The definition of <code>insertAdjacentHTML</code> has moved to <a href="https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#the-insertadjacenthtml()-method">the HTML Standard</a>.</p>
</section><!-- end Extensions to the Element interface -->
<section><h2>Extensions to the <code><a>Range</a></code> interface</h2>
<span id="dom-range-createcontextualfragment"></span>
<p>The definition of <code>createContextualFragment</code> has moved to <a href="https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#the-createcontextualfragment()-method">the HTML Standard</a>.</p>
</section><!-- end Extensions to the Range interface -->
<!-- Dropping this extention as it is not implemented, nor does it appear that any browser
is currently interested in supporting it. Perhaps it can come back in a V2 of this spec
if browsers become interested.
<section><h2>Extensions to the <code><a>Text</a></code> interface</h2>
<pre class="idl">
partial interface Text {
attribute boolean serializeAsCDATA;
};
</pre>
<dl class=domintro>
<dt><var>text</var> . <a data-link-for="Text">serializeAsCDATA</a> [ = <var>value</var> ]
<dd>Controls whether, in XML, this node is serialized as a CDATA section.
</dl>
<p><code><a>Text</a></code> nodes have an additional associated flag, the
<dfn>serialize as CDATA flag</dfn>.
<p>The <dfn data-dfn-for="Text"><code>serializeAsCDATA</code></dfn> attribute must return true if the
<a>context object</a> has its <a>serialize as CDATA flag</a> set, or false otherwise.
<p>Setting the <a data-link-for="Text">serializeAsCDATA</a> attribute must, if the new value is true, set
the <a>context object</a>'s <a>serialize as CDATA flag</a>, or unset it otherwise.
</section>--><!-- end Extensions to the Text interface -->
</section><!-- end APIs for DOM Parsing and Serializing -->
<section><h1>Algorithms for parsing and serializing</h1>
<section><h2>Parsing</h2>
<span id="dfn-fragment-parsing-algorithm"></span>
<p>The definition of <code>fragment parsing algorithm</code> has moved to <a href="https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#fragment-parsing-algorithm-steps">the HTML Standard</a>.</p>
</section><!-- end Parsing -->
<section><h2>Serializing</h2>
<span id="dfn-fragment-serializing-algorithm"></span>
<p>The definition of <code>fragment serializing algorithm</code> has moved to <a href="https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#fragment-serializing-algorithm-steps">the HTML Standard</a>.</p>
<section><h2>XML Serialization</h2>
<p>An <a>XML serialization</a> differs from an HTML serialization in the following ways:
<ul>
<li><a>Elements</a> and <a data-lt="attribute">attributes</a> will always be serialized such
that their <code>namespaceURI</code> is preserved. In some cases this means that an existing
<code>prefix</code>, prefix declaration attribute or default namespace declaration attribute
might be dropped, substituted or changed. An HTML serialization does not attempt to
preserve the <code>namespaceURI</code>.
<li><a>Elements</a> not in the <a>HTML namespace</a> containing no <a>children</a>, are
serialized using the <a>empty-element tag</a> syntax (i.e., according to the XML
<a>EmptyElemTag</a> production).
</ul>
<p>Otherwise, the algorithm for producing an <a>XML serialization</a> is designed to produce a
serialization that is compatible with the <a>HTML parser</a>. For example, elements in the
<a>HTML namespace</a> that contain no <a>child nodes</a> are serialized with an explicit begin and end
tag rather than using the <a>empty-element tag</a> syntax.
<p class=note>Per [[DOM4]], <code><a>Attr</a></code> objects do not inherit from <a>Node</a>, and
thus cannot be serialized by the <a>XML serialization algorithm</a>. An attempt to serialize an
<a>Attr</a> object will result in an empty string.
<p>To produce an <dfn>XML serialization</dfn> of a <code><a>Node</a></code> <var>node</var> given
a flag <a><dfn>require well-formed</dfn></a>, run the following steps:
<ol>
<li>Let <a><var>namespace</var></a> be a <a>context namespace</a> with value <code>null</code>.
The <dfn>context namespace</dfn> tracks the <a>XML serialization</a> algorithm's current default
namespace. The <a>context namespace</a> is changed when either an <a>Element</a> <a>Node</a> has
a default namespace declaration, or the algorithm generates a default namespace declaration for
the <a>Element</a> <a>Node</a> to match its own namespace. The algorithm assumes no namespace
(<code>null</code>) to start.
<li>Let <a><var>prefix map</var></a> be a new <a>namespace prefix map</a>.
<li><a>Add</a> the <a>XML namespace</a> with prefix value "<code>xml</code>" to
<a><var>prefix map</var></a>.
<li>Let <a><var>prefix index</var></a> be a <a>generated namespace prefix index</a> with value
<code>1</code>. The <dfn>generated namespace prefix index</dfn> is used to generate a new unique
prefix value when no suitable existing namespace prefix is available to serialize a
<var>node</var>'s <code>namespaceURI</code> (or the <code>namespaceURI</code> of one of
<var>node</var>'s attributes). <span class=note>See the <a>generate a prefix</a> algorithm.
</span>
<li>Return the result of running the <a>XML serialization algorithm</a> on <var>node</var>
passing the <a>context namespace</a> <a><var>namespace</var></a>, <a>namespace prefix map</a>
<a><var>prefix map</var></a>, <a>generated namespace prefix index</a> reference to
<a><var>prefix index</var></a>, and the flag <a><var>require well-formed</var></a>. If an
<dfn data-lt="throw an exception">exception</dfn> occurs during the execution of the algorithm,
then catch that exception and throw an "<code><a>InvalidStateError</a></code>"
<code><a>DOMException</a></code>.
</ol>
<p>Each of the following algorithms for <dfn>producing an XML serialization of a DOM node</dfn>
take as input a <var>node</var> to serialize and the following arguments:
<ul>
<li>A <a>context namespace</a> <dfn><var>namespace</var></dfn>
<li>A <a>namespace prefix map</a> <dfn><var>prefix map</var></dfn>
<li>A <a>generated namespace prefix index</a> <dfn><var>prefix index</var></dfn>
<li>The <a><var>require well-formed</var></a> flag
</ul>
<p>The <dfn>XML serialization algorithm</dfn>
<a data-lt="producing an XML serialization of a DOM node">produces an XML serialization of an arbitrary DOM node</a>
<var>node</var> based on the <var>node</var>'s interface type. Each referenced algorithm is
to be passed the arguments as they were recieved by the caller and return their result to the
caller. Re-throw any exceptions. If <var>node</var>'s interface is:</p>
<dl class=switch>
<dt><code><a>Element</a></code>
<dd>Run the algorithm for <a>XML serializing an Element node</a> <var>node</var>.
<dt><code><a>Document</a></code>
<dd>Run the algorithm for <a>XML serializing a Document node</a> <var>node</var>.
<dt><code><a>Comment</a></code>
<dd>Run the algorithm for <a>XML serializing a Comment node</a> <var>node</var>.
<!--<dt><code><a>CDATASection</a></code>
<dd>Run the algorithm for <a>XML serializing a CDATASection node</a> <var>node</var>.-->
<dt><code><a>Text</a></code>
<dd>Run the algorithm for <a>XML serializing a Text node</a> <var>node</var>.
<dt><code><a>DocumentFragment</a></code>
<dd>Run the algorithm for <a>XML serializing a DocumentFragment node</a> <var>node</var>.
<dt><code><a>DocumentType</a></code>
<dd>Run the algorithm for <a>XML serializing a DocumentType node</a> <var>node</var>.
<dt><code><a>ProcessingInstruction</a></code>
<dd>Run the algorithm for <a>XML serializing a ProcessingInstruction node</a> <var>node</var>.
<dt>An <code><a>Attr</a></code> object
<dd>Return an empty string.
<dt>Anything else
<dd>Throw a <a>TypeError</a>. Only <a>Node</a>s and <a>Attr</a> objects can be serialized by
this algorithm.
</dl>
Each of the above referenced algorithms are detailed in the sections that follow.
<section><h2><dfn>XML serializing an Element node</dfn></h2>
The algorithm for <a>producing an XML serialization of a DOM node</a> of type <a>Element</a> is as
follows:
<ol>
<li>If the <a><var>require well-formed</var></a> flag is set (its value is <code>true</code>),
and this <var>node</var>'s <code><a>localName</a></code> attribute contains the character
"<code>:</code>" (U+003A COLON) or does not match the XML <a>Name</a> production, then
<a>throw an exception</a>; the serialization of this <var>node</var> would not be a well-formed
element.
<li>Let <var>markup</var> be the string "<code><</code>" (U+003C LESS-THAN SIGN).
<li>Let <dfn><var>qualified name</var></dfn> be an empty string.
<li>Let <dfn><var>skip end tag</var></dfn> be a boolean flag with value <code>false</code>.
<li>Let <dfn><var>ignore namespace definition attribute</var></dfn> be a boolean flag with value
<code>false</code>.
<li>Given <a><var>prefix map</var></a>, <a>copy a namespace prefix map</a> and let
<var>map</var> be the result.
<li>Let <dfn><var>local prefixes map</var></dfn> be an empty map. The map has unique <a>Node</a>
<code>prefix</code> strings as its keys, with corresponding <code>namespaceURI</code>
<a>Node</a> values as the map's key values (in this map, the <code>null</code> namespace is
represented by the empty string).
<p class=note>This map is local to each element. It is used to ensure there are no conflicting
prefixes should a new namespace <code>prefix</code> attribute need to be
<a data-lt="generate a prefix">generated</a>. It is also used to enable skipping of duplicate
prefix definitions when
<a data-lt="XML serialization of the attributes">writing an element's attributes</a>: the map
allows the algorithm to distinguish between a <code>prefix</code> in the
<a>namespace prefix map</a> that might be locally-defined (to the current <a>Element</a>) and
one that is not.</p>
<li>Let <dfn><var>local default namespace</var></dfn> be the result of
<a>recording the namespace information</a> for <var>node</var> given <var>map</var> and
<a><var>local prefixes map</var></a>.
<p class="note">The above step will update <var>map</var> with any found namespace prefix
definitions, add the found prefix definitions to the <a><var>local prefixes map</var></a> and
return a <a><var>local default namespace</var></a> value defined by a default namespace attribute if one
exists. Otherwise it returns <code>null</code>.</p>
<li>Let <dfn><var>inherited ns</var></dfn> be a copy of <a><var>namespace</var></a>.
<li>Let <var>ns</var> be the value of <var>node</var>'s
<code><a data-lt="element namespaceURI">namespaceURI</a></code> attribute.
<li>If <a><var>inherited ns</var></a> is equal to <var>ns</var>, then:
<ol>
<li>If <a><var>local default namespace</var></a> is not <code>null</code>, then set
<a><var>ignore namespace definition attribute</var></a> to <code>true</code>.
<li>If <var>ns</var> is the <a>XML namespace</a>, then append to
<a><var>qualified name</var></a> the concatenation of the string "<code>xml:</code>" and the
value of <var>node</var>'s <code><a>localName</a></code>.
<li>Otherwise, append to <a><var>qualified name</var></a> the value of <var>node</var>'s
<code><a>localName</a></code>. <span class=note>The <var>node</var>'s
<code><a data-lt="element prefix">prefix</a></code> if it exists, is dropped.</span>
<li>Append the value of <a><var>qualified name</var></a> to <var>markup</var>.
</ol>
<li>Otherwise, <a><var>inherited ns</var></a> is not equal to <var>ns</var> (the <var>node</var>'s
own namespace is different from the context namespace of its parent). Run these sub-steps:
<!-- The serialization algorithm must differentiate this node's namespace from it's parent's
default namespace. 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 handled
accordingly. -->
<ol>
<li>Let <var>prefix</var> be the value of <var>node</var>'s
<code><a data-lt="element prefix">prefix</a></code> attribute.
<li>Let <var>candidate prefix</var> be the result of
<a>retrieving a preferred prefix string</a> <var>prefix</var> from <var>map</var></a> given
namespace <var>ns</var>.
<p class=note>The above may return <code>null</code> if no namespace key <var>ns</var> exists
in <var>map</var>.</p>
<li>If the value of <var>prefix</var> matches "<code>xmlns</code>", then run the following
steps:
<ol>
<li>If the <a><var>require well-formed</var></a> flag is set, then throw an error. An
<a>Element</a> with <code><a data-lt="element prefix">prefix</a></code> "<code>xmlns</code>"
will not legally round-trip in a conforming <a>XML parser</a>.
<li>Let <var>candidate prefix</var> be the value of <var>prefix</var>.
</ol>
<!-- 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><dfn>Found a suitable namespace prefix</dfn>: if <var>candidate prefix</var> is not
<code>null</code> (a namespace prefix is defined which maps to <var>ns</var>), then:
<p class=note>The following may serialize a different
<code><a data-lt="element prefix">prefix</a></code> than the <a>Element</a>'s existing
<code><a data-lt="element prefix">prefix</a></code> if it already had one. However, the
<a>retrieving a preferred prefix string</a> algorithm already tried to match the existing
prefix if possible.</p>
<ol>
<li>Append to <a><var>qualified name</var></a> the concatenation of
<var>candidate prefix</var>, "<code>:</code>" (U+003A COLON), and <var>node</var>'s <code>
<a>localName</a></code>. <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 <a><var>local default namespace</var></a> is not <code>null</code> (there exists
a locally-defined default namespace declaration attribute) and its value is not the
<a>XML namespace</a>, then let <a><var>inherited ns</var></a> get the value of
<a><var>local default namespace</var></a> unless the
<a><var>local default namespace</var></a> is the empty string in which case let it get
<code>null</code> (the <a>context namespace</a> is changed to the declared default, rather
than this <var>node</var>'s own namespace).
<p class=note>Any default namespace definitions or namespace prefixes that define the
<a>XML namespace</a> are omitted when serializing this node's attributes.</p>
<li>Append the value of <a><var>qualified name</var></a> to <var>markup</var>.
</ol>
<li>Otherwise, if <var>prefix</var> is not <code>null</code>, then:
<p class=note>By this step, there is no namespace or prefix mapping declaration in this
<var>node</var> (or any parent <var>node</var> visited by this algorithm) that defines
<var>prefix</var> otherwise the step labelled <a>Found a suitable namespace prefix</a> would
have been followed. The sub-steps that follow will create a new namespace prefix declaration
for <var>prefix</var> and ensure that <var>prefix</var> does not conflict with an existing
namespace prefix declaration of the same <code>localName</code> in <var>node</var>'s
<a>attribute list</a>.</p>
<ol>
<li>If the <a><var>local prefixes map</var></a> contains a key matching <var>prefix</var>,
then let <var>prefix</var> be the result of <a>generating a prefix</a> providing as input
<var>map</var>, <var>ns</var>, and <a>prefix index</a>.
<li><a>Add</a> <var>prefix</var> to <var>map</var> given namespace <var>ns</var>.
<li>Append to <a><var>qualified name</var></a> the concatenation of <var>prefix</var>,
"<code>:</code>" (U+003A COLON), and <var>node</var>'s <code><a>localName</a></code>.
<li>Append the value of <a><var>qualified name</var></a> to <var>markup</var>.
<li>Append the following to <var>markup</var>, in the order listed:
<p class=note>The following serializes a namespace prefix declaration for <var>prefix</var>
which was just added to the <var>map</var>.</p>
<ol>
<li>"<code> </code>" (U+0020 SPACE);
<li>The string "<code>xmlns:</code>";
<li>The value of <var>prefix</var>;
<li>"<code>="</code>" (U+003D EQUALS SIGN, U+0022 QUOTATION MARK);
<li>The result of <a>serializing an attribute value</a> given <var>ns</var> and the
<a><var>require well-formed</var></a> flag as input;
<li>"<code>"</code>" (U+0022 QUOTATION MARK).
<li>If <a><var>local default namespace</var></a> is not <code>null</code> (there exists a
locally-defined default namespace declaration attribute), then let
<a><var>inherited ns</var></a> get the value of <a><var>local default namespace</var></a>
unless the <a><var>local default namespace</var></a> is the empty string in which case let
it get <code>null</code>.
</ol>
</ol>
<li>Otherwise, if <a><var>local default namespace</var></a> is <code>null</code>, or
<a><var>local default namespace</var></a> is not <code>null</code> and its value is not equal
to <var>ns</var>, then:
<p class=note>At this point, the namespace for this node still needs to be serialized, but
there's no <code>prefix</code> (or <var>candidate prefix</var>) availble; the following uses
the default namespace declaration to define the namespace--optionally replacing an existing
default declaration if present.</p>
<ol>
<li>Set the <a><var>ignore namespace definition attribute</var></a> flag to
<code>true</code>.
<li>Append to <a><var>qualified name</var></a> the value of <var>node</var>'s
<code><a>localName</a></code>.
<li>Let the value of <a><var>inherited ns</var></a> be <var>ns</var>.
<p class=note>The new default namespace will be used in the serialization to define this
<var>node</var>'s namespace and act as the <a>context namespace</a> for its
<a>children</a>.</p>
<li>Append the value of <a><var>qualified name</var></a> to <var>markup</var>.
<li>Append the following to <var>markup</var>, in the order listed:
<p class=note>The following serializes the new (or replacement) default namespace
definition.</p>
<ol>
<li>"<code> </code>" (U+0020 SPACE);
<li>The string "<code>xmlns</code>";
<li>"<code>="</code>" (U+003D EQUALS SIGN, U+0022 QUOTATION MARK);
<li>The result of <a>serializing an attribute value</a> given <var>ns</var> and the
<a><var>require well-formed</var></a> flag as input;
<li>"<code>"</code>" (U+0022 QUOTATION MARK).
</ol>
</ol>
<!-- Finally, regardless of prefix, the node has a local default namespace that matches 'ns'.
So, we'll just use that and drop the prefix -->
<li>Otherwise, the <var>node</var> has a <a><var>local default namespace</var></a> that matches
<var>ns</var>. Append to <a><var>qualified name</var></a> the value of <var>node</var>'s
<code><a>localName</a></code>, let the value of <a><var>inherited ns</var></a> be <var>ns</var>,
and append the value of <a><var>qualified name</var></a> to <var>markup</var>.
<p class=note>All of the combinations where <var>ns</var> is not equal to
<a><var>inherited ns</var></a> are handled above such that <var>node</var> will be serialized
preserving its original <code><a data-lt="element namespaceURI">namespaceURI</a></code>.</p>
</ol>
<li>Append to <var>markup</var> the result of the
<a data-lt="XML serialization of the attributes">XML serialization of <var>node</var>'s attributes</a>
given <var>map</var>, <a><var>prefix index</var></a>, <a><var>local prefixes map</var></a>,
<a><var>ignore namespace definition attribute</var></a> flag, and
<a><var>require well-formed</var></a> flag.
<li>If <var>ns</var> is the <a>HTML namespace</a>, and the <var>node</var>'s list of
<a>children</a> is empty, and the <var>node</var>'s <code><a>localName</a></code> matches any
one of the following <a>void elements</a>:
"<code>area</code>",
"<code>base</code>",
"<code>basefont</code>",
"<code>bgsound</code>",
"<code>br</code>",
"<code>col</code>",
"<code>embed</code>",
"<code>frame</code>",
"<code>hr</code>",
"<code>img</code>",
"<code>input</code>",
"<code>keygen</code>",
"<code>link</code>",
"<code>menuitem</code>",
"<code>meta</code>",
"<code>param</code>",
"<code>source</code>",
"<code>track</code>",
"<code>wbr</code>";
then append the following to <var>markup</var>, in the order listed:
<ol>
<li>"<code> </code>" (U+0020 SPACE);
<li>"<code>/</code>" (U+002F SOLIDUS).
</ol>
and set the <a><var>skip end tag</var></a> flag to <code>true</code>.
<li>If <var>ns</var> is not the <a>HTML namespace</a>, and the <var>node</var>'s list of
<a>children</a> is empty, then append "<code>/</code>" (U+002F SOLIDUS) to <var>markup</var>
and set the <a><var>skip end tag</var></a> flag to <code>true</code>.
<li>Append "<code>></code>" (U+003E GREATER-THAN SIGN) to <var>markup</var>.
<li>If the value of <a><var>skip end tag</var></a> is <code>true</code>, then return the value of
<var>markup</var> and skip the remaining steps. The <var>node</var> is a leaf-node.
<li>If <var>ns</var> is the <a>HTML namespace</a>, and the <var>node</var>'s
<code><a>localName</a></code> matches the string "<code>template</code>", then this is a
<code><a>template</a></code> element. Append to <var>markup</var> the result of
<a>XML serializing a DocumentFragment node</a> given the <a>template</a> element's
<a>template contents</a> (a <code><a>DocumentFragment</a></code>), providing
<a><var>inherited ns</var></a>, <var>map</var>, <a><var>prefix index</var></a>, and the
<a><var>require well-formed</var></a> flag.
<p class=note>This allows <a>template content</a> to round-trip , given the rules for
<a>parsing XHTML documents</a>.</p>
<li>Otherwise, append to <var>markup</var> the result of running the
<a>XML serialization algorithm</a> on each of <var>node</var>'s <a>children</a>, in
<a>tree order</a>, providing <a><var>inherited ns</var></a>, <var>map</var>,
<a><var>prefix index</var></a>, and the <a><var>require well-formed</var></a> flag.
<li>Append the following to <var>markup</var>, in the order listed:
<ol>
<li>"<code></</code>" (U+003C LESS-THAN SIGN, U+002F SOLIDUS);
<li>The value of <a><var>qualified name</var></a>;
<li>"<code>></code>" (U+003E GREATER-THAN SIGN).
</ol>
<li>Return the value of <var>markup</var>.
</ol>
<section><h2>Recording the namespace</h2>
<p>This following algorithm will update the <a>namespace prefix map</a> with any found namespace
prefix definitions, add the found prefix definitions to the <a><var>local prefixes map</var></a>,
and return a <a><var>local default namespace</var></a> value defined by a default namespace attribute if one
exists. Otherwise it returns <code>null</code>.</p>
<p>When <dfn>recording the namespace information</dfn> for an <code><a>Element</a></code>
<var>element</var>, given a <a>namespace prefix map</a> <var>map</var> and a
<a><var>local prefixes map</var></a> (initially empty), the user agent must run the following
steps:
<ol>
<li>Let <var>default namespace attr value</var> be <code>null</code>.
<li><dfn>Main</dfn>: For each <a>attribute</a> <var>attr</var> in <var>element</var>'s
<code><a>attributes</a></code>, in the order they are specified in the <var>element</var>'s
<a>attribute list</a>:
<p class="note">The following conditional steps find namespace prefixes. Only attributes
in the <a>XMLNS namespace</a> are considered (e.g., attributes made to look like namespace
declarations via <code><a>setAttribute</a>(<em>"xmlns:pretend-prefix"</em>,
<em>"pretend-namespace"</em>)</code> are not included).</p>
<ol>
<li>Let <var>attribute namespace</var> be the value of <var>attr</var>'s
<code><a>namespaceURI</a></code> value.
<li>Let <var>attribute prefix</var> be the value of <var>attr</var>'s
<code><a>prefix</a></code>.
<li>If the <var>attribute namespace</var> is the <a>XMLNS namespace</a>, then:
<ol>
<li>If <var>attribute prefix</var> is <code>null</code>, then <var>attr</var> is a default
namespace declaration. Set the <var>default namespace attr value</var> to <var>attr</var>'s
<code><a>value</a></code> and stop running these steps, returning to <a>Main</a> 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
<code><a data-lt="attr localName">localName</a></code>.
<li>Let <var>namespace definition</var> be the value of <var>attr</var>'s
<code><a>value</a></code>.
<li>If <var>namespace definition</var> is the <a>XML namespace</a>, then stop running
these steps, and return to <a>Main</a> to visit the next attribute.
<p class=note><a>XML namespace</a> definitions in prefixes are completely ignored (in
order to avoid unnecessary work when there might be prefix conflicts).
<a>XML namespace</a>d elements are always handled uniformly by prefixing (and overriding
if necessary) the element's localname with the reserved "<code>xml</code>" prefix.</p>
<li>If <var>namespace definition</var> is the empty string (the declarative form of
having no namespace), then let <var>namespace definition</var> be <code>null</code>
instead.
<li>If <var>prefix definition</var> is <a>found</a> in <var>map</var> given the namespace
<var>namespace definition</var>, then stop running these steps, and return to <a>Main</a>
to visit the next attribute.
<p class=note>This step avoids adding duplicate prefix definitions for the same namespace
in the <var>map</var>. This has the side-effect of avoiding later serialization of
duplicate namespace prefix declarations in any descendant nodes.</p>
<li><a>Add</a> the prefix <var>prefix definition</var> to <var>map</var> given namespace
<var>namespace definition</var>.
<li>Add the value of <var>prefix definition</var> as a new key to the
<a><var>local prefixes map</var></a>, with the <var>namespace definition</var> as the
key's value replacing the value of <code>null</code> with the empty string if applicable.
</ol>
</ol>
</ol>
<li>Return the value of <var>default namespace attr value</var>.
<p class=note>The empty string is a legitimate return value and is not converted to
<code>null</code>.</p>
</ol>
</section><!-- Recording the namespace -->
<section><h2>The Namespace Prefix Map</h2>
<p>A <dfn>namespace prefix map</dfn> is a map that associates <code>namespaceURI</code> and
<dfn data-lt="list">namespace prefix lists</dfn>, where <code>namespaceURI</code> values are the
map's unique keys (which can include the <code>null</code> value representing no namespace), and
ordered lists of associated <code>prefix</code> values are the map's key values. The
<a>namespace prefix map</a> will be populated by previously seen namespaceURIs and all their
previously encountered prefix associations for a given node and its ancestors.
<p class=note><strong>Note:</strong> the last seen <code>prefix</code> for a given
<code>namespaceURI</code> is at the end of its respective <a>list</a>. The list is searched to
find potentially matching prefixes, and if no matches are found for the given
<code>namespaceURI</code>, then the last <code>prefix</code> in the list is used. See
<a>copy a namespace prefix map</a> and <a>retrieve a preferred prefix string</a> for additional
details.</p>
<p>To <dfn>copy a namespace prefix map</dfn> <var>map</var> means to copy the <var>map</var>'s
keys into a new empty <a>namespace prefix map</a>, and to copy each of the values in the
<a data-lt="list">namespace prefix list</a> associated with each keys' value into a new
<a>list</a> which should be associated with the respective key in the new map.
<p>To <dfn data-lt="retrieving a preferred prefix string">retrieve a preferred prefix string</dfn>
<var>preferred prefix</var> from the <a>namespace prefix map</a> <var>map</var> given a namespace
<var>ns</var>, the user agent should:
<ol>
<li>Let <var>candidates list</var> be the result of retrieving a <a>list</a> 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 stop running these steps, and return the <code>null</code> value.
<li>Otherwise, for each prefix value <var>prefix</var> in <var>candidates list</var>, iterating
from beginning to end:
<p class=note>There will always be at least one prefix value in the list.</p>
<ol>
<li>If <var>prefix</var> matches <var>preferred prefix</var>, then stop running these steps
and return <var>prefix</var>.
<li>If <var>prefix</var> is the last item in the <var>candidates list</var>, then stop running
these steps and return <var>prefix</var>.
</ol>
</ol>
<p>To check if a prefix string <var>prefix</var> is <dfn>found</dfn> in a
<a>namespace prefix map</a> <var>map</var> given a namespace <var>ns</var>, the user agent should:
<ol>
<li>Let <var>candidates list</var> be the result of retrieving a <a>list</a> 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 stop running these steps, and return <code>false</code>.
<li>If the value of <var>prefix</var> occurs at least once in <var>candidates list</var>, return
<code>true</code>, otherwise return <code>false</code>.
</ol>
<p>To <dfn>add</dfn> a prefix string <var>prefix</var> to the <a>namespace prefix map</a>
<var>map</var> given a namespace <var>ns</var>, the user agent should:
<ol>
<li>Let <var>candidates list</var> be the result of retrieving a <a>list</a> 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 let <var>candidates list</var> be <code>null</code>.
<li>If <var>candidates list</var> is <code>null</code>, then create a new <a>list</a> with
<var>prefix</var> as the only item in the <a>list</a>, and associate that <a>list</a> with a new
key <var>ns</var> in <var>map</var>.
<li>Otherwise, append <var>prefix</var> to the end of <var>candidates list</var>.
<p class=note>The steps in <a>retrieve a preferred prefix string</a> use the <a>list</a> to
track the most recently used (MRU) <var>prefix</var> associated with a given namespace, which
will be the <var>prefix</var> at the end of the list. This list may contain duplicates of the
same <var>prefix</var> value seen earlier (and that's OK).</p>
</ol>
</section><!-- The Namespace Prefix Map -->
<section><h2>Serializing an Element's attributes</h2>
<p>The <dfn>XML serialization of the attributes</dfn> of an <code><a>Element</a></code>
<var>element</var> together with a <a>namespace prefix map</a> <var>map</var>, a
<a>generated namespace prefix index</a> <var>prefix index</var> reference, a
<a>local prefixes map</a>, a <a><var>ignore namespace definition attribute</var></a> flag, and a
<a><var>require well-formed</var></a> flag, is the result of the following algorithm:
<ol>
<li>Let <var>result</var> be the empty string.
<li>Let <var>localname set</var> be a new empty <var>namespace localname set</var>. This
<var>localname set</var> will contain tuples of unique attribute
<code><a>namespaceURI</a></code> and <code><a>localName</a></code> pairs, and is populated as
each <var>attr</var> is processed. <span class="note">This set is used to [optionally] enforce
the well-formed constraint that an element cannot have two attributes with the same
<code><a>namespaceURI</a></code> and <code><a>localName</a></code>. This can occur when two
otherwise identical attributes on the same element differ only by their prefix values.</span>
<li><dfn>Loop</dfn>: For each <a>attribute</a> <var>attr</var> in <var>element</var>'s
<code><a>attributes</a></code>, in the order they are specified in the <var>element</var>'s
<a>attribute list</a>:
<ol>
<li>If the <a><var>require well-formed</var></a> flag is set (its value is <code>true</code>), and the
<var>localname set</var> contains a tuple whose values match those of a new tuple consisting
of <var>attr</var>'s <code><a>namespaceURI</a></code> attribute and
<code><a>localName</a></code> attribute, then <a>throw an exception</a>; the serialization of
this <var>attr</var> would fail to produce a well-formed element serialization.
<li>Create a new tuple consisting of <var>attr</var>'s <code><a>namespaceURI</a></code>
attribute and <code><a>localName</a></code> attribute, and add it to the
<var>localname set</var>.
<li>Let <var>attribute namespace</var> be the value of <var>attr</var>'s
<code><a>namespaceURI</a></code> value.
<!-- Check for an unregistered attribute namespace, and if so, serialize a definition for it -->
<li>Let <var>candidate prefix</var> be <code>null</code>.
<li>If <var>attribute namespace</var> is not <code>null</code>, then run these sub-steps:
<ol>
<li>Let <var>candidate prefix</var> be the result of
<a>retrieving a preferred prefix string</a> from <var>map</var> given namespace
<var>attribute namespace</var> with preferred prefix being <var>attr</var>'s
<code><a>prefix</a></code> value.
<li>If the value of <var>attribute namespace</var> is the <a>XMLNS namespace</a>, then run
these steps:
<ol>
<li>If any of the following are true, then stop running these steps and goto <a>Loop</a>
to visit the next attribute:
<ul>
<li>the <var>attr</var>'s <code><a>value</a></code> is the <a>XML namespace</a>;
<p class=note>The <a>XML namespace</a> cannot be redeclared and survive
<a>round-tripping</a> (unless it defines the prefix "<code>xml</code>"). To avoid this
problem, this algorithm always prefixes elements in the <a>XML namespace</a> with
"<code>xml</code>" and drops any related definitions as seen in the above condition.</p>
<li>the <var>attr</var>'s <code><a>prefix</a></code> is <code>null</code> and the
<a><var>ignore namespace definition attribute</var></a> flag is <code>true</code> (the
<a>Element</a>'s default namespace attribute should be skipped);
<li>the <var>attr</var>'s <code><a>prefix</a></code> is not <code>null</code> and either
<ul>
<li>the <var>attr</var>'s <code><a data-lt="attr localName">localName</a></code> is
not a key contained in the <a>local prefixes map</a>, or
<li>the <var>attr</var>'s <code><a data-lt="attr localName">localName</a></code> is
present in the <a>local prefixes map</a> but the value of the key does not match
<var>attr</var>'s <code><a>value</a></code>
</ul>
and furthermore that the <var>attr</var>'s
<code><a data-lt="attr localName">localName</a></code> (as the <var>prefix</var> to
find) is <a>found</a> in the <a>namespace prefix map</a> given the namespace consisting
of the <var>attr</var>'s <code><a>value</a></code> (the current namespace prefix
definition was exactly defined previously--on an ancestor element not the current
element whose attributes are being processed).
</ul>
<li>If the <a><var>require well-formed</var></a> flag is set (its value is <code>true</code>), and
the value of <var>attr</var>'s <code><a>value</a></code> attribute matches the
<a>XMLNS namespace</a>, then throw an exception; the serialization of this attribute would
produce invalid XML because the <a>XMLNS namespace</a> is reserved and cannot be applied
as an element's namespace via XML parsing.
<p class=note>DOM APIs do allow creation of elements in the <a>XMLNS namespace</a> but
with strict qualifications.</p>
<li>If the <a><var>require well-formed</var></a> flag is set (its value is <code>true</code>), and
the value of <var>attr</var>'s <code><a>value</a></code> attribute is the empty string,
then throw an exception; namespace prefix declarations cannot be used to undeclare a
namespace (use a default namespace declaration instead).
<li>the <var>attr</var>'s <code><a>prefix</a></code> matches the string
"<code>xmlns</code>", then let <var>candidate prefix</var> be the string
"<code>xmlns</code>".
</ol>
<li>Otherwise, the <var>attribute namespace</var> in not the <a>XMLNS namespace</a>. Run
these steps: