title: Closing tags when extracting HTML from XML
body: <p>I am transforming a mixed html and xml document using an xslt stylesheet and extracting only the html elements.</p>
  
  <p>Source file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;html &gt;
    &lt;head&gt;
      &lt;title&gt;Simplified Example Form&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;TLA:document xmlns:TLA="http://www.TLA.com"&gt;
        &lt;TLA:contexts&gt;
          &lt;TLA:context id="id_1" value=""&gt;&lt;/TLA:context&gt;
        &lt;/TLA:contexts&gt;
        &lt;table id="table_logo" style="display:inline"&gt;
          &lt;tr&gt;
            &lt;td height="20" align="middle"&gt;Big Title Goes Here&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
            &lt;td align="center"&gt;
              &lt;img src="logo.jpg" border="0"&gt;&lt;/img&gt;
            &lt;/td&gt;
          &lt;/tr&gt;
        &lt;/table&gt;
        &lt;TLA:page&gt;
          &lt;TLA:question id="q_id_1"&gt;
            &lt;table id="table_id_1"&gt;
              &lt;tr&gt;
                &lt;td&gt;Label text goes here&lt;/td&gt;
                &lt;td&gt;
                  &lt;input id="input_id_1" type="text"&gt;&lt;/input&gt;
                &lt;/td&gt;
              &lt;/tr&gt;
            &lt;/table&gt;
          &lt;/TLA:question&gt;
        &lt;/TLA:page&gt;
        &lt;!-- Repeat many times --&gt;
      &lt;/TLA:document&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Stylesheet:</p>
  
  <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:TLA="http://www.TLA.com" exclude-result-prefixes="TLA"&gt;
    &lt;xsl:output method="html" indent="yes" version="4.0" /&gt;
    &lt;xsl:strip-space elements="*" /&gt;
  
    &lt;xsl:template match="@*|node()" priority="-2"&gt;
      &lt;xsl:copy&gt;
        &lt;xsl:apply-templates select="@*|node()"/&gt;
      &lt;/xsl:copy&gt;
    &lt;/xsl:template&gt;
  
    &lt;!-- This element-only identity template prevents the 
         TLA namespace declaration from being copied to the output --&gt;
    &lt;xsl:template match="*"&gt;
      &lt;xsl:element name="{name()}"&gt;
        &lt;xsl:apply-templates select="@* | node()" /&gt;
      &lt;/xsl:element&gt;
    &lt;/xsl:template&gt;
  
    &lt;!-- Pass processing on to child elements of TLA elements --&gt;
    &lt;xsl:template match="TLA:*"&gt;
      &lt;xsl:apply-templates select="*" /&gt;
    &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>&lt;html&gt;
    &lt;head&gt;
      &lt;META http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
      &lt;title&gt;Simplified Example Form&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;table id="table_logo" style="display:inline"&gt;
        &lt;tr&gt;
          &lt;td height="20" align="middle"&gt;Big Title Goes Here&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td align="center"&gt;&lt;img src="logo.jpg" border="0"&gt;&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/table&gt;
      &lt;table id="table_id_1"&gt;
        &lt;tr&gt;
          &lt;td&gt;Label text goes here&lt;/td&gt;
          &lt;td&gt;&lt;input id="input_id_1" type="text"&gt;&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/table&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>However there's a problem in that the meta, img, and input elements are not being closed correctly. I've set the xsl:output to html and the version to 4.0 so as far as I know they <em>should</em> output correct html.</p>
  
  <p>I'm guessing that there needs to be a subtle change in the first xsl:template/xsl:copy instruction but my xslt skills are highly limited.</p>
  
  <p>What change needs to be made to get the tags to close correctly?</p>
  
  <p>P.S. I'm not sure if there's a difference between different tools/parsers but I'm using Visual Studio 2012 to debug the stylesheet so that I can see the immediate effect of any changes. </p>