title: How do I add an arbitrary namespace to an XslCompiledTransform at run-time?
body: <p>I'm attempting to perform a very simply transform of Timed Text Markup Language (TTML) documents.  Here's a minimalist example of a TTML file:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;tt xml:lang="en" xmlns="http://www.w3.org/2006/04/ttaf1"
      xmlns:tts="http://www.w3.org/2006/04/ttaf1#styling"&gt;
      &lt;head&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;div xml:lang="en" style="1"&gt;
              &lt;p begin="00:00:00.20" dur="00:00:02.26"&gt;&amp;gt;&amp;gt; One time entry&lt;br/&gt;with a line break.&lt;/p&gt;
          &lt;/div&gt;
      &lt;/body&gt;
  &lt;/tt&gt;
  </code></pre>
  
  <p>Note the document's default namespace.  That's key to the problem I'm having.</p>
  
  <p>Here is the transform I'm using.  This is the whole thing, it's very simple.</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;xsl:stylesheet version="1.0" 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
      xmlns:tt="http://www.w3.org/2006/04/ttaf1"&gt;
      &lt;xsl:output method="text" indent="yes" /&gt;
      &lt;xsl:strip-space elements="*" /&gt;
      &lt;xsl:preserve-space elements="tt:p"/&gt;
  
      &lt;!-- The indentation of the close tag for the following node is crucial to the transformed layout. Don't move it! --&gt;
      &lt;xsl:template match="tt:p"&gt;&lt;xsl:apply-templates /&gt;&amp;#160;
  &lt;/xsl:template&gt;
  
      &lt;xsl:template match="tt:p/text()"&gt;&lt;xsl:copy /&gt;&amp;#160;&lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>Our dataset has hundreds of documents and they don't all have the same default namespace. However, as you can see from the above XSLT, the transform is simple and based on the core data element <code>&lt;p /&gt;</code> so ultimately the namespace variations are unimportant.</p>
  
  <p>The above XSL currently works with some of the source files since the XSLT file has an explicitly defined namespace prefix (<code>tt</code>).  However, this doesn't work when encountering another source file with a different default namespace.</p>
  
  <p>So I need to find a way to provide the XSLT with an arbitrary, unknown namespace value for a known prefix.  I have code that can be used to find the source document default namespace:</p>
  
  <pre class="lang-cs prettyprint-override"><code>XPathDocument sourceDoc = new XPathDocument("sourcefile.xml");
  XPathNavigator sourceNav = sourceDoc.CreateNavigator();
  sourceNav.MoveToFollowing(XPathNodeType.Element);
  var sourceNS = sourceNav.GetNamespacesInScope(XmlNamespaceScope.ExcludeXml);
  string defNS = sourceNS.Single(n =&gt; n.Key =="").Value;
  </code></pre>
  
  <p>This correctly gives me <code>http://www.w3.org/2006/04/ttaf1</code>.  However, I can't seem to figure out what to do with this value.  I have searched and experimented for many hours trying to somehow provide the <code>XslCompiledTransform</code> instance the variable namespace.  It appears that there's nothing within the area of the transform itself that can take it.  </p>
  
  <p>I've tried explicitly loading the XSLT file into an <code>XmlDocument</code> in order to manipulate the name table (after removing the explicit <code>xmlns:tt="..."</code> namespace declaration in the above XSLT):</p>
  
  <pre class="lang-cs prettyprint-override"><code>XslCompiledTransform objXsl = new XslCompiledTransform();
  StringWriter writer = new StringWriter();
  XPathDocument sourceDoc;
  XmlDocument xslDoc = new XmlDocument();
  XPathNavigator sourceNav, xslNav;
  XmlNamespaceManager xslNsManager;
  
  sourceDoc = new XPathDocument("sourcefile.xml");
  sourceNav = sourceDoc.CreateNavigator();
  sourceNav.MoveToFollowing(XPathNodeType.Element);
  var sourceNS = sourceNav.GetNamespacesInScope(XmlNamespaceScope.ExcludeXml);
  
  xslDoc.Load("transform.xslt");
  
  xslNsManager = new XmlNamespaceManager(xslDoc.NameTable);
  xslNsManager.AddNamespace("tt", sourceNS.Single(n =&gt; n.Key =="").Value);
  
  xslNav = xslDoc.CreateNavigator();
  objXsl.Load(xslNav);
  
  objXsl.Transform(sourceNav, null, writer);
  </code></pre>
  
  <p>This runs up until the actual transform, where I get an <code>XslLoadException</code> stating that <code>Prefix 'tt' is not defined.</code></p>
  
  <p>I'm at a loss at this point.  Everything I can find from searching discusses putting the namespace into the XSLT document (which I already have and it works fine for one namespace value).  I can't find anything in MSDN documentation or elsewhere that explains how to add the namespace definitions into the transform on the fly.</p>
  
  <p>Anyone have any ideas?</p>