Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# xsl试图转换为另一个xsl样式表_C#_Xml_Xslt_Xslt 1.0 - Fatal编程技术网

C# xsl试图转换为另一个xsl样式表

C# xsl试图转换为另一个xsl样式表,c#,xml,xslt,xslt-1.0,C#,Xml,Xslt,Xslt 1.0,我试图在.net中进行xsl转换,以输出另一个xsl样式表。我正在输出文本,以便在构建时查看。但是,我得到了以下错误: 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 Confor

我试图在.net中进行xsl转换,以输出另一个xsl样式表。我正在输出文本,以便在构建时查看。但是,我得到了以下错误:

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();
以下是完整的代码:

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!";
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.Xml;
使用System.Xml.Xsl;
命名空间WebApplication1
{
公共部分类\u默认值:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
//http://stackoverflow.com/q/2384306/139698
字符串转换=GetXsl();
字符串输入=GetXml();
StringWriter sw=新的StringWriter();
使用(XmlReader xrt=XmlReader.Create(new-StringReader(transform)))
使用(XmlReader xri=XmlReader.Create(新的StringReader(输入)))
使用(xmlwriterxwo=XmlWriter.Create(sw))
{
XsltArgumentList xal=新的XsltArgumentList();
xal.AddExtensionObject(
MyXsltExtensionFunctions.Namespace,
新的MyXsltExtensionFunctions());
XslCompiledTransform xslt=新的XslCompiledTransform();
Load(xrt);
Transform(xri、xal、xwo);
}
out11.InnerHtml=sw.ToString();
}
私有字符串GetXml()
{
返回
@"
";
}
私有字符串GetXsl()
{
返回
@"
";
}
}
公共类MyXsltExtensionFunctions
{
公用常量字符串命名空间=”http://XsltSampleSite.XsltFunctions/1.0";
公共字符串HelloWorld()
{
返回“它工作!”;
}
}
}

请向我们展示您的输入XSLT和用于转换它的XSLT。问题几乎肯定存在,而不是在您的C#代码中。@Rod-错误可能是由于将输出设置为“text”,但将输出写入XML编写器(因为文本不是XML)而引起的。此外,XSLT无效,因为您有一个
xsl:template
,它是非XSLT元素的子元素
xsl:template
元素必须是根
xsl:stylesheet
元素的子元素。您的问题表示您正在尝试生成另一个XSLT。在这种情况下,最好的方法是使用xsl:namespace别名。例如,请参阅。我不知道带有输出方法
text
的样式表如何能够将XSLT作为XSLT是XML输出,除非您将每个标记作为文本(转义或CDATA部分)输入。此外,您的样式表根本没有生成某些输出的单个模板,因此我不确定您希望得到什么。并且,在您尝试这样做时将文本输出推送到XmlWriter并不是正确的方法,除非您使用XslCompiledTransform的输出设置,否则Xml只需要一个根标记。当在根级别有多个标记时,就会有片段,这就是错误消息所说的。所以您的xmlreader和xmlwriter设置需要如下所示:XmlReaderSettings=new XmlReaderSettings();settings.ConformanceLevel=ConformanceLevel.Fragment;