locked
Unusual Error - Token Text in state Start would result in an invalid XML document RRS feed

  • Question

  • Why do I get this error/warning when I attempt to translate an Xml File?

     

    Token Text in state Start would result in an invalid XML document. Make sure that the ConformanceLevel setting is set to ConformanceLevel.Fragment or ConformanceLevel.Auto if you want to write an XML fragment.


    My code :

    Code Snippet

    Dim xslt As New XslCompiledTransform()

    Dim settings As New XmlReaderSettings

    settings.ProhibitDtd = False

    xslt.Load(XmlReader.Create(stylesheetXslFile, settings))

    'xslt.Transform(reader, writer)

    xslt.Transform(XmlReader.Create(sourceXmlFile, settings), XmlWriter.Create(outputXmlFile))

     

    IF there's an MSDN article about this, could you please point me to it? Thank you.

    Monday, June 4, 2007 3:57 PM

Answers

  • That sounds as if your XSLT stylesheet does not generate a root element but rather a top level text node instead. Can you show us the XSLT stylesheet?

    To work around the problem it might help to use XmlWriterSettings with ConformanceLevel set to Fragment.

    Monday, June 4, 2007 4:12 PM
  • Hi

    After going through creating a simple example that produced the error I decided to remove the namespaces from the schemas used to create the serialisable objects and suddenly the exception no longer occurred.  I'm not sure how the transform had worked at all given that the XSLT didn't make any reference to the namespaces (originally I'd had no namespaces as the XML and objects are only for internal use).  Anyway, the long and short of it is that once the namespaces were in and the XSLT was corrected to scope everything to the correct namespace the transform no longer threw the exception.

    Thanks for having a go, much appreciated.
    Thursday, November 29, 2007 8:07 AM

All replies

  • That sounds as if your XSLT stylesheet does not generate a root element but rather a top level text node instead. Can you show us the XSLT stylesheet?

    To work around the problem it might help to use XmlWriterSettings with ConformanceLevel set to Fragment.

    Monday, June 4, 2007 4:12 PM
  • Right on money Martin...The xslt in deed was not generating the root element. Thank you very much.

     

    I wish they had some sort of reference for this error message in MSDN.

    Monday, June 4, 2007 7:29 PM
  • >That sounds as if your XSLT stylesheet does not generate a root element but rather a top level text node instead. Can you >show us the XSLT stylesheet?

    I have an XSLT that's producing this exception as well.  The XML being transformed is produced from an object that is serialised to XML using XmlSerializer.  There is evidently a difference between this XML and the XML that is read in if the OuterXml from that document is saved to a file and then read in for transformation instead of attempting to transform it directly as reading it in from a file doesn't cause the exception to be thrown.  I haven't yet been able to determine why that is.

    The XSLT looks something like this (shortened version and names have been changed to protect the innocent):

    <?xml version="1.0" encoding="UTF-8" ?>
    <xslTongue Tiedtylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xslSurpriseutput doctype-public="-//W3C//DTD html 4.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" omit-xml-declaration="yes" indent="yes" encoding="UTF-8"/>
      <xslStick out tonguearam name="html-content-type" />
      <xsl:template match="/SourceRootNode">
        <html>
          <head xmlns="http://www.w3.org/1999/xhtml">
          </head>
          <body vLink="purple" link="blue">
            <table class="outerTable">
              <tr class="outerTableInfoRow">
                <td width="75%">
                  <xsl:text>Some text : </xsl:text><xsl:value-of select="ItemCode"/>
                </td>
                <td width="15%">
                  <xsl:value-of select="PaymentDate"/>
                </td>
                <td width="10%">
                  <xsl:value-of select="PaymentTime"/>
                </td>
              </tr>
            </table>
          </body>
        </html>
      </xsl:template>
    </xslTongue Tiedtylesheet>

    I'm using this to transform the (shortened) XML below into HTML:

    <?xml version="1.0"?>
    <SourceRootNode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <ItemCode>CodeValue</ItemCode>
        <PaymentDate>2007-11-28</PaymentDate>
        <PaymentTime>15:32</PaymentTime>
        <InnerData>
           
        </InnerData>
    </SourceRootNode>

    With the XSLT as shown I get the exception.  If I remove SourceRootNode from the match value and put it on each select then the XSLT works fine.  Using ConformanceLevel.Fragment or Auto has the correct data selected, but only the selected data is in the output, everything else in the XSLT is gone.  The source XML is properly formed and the XSLT is producing a root node, <html>, but I still get an exception telling me there's no root node.

    I've tried adding an parent node for the SourceRootNode, but that makes no difference.  So long as the XSLT has the root node in the match attribute the exception is thrown.

    Any help is much appreciated.
    Wednesday, November 28, 2007 2:56 PM
  • I can't reproduce the issue with the samples you posted.

    Wednesday, November 28, 2007 4:12 PM
  • I know.  As I said, if the XML comes from a file (probably also true if the XML is just a string) rather than from the .NET XmlSerializer then the transform works.  I'll create some arbitrary serialisable types and see if I can reproduce it with those and, if so, I'll post them.
    Thursday, November 29, 2007 7:09 AM
  • Hi

    After going through creating a simple example that produced the error I decided to remove the namespaces from the schemas used to create the serialisable objects and suddenly the exception no longer occurred.  I'm not sure how the transform had worked at all given that the XSLT didn't make any reference to the namespaces (originally I'd had no namespaces as the XML and objects are only for internal use).  Anyway, the long and short of it is that once the namespaces were in and the XSLT was corrected to scope everything to the correct namespace the transform no longer threw the exception.

    Thanks for having a go, much appreciated.
    Thursday, November 29, 2007 8:07 AM
  • This error seems to be triggered at different situations in my case it was because an expression in the xslt file:

     

    what did not work:

     

    Code Snippet

    <?xml version="1.0" encoding="us-ascii" ?>

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:user="urn:tamer-extension-lib:ToFormatMyContent">

    <xsl:output method="xml" indent="yes"/>

    <!--match the feed element at the root-->

    <xsl:template match="/Public/list/search">

    <!--add the root element "posts"-->

    <xsl:element name="posts">

    <!--add an attribute to count how many posts are in the file-->

    <xsl:attribute name="count">

    <xsl:value-of select="count(//Article)"/>

    </xsl:attribute>

    <!--at the Feed element level, drill down to all Article (regardless of the intermediate Source element)-->

    <xsl:apply-templates select="//Article" />

    </xsl:element>

    </xsl:template>

     

    continue the rest of the document here...

     

     

    so I was trying to get to all <Article> elements in the document, assuming I have drilled enough at the root level by using the expression:

    "/Public/list/search", but once I modified this a little bit to "/Public", it worked.

     

    Note: both expressions work when you try to use Visual Studio xslt transformation.

     

    -Tamer

    Friday, June 20, 2008 1:21 PM
  • Copying over the settings for XML Writer from CompiledTransform.OutputSettings did the trick for me. This also appears as a tool-tip for the Transform function in Visual Studio.
    • Proposed as answer by sprabs Tuesday, May 4, 2010 9:30 PM
    Tuesday, May 4, 2010 9:30 PM