You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xerces.apache.org by Jesse Pelton <js...@PKC.com> on 2007/04/20 16:37:19 UTC

Namespace defaulting on imported document fragments

I'm seeing a behavior with the handling of namespaces that strikes me as
a little odd.  I don't remember seeing it discussed, and maybe it's by
design, so I thought it would be worth running it by the list.

I have a document with its own schema and namespace that has elements
that can contain XHTML children.  I use the DOM to extract those
children and import them into XHTML documents that I'm assembling.  The
root element of the target document declares the XHTML namespace as the
default namespace.

If the element in the source document that contains the XHTML fragment
to be extracted makes the XHTML namespace the default, everything works
as I'd expect.  In particular, the imported fragment has the correct
namespace and gets serialized without a namespace declaration.

If, however, the XHTML namespace declaration in the source document is
on an element that is extracted and then imported, the xmlns declaration
is treated as an attribute and serialized.  (Namespace processing also
occurs correctly.)  The end result is an output document that has a
redundant default namespace declaration.  It's not harmful, of course,
but it's a bit surprising, and I'd like to avoid it if possible.

I'm about to head off on vacation and I don't think it'll be trivial to
work up a sample, so before I do, I thought I'd check whether this rings
bells with anyone.

Here are two contrived illustrations, source document followed by output
document.  The first pair illustrates the expected serialization that
results when XHTML elements inherit the correct default namespace.

<?xml version="1.0" encoding="UTF-8"?>
<src:source xmlns    ="http://www.w3.org/1999/xhtml"
            xmlns:src="http://www.example.com/source">
  <p>Some text to be extracted and imported into a target document.</p>
</src:source>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
  <body>
    <p>Some text to be extracted and imported into a target
document.</p>
  </body>
</html>

The second pair illustrates the surprising serialization that results
when XHTML elements declare their own default namespace.

<?xml version="1.0" encoding="UTF-8"?>
<source xmlns="http://www.example.com/source">
  <p xmlns="http://www.w3.org/1999/xhtml">Some text to be extracted and
imported into a target document.</p>
</source>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
  <body>
    <p xmlns="http://www.w3.org/1999/xhtml">Some text to be extracted
and imported into a target document.</p>
  </body>
</html>