locked
token startelement in state endrootelement - System.InvalidOperationException RRS feed

Answers


  • The difference is, I want the output of the transform to be the whole of the XmlDocument (including DocumentElement).

    Is that feasible?


    With the samples you have posted I don't see any problems and I don't get any problems when I change my code sample to

          XmlDocument doc = new XmlDocument();
          //doc.LoadXml(@"<root><foo><bar>baz</bar></foo></root>");
    
          XslCompiledTransform proc = new XslCompiledTransform();
          proc.Load(@"..\..\XSLTFile2.xslt");
    
          using (XmlWriter xw = doc.CreateNavigator().AppendChild())
          {
            proc.Transform(@"..\..\XMLFile2.xml", null, xw);
            xw.Close();
          }
          doc.Save(Console.Out);
    


    it continues to work fine, it doesn't give the exception you say you get. But in general the result of an XSLT transformation does not have to be a well-formed document, it suffices if it is a well-formed fragment (or the correct term is entity), so it could have several top level elements or even top level non-whitespace text nodes and no element at all. In that case a System.Xml.XmlDocument can't handle such a transformation result as it always represents a well-formed document with a single root element containing all other elements.

    So that is what I guess what happens when you get the error, your stylesheet does not create well-formed document, only a fragment. You would need to change your stylesheet code to ensure it outputs a root element containing other elements if you want the transformation result to be stored in a System.Xml.XmlDocument.

    Or you would need to write an XmlDocumentFragment as in the following sample:

          XmlDocument doc = new XmlDocument();
          XmlDocumentFragment frag = doc.CreateDocumentFragment();
    
          XslCompiledTransform proc = new XslCompiledTransform();
          proc.Load(@"..\..\XSLTFile3.xslt");
    
          using (XmlWriter xw = frag.CreateNavigator().AppendChild())
          {
            proc.Transform(@"..\..\XMLFile2.xml", null, xw);
            xw.Close();
          }
    
          using (XmlWriter xw = XmlWriter.Create(Console.Out, new XmlWriterSettings() { ConformanceLevel = ConformanceLevel.Fragment }))
          {
            frag.WriteTo(xw);
          }
    
    


    That way the following stylesheet creating a fragment with several top level elements and text nodes works fine:

    <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"
    >
      <xsl:output method="xml" indent="yes"/>
    
    
     <xsl:template match="/">
      Text.
      <first-element/>
      Text.
      <second-element/>
      Text
     </xsl:template>
    </xsl:stylesheet>
    
    


     

     


    MVP Data Platform Development My blog
    Sunday, July 31, 2011 10:23 AM

