0

I'm trying to do xsl transform in .net that outputs another xsl stylesheet. I'm outputting to text to see as I'm building. However, I'm getting the following error:

Server Error in '/' Application.

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.  
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

 Exception Details: System.InvalidOperationException: 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. 

Source Error: 



Line 33:                 xslt.Load(xrt);
Line 34:         
Line 35:                 xslt.Transform(xri, xal, xwo);
Line 36:             }
Line 37:             out11.InnerHtml = sw.ToString();

Here's entire code:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Xsl;

namespace WebApplication1
{
    public partial class _default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //http://stackoverflow.com/q/2384306/139698
            string transform = GetXsl();
            string input = GetXml();

            StringWriter sw = new StringWriter();
            using (XmlReader xrt = XmlReader.Create(new StringReader(transform)))
            using (XmlReader xri = XmlReader.Create(new StringReader(input)))
            using (XmlWriter xwo = XmlWriter.Create(sw))
            {
                XsltArgumentList xal = new XsltArgumentList();
                xal.AddExtensionObject(
                    MyXsltExtensionFunctions.Namespace,
                   new MyXsltExtensionFunctions());

                XslCompiledTransform xslt = new XslCompiledTransform();

                xslt.Load(xrt);

                xslt.Transform(xri, xal, xwo);
            }
            out11.InnerHtml = sw.ToString();
        }

        private string GetXml()
        {
            return
@"<?xml version='1.0' encoding='UTF-8'?>
<catalog>
    <data id='1' option1='key1' option2='0' />
    <data id='2' option1='' option2='1' />
</catalog>
";
        }

        private string GetXsl()
        {
            return
@"<?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:ext='http://XsltSampleSite.XsltFunctions/1.0'
            xmlns:xslout='blah'>

    <xsl:output method='text'/>

    <xslout:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

    <xsl:template match='/'>
        <span>
            <xsl:value-of select=""ext:HelloWorld()"" />
        </span>
     </xsl:template>
    </xslout:stylesheet>
</xsl:stylesheet>
";
        }
    }

    public class MyXsltExtensionFunctions
    {
        public const string Namespace = "http://XsltSampleSite.XsltFunctions/1.0";

        public string HelloWorld()
        {
            return "It works!";
        }
    }
}
Rod
  • 14,529
  • 31
  • 118
  • 230
  • Please show us your input XSLT and the XSLT you are using to transform it. The problem is almost definitely there, not in your C# code. – JLRishe Apr 22 '17 at 03:04
  • @Rod - The error could be caused by setting the output as "text" but writing the output to an XML writer (as text is not XML). Additionally, the XSLT is not valid because you have an `xsl:template` that is a child of a non-XSLT element. `xsl:template` elements must be children of the root `xsl:stylesheet` element. Your question says you are trying to generate another XSLT. In that case the best approach is to use xsl:namespace-alias. See http://stackoverflow.com/questions/13473268/xslt-to-generate-another-xslt-script for example – Tim C Apr 22 '17 at 08:29
  • I don't see how a stylesheet with output method `text` could output XSLT as XSLT is XML, unless you put every markup in as text (either escaped or in CDATA sections). Furthermore, your stylesheet does not have a single template at all producing some output so I am not sure what you expect. And pushing text output to an XmlWriter as you try to do is not the right way, unless you use the output settings of XslCompiledTransform, – Martin Honnen Apr 22 '17 at 08:30
  • Xml requires only one root tag. When you have multiple tags at the root level you have fragments which is what the error message is saying. So your xmlreader and xmlwriter settings need to be something like this : XmlReaderSettings settings = new XmlReaderSettings(); settings.ConformanceLevel = ConformanceLevel.Fragment; – jdweng Apr 22 '17 at 10:57

0 Answers0