Note that there are some explanatory texts on larger screens.

plurals
  1. POChrome 22 outputs invalid XML when attributes have an xlink namespace
    primarykey
    data
    text
    <p>I have the following minimal JavaScript fragment:</p> <pre class="lang-js prettyprint-override"><code>var xml = '&lt;El a:title="T" a:href="H" xmlns:a="http://www.w3.org/1999/xlink" /&gt;'; var dom = new DOMParser().parseFromString(xml, 'text/xml'); xml = new XMLSerializer().serializeToString(dom); </code></pre> <p>When I execute the code in most browsers (just paste it into your browser's JavaScript console), the parsed-then-serialized XML is equivalent to the original. For example on Chrome 8 I get:</p> <pre class="lang-xml prettyprint-override"><code>&lt;El xmlns:a="http://www.w3.org/1999/xlink" a:title="T" a:href="H"/&gt; </code></pre> <p>However on Chrome 22 the same code fragment changes the XML to:</p> <pre class="lang-xml prettyprint-override"><code>&lt;El xmlns:a="http://www.w3.org/1999/xlink" xlink:title="T" xlink:href="H"/&gt; </code></pre> <p>Note that the namespace prefix <code>xlink</code> used by the title and href attributes is not defined anywhere, so the XML is now invalid. As you probably can imagine this causes all kinds of problems for code that tries to subsequently use the XML.</p> <p>Is this a bug in the XMLSerializer or am I missing some intricacies about how the DOM should be serialized? </p> <p>Also did anyone find a workaround that I can put in code, as opposed to make the XML match the apparent preference to use <code>xlink</code> as the prefix for the XLink namespace?</p> <h1>Update</h1> <p>I did some additional testing and the problem seems to be caused by the fact that the XMLSerializer recognizes the XLink namespace and insists on outputting an <code>xlink</code> prefix for it, without properly registering that prefix.</p> <p>So this fragment work fine:</p> <pre class="lang-js prettyprint-override"><code>var xml = '&lt;El a:title="T" a:href="H" xmlns:a="any-other-namespace-uri" /&gt;'; var dom = new DOMParser().parseFromString(xml, 'text/xml'); xml = new XMLSerializer().serializeToString(dom); </code></pre> <p>So here I changed the Namespace URL to something less well-known and the output is now valid:</p> <pre class="lang-xml prettyprint-override"><code>&lt;El xmlns:a="any-other-namespace-uri" a:title="T" a:href="H"/&gt; </code></pre> <p>The following fragment also works fine: </p> <pre class="lang-js prettyprint-override"><code>var xml = '&lt;El a:title="T" a:href="H" xmlns:a="http://www.w3.org/2000/xlink" /&gt;'; var dom = new DOMParser().parseFromString(xml, 'text/xml'); xml = new XMLSerializer().serializeToString(dom); </code></pre> <p>So in this case we use the "expected" prefix for the XLink namespace and it then serializes without problems:</p> <pre class="lang-xml prettyprint-override"><code>&lt;El xmlns:a="http://www.w3.org/2000/xlink" a:title="T" a:href="H"/&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload