title: How to fix "An item of type 'Attribute' cannot be constructed within a node of type 'Root'." error
body: <p>I'm working on an XSLT that will output an XSLT that will be used by our installer to update an installed XML configuration file.</p>
  
  <p>My first XSLT looks like this:</p>
  
  <pre><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:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:outxsl="outputsasxsl" exclude-result-prefixes="outxsl"&gt;
    &lt;xsl:output method="xml" indent="yes" /&gt;
  
    &lt;xsl:namespace-alias result-prefix="xsl" stylesheet-prefix="outxsl"/&gt;
  
    &lt;!-- XsltArgumentList parameters--&gt;
    &lt;xsl:param name="parameter1"&gt;default1&lt;/xsl:param&gt;
    &lt;xsl:param name="parameter2"&gt;default2&lt;/xsl:param&gt;
    &lt;xsl:param name="parameter3"&gt;default3&lt;/xsl:param&gt;
    &lt;xsl:param name="parameter4"&gt;default4&lt;/xsl:param&gt;
    &lt;xsl:param name="parameter5"&gt;default5&lt;/xsl:param&gt;
    &lt;xsl:param name="parameter6"&gt;default6&lt;/xsl:param&gt;
  
    &lt;xsl:template match="/"&gt;
      &lt;outxsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
        &lt;outxsl:output method="xml" indent="yes" /&gt;
  
        &lt;!-- Output all nodes &amp; attributes for all Elements --&gt;
        &lt;outxsl:template match="/Element/Elements/Element"&gt;
          &lt;!-- Copy all attributes and nodes --&gt;
          &lt;outxsl:copy-of select="@* | node()" /&gt;
  
          &lt;!-- Output the new configuration info --&gt;
          &lt;Element xsi:type="MyType"&gt;
            &lt;Enabled&gt;true&lt;/Enabled&gt;
            &lt;ID&gt;1A619316-634A-43C0-BE69-97BB7865219F&lt;/ID&gt;
            &lt;Property1&gt;&lt;xsl:value-of select="$parameter1"/&gt;&lt;/Property1&gt;
            &lt;Property2&gt;&lt;xsl:value-of select="$parameter2" /&gt;&lt;/Property2&gt;
            &lt;Property3&gt;&lt;xsl:value-of select="$parameter3" /&gt;&lt;/Property3&gt;
            &lt;Property4&gt;&lt;xsl:value-of select="$parameter4" /&gt;&lt;/Property4&gt;
            &lt;Property5&gt;&lt;xsl:value-of select="$parameter5" /&gt;&lt;/Property5&gt;
          &lt;/Element&gt;
        &lt;/outxsl:template&gt;
  
        &lt;!-- Output everything for all the Details --&gt;
        &lt;outxsl:template match="/Element/Details/Detail"&gt;
          &lt;!-- Copy all attributes and nodes --&gt;
          &lt;outxsl:copy-of select="@* | node()"/&gt;
  
          &lt;Detail&gt;
            &lt;Property1&gt;1A619316-634A-43C0-BE69-97BB7865219F&lt;/Property1&gt;
            &lt;Property2&gt;aName&lt;/Property2&gt;
            &lt;Property3&gt;&lt;xsl:value-of select="$parameter6"/&gt;&lt;/Proprty3&gt;
            &lt;Property4&gt;anotherName&lt;/Property4&gt;
          &lt;/Detail&gt;
        &lt;/outxsl:template&gt;
      &lt;/outxsl:stylesheet&gt;      
    &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>When I run this transform in the VS 2013 XSLT tool, it works and I get a new XSLT that looks like this:</p>
  
  <pre><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:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
    &lt;xsl:output method="xml" indent="yes" /&gt;
    &lt;xsl:template match="/Element/Elements/Element"&gt;
      &lt;xsl:copy-of select="@* | node()" /&gt;
      &lt;Element xsi:type="MyType"&gt;
        &lt;Enabled&gt;true&lt;/Enabled&gt;
        &lt;ID&gt;1A619316-634A-43C0-BE69-97BB7865219F&lt;/ID&gt;
        &lt;Property1&gt;value1&lt;/Property1&gt;
        &lt;Property2&gt;value2&lt;/Property2&gt;
        &lt;Property3&gt;value3&lt;/Property3&gt;
        &lt;Property4&gt;value4&lt;/Property4&gt;
        &lt;Property5&gt;value5&lt;/Property5&gt;
      &lt;/Element&gt;
    &lt;/xsl:template&gt;
    &lt;xsl:template match="/Element/Details/Detail"&gt;
      &lt;xsl:copy-of select="@* | node()" /&gt;
      &lt;Detail&gt;
        &lt;Property1&gt;1A619316-634A-43C0-BE69-97BB7865219F&lt;/Property1&gt;
        &lt;Property2&gt;aName&lt;/Property2&gt;
        &lt;Property3&gt;Default6&lt;/Property3&gt;
        &lt;Property4&gt;anotherName&lt;/Property4&gt;
      &lt;/Detail&gt;
    &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>When I try running this second XSLT, I get the "An item of type 'Attribute' cannot be constructed within a node of type 'Root.'" error.  When I try to start the VS2013 XSLT debugger, VS crashes, so I can't even figure out what line the error is occurring on. I've tried commenting out most of code in there and the error still happens, so I think the error is somewhere near the top.</p>
  
  <p>Would someone please help me figure out what's wrong with the second XSLT &amp; how to fix the first one?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Here's a sample of the XML that is to be processed by the second XSLT:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Element xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="SubElement"&gt;
    &lt;ID&gt;1f293e9d-e779-4e07-81e8-de13fd8cac7c&lt;/ID&gt;
    &lt;Enabled&gt;true&lt;/Enabled&gt;
    &lt;Name&gt;Normal&lt;/Name&gt;
    &lt;Elements&gt;
      &lt;Element xsi:type="Type1"&gt;
        &lt;ID&gt;07e9b115-f53f-4137-8aeb-73bca17484a6&lt;/ID&gt;
        &lt;Enabled&gt;true&lt;/Enabled&gt;
        &lt;Name&gt;Type1&lt;/Name&gt;
        &lt;Stuff&gt;
          &lt;MoreStuff /&gt;
        &lt;/Stuff&gt;
        &lt;OtherStuff xsi:type="Type2"&gt;
          &lt;ID&gt;b50ae9ee-500f-483a-979e-5774de146480&lt;/ID&gt;
          &lt;Enabled&gt;true&lt;/Enabled&gt;
          &lt;Name&gt;Type2&lt;/Name&gt;
          &lt;MoreStuff /&gt;
        &lt;/OtherStuff&gt;
        &lt;SomeProperty1&gt;A Value&lt;/SomeProperty1&gt;
        &lt;SomeProperty2&gt;A Second Value&lt;/SomeProperty2&gt;
        &lt;SomeProperty3&gt;A Third Value&lt;/SomeProperty3&gt;
      &lt;/Element&gt;
      &lt;Element xsi:type="Type3"&gt;
        &lt;ID&gt;39ced73b-098b-4ec1-8b1f-c62f1b71c3ed&lt;/ID&gt;
        &lt;Enabled&gt;true&lt;/Enabled&gt;
        &lt;Name&gt;Type3&lt;/Name&gt;
        &lt;SiteId&gt;1ced7cfc-54e7-4d7e-858d-40875ff7b99d&lt;/SiteId&gt;
        &lt;StartLPRsOnStart&gt;false&lt;/StartLPRsOnStart&gt;
      &lt;/Element&gt;
    &lt;/Elements&gt;
    &lt;Details&gt;
      &lt;Detail&gt;
        &lt;Property1&gt;39ced73b-098b-4ec1-8b1f-c62f1b71c3ed&lt;/Property1&gt;
        &lt;Property2&gt;PlateRead&lt;/Property2&gt;
        &lt;Property3&gt;e8a0b04c-4c38-48e5-b013-307dd2876f3a&lt;/Property3&gt;
        &lt;Property4&gt;ReadIn&lt;/Property4&gt;
      &lt;/Detail&gt;
      &lt;Detail&gt;
        &lt;Property1&gt;07e9b115-f53f-4137-8aeb-73bca17484a6&lt;/Property1&gt;
        &lt;Property2&gt;Subscribe&lt;/Property2&gt;
        &lt;Property3&gt;39ced73b-098b-4ec1-8b1f-c62f1b71c3ed&lt;/Property3&gt;
        &lt;Property4&gt;ObjectIn&lt;/Property4&gt;
      &lt;/Detail&gt;
      &lt;Detail&gt;
        &lt;Property1&gt;07e9b115-f53f-4137-8aeb-73bca17484a6&lt;/Property1&gt;
        &lt;Property2&gt;RPC&lt;/Property2&gt;
        &lt;Property3&gt;39ced73b-098b-4ec1-8b1f-c62f1b71c3ed&lt;/Property3&gt;
        &lt;Property4&gt;RPC&lt;/Property4&gt;
      &lt;/Detail&gt;
      &lt;Detail&gt;
        &lt;Property1&gt;e8a0b04c-4c38-48e5-b013-307dd2876f3a&lt;/Property1&gt;
        &lt;Property2&gt;ReadOut&lt;/Property2&gt;
        &lt;Property3&gt;f8fa94ed-5f16-421b-810f-5eb3b1c0642e&lt;/Property3&gt;
        &lt;Property4&gt;ReadIn&lt;/Property4&gt;
      &lt;/Detail&gt;
      &lt;Detail&gt;
        &lt;Property1&gt;f8fa94ed-5f16-421b-810f-5eb3b1c0642e&lt;/Property1&gt;
        &lt;Property2&gt;ReadOut&lt;/Property2&gt;
        &lt;Property3&gt;07e9b115-f53f-4137-8aeb-73bca17484a6&lt;/Property3&gt;
        &lt;Property4&gt;Publish&lt;/Property4&gt;
      &lt;/Detail&gt;
      &lt;Detail&gt;
        &lt;Property1&gt;07e9b115-f53f-4137-8aeb-73bca17484a6&lt;/Property1&gt;
        &lt;Property2&gt;Subscribe&lt;/Property2&gt;
        &lt;Property3&gt;f8fa94ed-5f16-421b-810f-5eb3b1c0642e&lt;/Property3&gt;
        &lt;Property4&gt;ObjectIn&lt;/Property4&gt;
      &lt;/Detail&gt;
      &lt;Detail&gt;
        &lt;Property1&gt;f8fa94ed-5f16-421b-810f-5eb3b1c0642e&lt;/Property1&gt;
        &lt;Property2&gt;AlarmOut&lt;/Property2&gt;
        &lt;Property3&gt;07e9b115-f53f-4137-8aeb-73bca17484a6&lt;/Property3&gt;
        &lt;Property4&gt;Publish&lt;/Property4&gt;
      &lt;/Detail&gt;
      &lt;Detail&gt;
        &lt;Property1&gt;e253bfb5-4795-4d40-b4ff-88a334a17fc6&lt;/Property1&gt;
        &lt;Property2&gt;LogEvent&lt;/Property2&gt;
        &lt;Property3&gt;07e9b115-f53f-4137-8aeb-73bca17484a6&lt;/Property3&gt;
        &lt;Property4&gt;Publish&lt;/Property4&gt;
      &lt;/Detail&gt;
      &lt;Detail&gt;
        &lt;Property1&gt;1efde71d-9763-418e-be6d-984fce13869f&lt;/Property1&gt;
        &lt;Property2&gt;RunHotlist&lt;/Property2&gt;
        &lt;Property3&gt;5e6dcf8d-60be-4aab-b26c-91ca988bb821&lt;/Property3&gt;
        &lt;Property4&gt;RunHotlist&lt;/Property4&gt;
      &lt;/Detail&gt;
      &lt;Detail&gt;
        &lt;Property1&gt;1efde71d-9763-418e-be6d-984fce13869f&lt;/Property1&gt;
        &lt;Property2&gt;TaskOut&lt;/Property2&gt;
        &lt;Property3&gt;07e9b115-f53f-4137-8aeb-73bca17484a6&lt;/Property3&gt;
        &lt;Property4&gt;Publish&lt;/Property4&gt;
      &lt;/Detail&gt;
    &lt;/Details&gt;
  &lt;/Element&gt;
  </code></pre>