All replies

  • Consider to show us code samples of what exactly you are doing, otherwise it is hard to tell why you get that error and how to avoid it.
    MVP Data Platform Development My blog
    Friday, July 29, 2011 5:19 PM
  • <?xml version="1.0" encoding="utf-8"?>
    <GetListItems-Results xmlns="">
     <listitems xmlns="http://schemas.microsoft.com/sharepoint/soap/" xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema">
      <rs:data ItemCount="2">
       <z:row ows_ContentTypeId="0x01007E207DAF5907EA4FB44D6B8D8B5672B1" ows_Title="Person1" ows_Addr1="Addr1-1" ows_Addr2="Addr1-2" ows_ID="1" ows_ContentType="Item" ows_Modified="2011-07-29 10:44:32" ows_Created="2011-07-29 10:44:32" ows_owshiddenversion="1" ows_WorkflowVersion="1" ows__UIVersion="512" ows__UIVersionString="1.0" ows_Attachments="0" ows__ModerationStatus="0" ows_LinkTitleNoMenu="Person1" ows_LinkTitle="Person1" ows_LinkTitle2="Person1" ows_SelectTitle="1" ows_Order="100.000000000000" ows_GUID="{B6CB6909-A60C-4E7B-B088-E1CAD1BBF656}" ows_FileRef="1;#Lists/Customer/1_.000" ows_FileDirRef="1;#Lists/Customer" ows_Last_x0020_Modified="1;#2011-07-29 10:44:32" ows_Created_x0020_Date="1;#2011-07-29 10:44:32" ows_FSObjType="1;#0" ows_SortBehavior="1;#0" ows_PermMask="0x7fffffffffffffff" ows_FileLeafRef="1;#1_.000" ows_UniqueId="1;#{12CF627D-14C7-4672-B41B-1D1141D31AB9}" ows_ProgId="1;#" ows_ScopeId="1;#{51211A82-ADC4-4B1A-8B4D-3E1A6A2ED3E2}" ows__EditMenuTableStart="1_.000" ows__EditMenuTableStart2="1" ows__EditMenuTableEnd="1" ows_LinkFilenameNoMenu="1_.000" ows_LinkFilename="1_.000" ows_LinkFilename2="1_.000" ows_ServerUrl="/Lists/Customer/1_.000" ows_BaseName="1_" ows_MetaInfo="1;#" ows__Level="1" ows__IsCurrentVersion="1" ows_ItemChildCount="1;#0" ows_FolderChildCount="1;#0" />
       <z:row ows_ContentTypeId="0x01007E207DAF5907EA4FB44D6B8D8B5672B1" ows_Title="Person2" ows_Addr1="Addr2-1" ows_Addr2="Addr2-2" ows_ID="2" ows_ContentType="Item" ows_Modified="2011-07-29 10:44:56" ows_Created="2011-07-29 10:44:56" ows_owshiddenversion="1" ows_WorkflowVersion="1" ows__UIVersion="512" ows__UIVersionString="1.0" ows_Attachments="0" ows__ModerationStatus="0" ows_LinkTitleNoMenu="Person2" ows_LinkTitle="Person2" ows_LinkTitle2="Person2" ows_SelectTitle="2" ows_Order="200.000000000000" ows_GUID="{15889626-7119-46E1-98C3-FB3131536366}" ows_FileRef="2;#Lists/Customer/2_.000" ows_FileDirRef="2;#Lists/Customer" ows_Last_x0020_Modified="2;#2011-07-29 10:44:56" ows_Created_x0020_Date="2;#2011-07-29 10:44:56" ows_FSObjType="2;#0" ows_SortBehavior="2;#0" ows_PermMask="0x7fffffffffffffff" ows_FileLeafRef="2;#2_.000" ows_UniqueId="2;#{F604BCAE-673C-470E-9BB8-72D61155EFF8}" ows_ProgId="2;#" ows_ScopeId="2;#{51211A82-ADC4-4B1A-8B4D-3E1A6A2ED3E2}" ows__EditMenuTableStart="2_.000" ows__EditMenuTableStart2="2" ows__EditMenuTableEnd="2" ows_LinkFilenameNoMenu="2_.000" ows_LinkFilename="2_.000" ows_LinkFilename2="2_.000" ows_ServerUrl="/Lists/Customer/2_.000" ows_BaseName="2_" ows_MetaInfo="2;#" ows__Level="1" ows__IsCurrentVersion="1" ows_ItemChildCount="2;#0" ows_FolderChildCount="2;#0" />
      </rs:data>
     </listitems>
     <listitems xmlns="http://schemas.microsoft.com/sharepoint/soap/" xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema">
      <rs:data ItemCount="3">
       <z:row ows_ContentTypeId="0x010019CB6DCB64C75E48B609CF827DE86025" ows_Title="Goods1" ows_Customer="1;#Person1" ows_TotalPrice="234.000000000000" ows_ID="1" ows_ContentType="Item" ows_Modified="2011-07-29 10:45:17" ows_Created="2011-07-29 10:45:17" ows_owshiddenversion="1" ows_WorkflowVersion="1" ows__UIVersion="512" ows__UIVersionString="1.0" ows_Attachments="0" ows__ModerationStatus="0" ows_LinkTitleNoMenu="Goods1" ows_LinkTitle="Goods1" ows_LinkTitle2="Goods1" ows_SelectTitle="1" ows_Order="100.000000000000" ows_GUID="{B365B9D5-7FA4-4C7A-9E42-649886ECCB48}" ows_FileRef="1;#Lists/Orders/1_.000" ows_FileDirRef="1;#Lists/Orders" ows_Last_x0020_Modified="1;#2011-07-29 10:45:17" ows_Created_x0020_Date="1;#2011-07-29 10:45:17" ows_FSObjType="1;#0" ows_SortBehavior="1;#0" ows_PermMask="0x7fffffffffffffff" ows_FileLeafRef="1;#1_.000" ows_UniqueId="1;#{152EE609-BED7-4AF9-934A-4554F5A343FD}" ows_ProgId="1;#" ows_ScopeId="1;#{51211A82-ADC4-4B1A-8B4D-3E1A6A2ED3E2}" ows__EditMenuTableStart="1_.000" ows__EditMenuTableStart2="1" ows__EditMenuTableEnd="1" ows_LinkFilenameNoMenu="1_.000" ows_LinkFilename="1_.000" ows_LinkFilename2="1_.000" ows_ServerUrl="/Lists/Orders/1_.000" ows_BaseName="1_" ows_MetaInfo="1;#" ows__Level="1" ows__IsCurrentVersion="1" ows_ItemChildCount="1;#0" ows_FolderChildCount="1;#0" />
       <z:row ows_ContentTypeId="0x010019CB6DCB64C75E48B609CF827DE86025" ows_Title="Good2" ows_Customer="1;#Person1" ows_TotalPrice="99.0000000000000" ows_ID="2" ows_ContentType="Item" ows_Modified="2011-07-29 10:45:30" ows_Created="2011-07-29 10:45:30" ows_owshiddenversion="1" ows_WorkflowVersion="1" ows__UIVersion="512" ows__UIVersionString="1.0" ows_Attachments="0" ows__ModerationStatus="0" ows_LinkTitleNoMenu="Good2" ows_LinkTitle="Good2" ows_LinkTitle2="Good2" ows_SelectTitle="2" ows_Order="200.000000000000" ows_GUID="{87448107-E6DE-4453-9E5D-1422F6691C4B}" ows_FileRef="2;#Lists/Orders/2_.000" ows_FileDirRef="2;#Lists/Orders" ows_Last_x0020_Modified="2;#2011-07-29 10:45:30" ows_Created_x0020_Date="2;#2011-07-29 10:45:30" ows_FSObjType="2;#0" ows_SortBehavior="2;#0" ows_PermMask="0x7fffffffffffffff" ows_FileLeafRef="2;#2_.000" ows_UniqueId="2;#{1A979C3B-6669-4EA2-AF5B-7F7C82A290BA}" ows_ProgId="2;#" ows_ScopeId="2;#{51211A82-ADC4-4B1A-8B4D-3E1A6A2ED3E2}" ows__EditMenuTableStart="2_.000" ows__EditMenuTableStart2="2" ows__EditMenuTableEnd="2" ows_LinkFilenameNoMenu="2_.000" ows_LinkFilename="2_.000" ows_LinkFilename2="2_.000" ows_ServerUrl="/Lists/Orders/2_.000" ows_BaseName="2_" ows_MetaInfo="2;#" ows__Level="1" ows__IsCurrentVersion="1" ows_ItemChildCount="2;#0" ows_FolderChildCount="2;#0" />
       <z:row ows_ContentTypeId="0x010019CB6DCB64C75E48B609CF827DE86025" ows_Title="Good2a" ows_Customer="2;#Person2" ows_TotalPrice="100.000000000000" ows_ID="3" ows_ContentType="Item" ows_Modified="2011-07-29 10:46:02" ows_Created="2011-07-29 10:46:02" ows_owshiddenversion="1" ows_WorkflowVersion="1" ows__UIVersion="512" ows__UIVersionString="1.0" ows_Attachments="0" ows__ModerationStatus="0" ows_LinkTitleNoMenu="Good2a" ows_LinkTitle="Good2a" ows_LinkTitle2="Good2a" ows_SelectTitle="3" ows_Order="300.000000000000" ows_GUID="{07AFD87A-20AA-410C-B57F-E828C38072EB}" ows_FileRef="3;#Lists/Orders/3_.000" ows_FileDirRef="3;#Lists/Orders" ows_Last_x0020_Modified="3;#2011-07-29 10:46:02" ows_Created_x0020_Date="3;#2011-07-29 10:46:02" ows_FSObjType="3;#0" ows_SortBehavior="3;#0" ows_PermMask="0x7fffffffffffffff" ows_FileLeafRef="3;#3_.000" ows_UniqueId="3;#{9B9DA6B6-7008-4836-BF43-DC505BA990D0}" ows_ProgId="3;#" ows_ScopeId="3;#{51211A82-ADC4-4B1A-8B4D-3E1A6A2ED3E2}" ows__EditMenuTableStart="3_.000" ows__EditMenuTableStart2="3" ows__EditMenuTableEnd="3" ows_LinkFilenameNoMenu="3_.000" ows_LinkFilename="3_.000" ows_LinkFilename2="3_.000" ows_ServerUrl="/Lists/Orders/3_.000" ows_BaseName="3_" ows_MetaInfo="3;#" ows__Level="1" ows__IsCurrentVersion="1" ows_ItemChildCount="3;#0" ows_FolderChildCount="3;#0" />
      </rs:data>
     </listitems>
    </GetListItems-Results>
    

    <?xml version="1.0" encoding="utf-8"?>
    <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:soap="http://schemas.microsoft.com/sharepoint/soap/"
            xmlns:rs="urn:schemas-microsoft-com:rowset"
            xmlns:z="#RowsetSchema"
    >
      <xsl:output method="xml" indent="yes"/>
    
    
    
     <xsl:template match="/">
      <Result>
       <xsl:apply-templates select="GetListItems-Results/soap:listitems[1]" mode="table1"/>
       <!--xsl:apply-templates select="GetListItems-Results/soap:listitems[2]/rs:data/z:row" mode="table2"/-->
    
      </Result>
     </xsl:template>
     <xsl:template match="GetListItems-Results">
      <xsl:apply-templates select="soap:listitems"/>
     </xsl:template>
     <xsl:template match="soap:listitems">
    
       <xsl:apply-templates select="rs:data/z:row" mode="table1"/>
     </xsl:template>
     
     <xsl:template match="z:row" mode="table1">
      <row>
       <xsl:apply-templates select="@*[name()='ows_Title' or name()='ows_Addr1']"/>
       <xsl:variable name="custId">
        <xsl:value-of select="@ows_ID"/><xsl:text>;#</xsl:text><xsl:value-of select="@ows_Title"/>
       </xsl:variable>
       <xsl:attribute name="GrandTotal">
        <xsl:value-of select="sum(/GetListItems-Results/soap:listitems[2]/rs:data/z:row[@ows_Customer=$custId]/@ows_TotalPrice)"/>
       </xsl:attribute>
      </row>
     </xsl:template>
    
     <xsl:template match="z:row" mode="table2">
      <xsl:apply-templates select="@*[name()='ows_TotalPrice']"/>
     </xsl:template>
     
     <xsl:template match="@*">
      <xsl:copy>
       <xsl:apply-templates select="@* | node()"/>
      </xsl:copy>
     </xsl:template>
    </xsl:stylesheet>
    

    Friday, July 29, 2011 6:39 PM
  • Well we need to see the code running the transformation as well, so far all you have shown is some XML and XSLT.

    When I run that code with the following sample C# console application code

          XmlDocument doc = new XmlDocument();
          doc.LoadXml(@"<root><foo><bar>baz</bar></foo></root>");
    
          XslCompiledTransform proc = new XslCompiledTransform();
          proc.Load(@"..\..\XSLTFile2.xslt");
    
          using (XmlWriter xw = doc.DocumentElement["foo"].CreateNavigator().AppendChild())
          {
            proc.Transform(@"..\..\XMLFile2.xml", null, xw);
            xw.Close();
          }
          doc.Save(Console.Out);
    


    I don't get any errors, the output on the console is

    <root>
     <foo>
      <bar>baz</bar>
      <Result xmlns:soap="http://schemas.microsoft.com/sharepoint/soap/" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema">
    
       <row ows_Title="Person1" ows_Addr1="Addr1-1" GrandTotal="333" />
       <row ows_Title="Person2" ows_Addr1="Addr2-1" GrandTotal="100" />
    
     </Result>
     </foo>
    </root>
    
    


     

     


    MVP Data Platform Development My blog
    Saturday, July 30, 2011 10:09 AM
  • Hi Martin,

     

    The difference is, I want the output of the transform to be the whole of the XmlDocument (including DocumentElement).

    Is that feasible?

    Thanks

    Martin

    Saturday, July 30, 2011 6:36 PM

  • The difference is, I want the output of the transform to be the whole of the XmlDocument (including DocumentElement).

    Is that feasible?


    With the samples you have posted I don't see any problems and I don't get any problems when I change my code sample to

          XmlDocument doc = new XmlDocument();
          //doc.LoadXml(@"<root><foo><bar>baz</bar></foo></root>");
    
          XslCompiledTransform proc = new XslCompiledTransform();
          proc.Load(@"..\..\XSLTFile2.xslt");
    
          using (XmlWriter xw = doc.CreateNavigator().AppendChild())
          {
            proc.Transform(@"..\..\XMLFile2.xml", null, xw);
            xw.Close();
          }
          doc.Save(Console.Out);
    


    it continues to work fine, it doesn't give the exception you say you get. But in general the result of an XSLT transformation does not have to be a well-formed document, it suffices if it is a well-formed fragment (or the correct term is entity), so it could have several top level elements or even top level non-whitespace text nodes and no element at all. In that case a System.Xml.XmlDocument can't handle such a transformation result as it always represents a well-formed document with a single root element containing all other elements.

    So that is what I guess what happens when you get the error, your stylesheet does not create well-formed document, only a fragment. You would need to change your stylesheet code to ensure it outputs a root element containing other elements if you want the transformation result to be stored in a System.Xml.XmlDocument.

    Or you would need to write an XmlDocumentFragment as in the following sample:

          XmlDocument doc = new XmlDocument();
          XmlDocumentFragment frag = doc.CreateDocumentFragment();
    
          XslCompiledTransform proc = new XslCompiledTransform();
          proc.Load(@"..\..\XSLTFile3.xslt");
    
          using (XmlWriter xw = frag.CreateNavigator().AppendChild())
          {
            proc.Transform(@"..\..\XMLFile2.xml", null, xw);
            xw.Close();
          }
    
          using (XmlWriter xw = XmlWriter.Create(Console.Out, new XmlWriterSettings() { ConformanceLevel = ConformanceLevel.Fragment }))
          {
            frag.WriteTo(xw);
          }
    
    


    That way the following stylesheet creating a fragment with several top level elements and text nodes works fine:

    <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"
    >
      <xsl:output method="xml" indent="yes"/>
    
    
     <xsl:template match="/">
      Text.
      <first-element/>
      Text.
      <second-element/>
      Text
     </xsl:template>
    </xsl:stylesheet>
    
    


     

     


    MVP Data Platform Development My blog
    Sunday, July 31, 2011 10:23 AM
  • Thanks Martin
    Monday, August 1, 2011 2:30 PM