# Friday, June 24, 2005

We’ve got some XML documents that are getting written out with way too many namespace declarations.  That probably wouldn’t be too much of a problem, except we then use those XML documents as templates to generate other documents, many with repetitive elements.  So we’re ending up with namespace bloat.  Scott and I found an example that was coming across the network at about 1.5Mb.  That’s a lot.  A large part of that turned out to be namespace declarations.  Because of the way XmlTextWriter does namespace scoping, it doesn’t write out a namespace declaration until it first sees it, which means for leaf nodes with a different namespace than their parent node, you end up with a namespace declaration on every element, like this…

<?xml version="1.0" encoding="UTF-8"?>

<ns0:RootNode xmlns:ns0="http://namespace/0">

            <ns1:FirstChild xmlns:ns1="http://namespace/1">

                        <ns2:SecondChild xmlns:ns2="http://namespace/2">Value</ns2:SecondChild>

                        <ns2:SecondChild xmlns:ns2="http://namespace/2">Value</ns2:SecondChild>

                        <ns2:SecondChild xmlns:ns2="http://namespace/2">Value</ns2:SecondChild>

                        <ns2:SecondChild xmlns:ns2="http://namespace/2">Value</ns2:SecondChild>

                        <ns2:SecondChild xmlns:ns2="http://namespace/2">Value</ns2:SecondChild>

                        <ns2:SecondChild xmlns:ns2="http://namespace/2">Value</ns2:SecondChild>

                        <ns2:SecondChild xmlns:ns2="http://namespace/2">Value</ns2:SecondChild>

                        <ns2:SecondChild xmlns:ns2="http://namespace/2">Value</ns2:SecondChild>

            </ns1:FirstChild>

</ns0:RootNode>

With our actual namespace strings, that’s like an additional 60 btyes per element that we don’t really need.  What we’d like to see is the namespaces declared once at the top of the file, then referenced elsewhere, like this…

<?xml version="1.0" encoding="UTF-8"?>

<ns0:RootNode xmlns:ns0="http://namespace/0" xmlns:ns1="http://namespace/1"  xmlns:ns2="http://namespace/2">

            <ns1:FirstChild>

                        <ns2:SecondChild>Value</ns2:SecondChild>

                        <ns2:SecondChild>Value</ns2:SecondChild>

                        <ns2:SecondChild>Value</ns2:SecondChild>

                        <ns2:SecondChild>Value</ns2:SecondChild>

                        <ns2:SecondChild>Value</ns2:SecondChild>

                        <ns2:SecondChild>Value</ns2:SecondChild>

                        <ns2:SecondChild>Value</ns2:SecondChild>

                        <ns2:SecondChild>Value</ns2:SecondChild>

            </ns1:FirstChild>

</ns0:RootNode>

When we edited the templates manually to achieve this effect, the 1.5Mb document went to like 660Kb.  Much better.

There doesn’t seem to be any way to get XmlTextWriter to do this, however.  Even if you explicitly write out the extra namespaces on the root element, you still get them everywhere, since the writer sees those as just attributes you chose to write, and not namespace declarations. 

Curses!  I’ve spent all day on this and have no ideas.  Anyone have any input?

Work | XML
Saturday, June 25, 2005 6:07:52 PM (Pacific Daylight Time, UTC-07:00)
If you can write the namespace declarations on the root element, a simple XSLT identity transform will clear up the extranous namespaces for you. I'll shoot you the code in an email (this comment thing doesn't like all the angle bracket jive talkin').
Saturday, June 25, 2005 11:06:37 PM (Pacific Daylight Time, UTC-07:00)
Can we derive our own writer and override the behavior later on?
Thursday, September 15, 2005 2:51:29 AM (Pacific Daylight Time, UTC-07:00)
Hello!I found here a plenty of useful information for myself! I will visit you soon...
Friday, September 16, 2005 2:31:15 AM (Pacific Daylight Time, UTC-07:00)
The interesting information located on your page
Tuesday, September 27, 2005 9:06:09 PM (Pacific Daylight Time, UTC-07:00)
Hello. I just wanted to give a quick greeting and tell you I enjoyed reading your material
Tuesday, December 06, 2005 1:42:45 AM (Pacific Standard Time, UTC-08:00)
A lot of news could be found here.
Tuesday, December 06, 2005 1:44:10 AM (Pacific Standard Time, UTC-08:00)
You're right. Very fascinating information!
Tuesday, December 06, 2005 1:49:19 AM (Pacific Standard Time, UTC-08:00)
Hello! I was interested in your article. It is rather instructive.
Tuesday, December 06, 2005 1:51:19 AM (Pacific Standard Time, UTC-08:00)
Hello! it;s useful and interesting site. Thanks!
Tuesday, December 06, 2005 1:52:37 AM (Pacific Standard Time, UTC-08:00)
Hello! It's very informative and splendid page!
Comments are closed.