xml声明未从页面中省略

时间:2010-03-29 18:08:59

标签: xml xslt asp.net

我有一个XSLT转换,用于处理XML文件,将其插入到我的aspx页面的主体中。

参考以下内容获取背景信息:

background on xml/xslt

我的xml文件中有以下内容:

<?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:myCustomStrings="urn:customStrings">
  <xsl:output
    method="xml" version="2.0"
    media-type="text/html"
    omit-xml-declaration="yes"
    indent="yes"
 />...unrelated stuff left out here

以下是相关的输出:

<div id="example" />
    <?xml version="1.0" encoding="utf-8"?><div xmlns:myCustomStrings="urn:customStrings"><div id="imFormBody" class="imFormBody">

我的问题与输出有关,特别是与输出中包含的<?xml version="1.0" encoding="utf-8"?>有关。该问题是否与我使用的自定义方法有关?如果是这样,我真的不需要包含xml部分,因为命名空间在div标签中。有没有办法确保这些额外的东西在我要求时被遗漏?

转换代码:是的,它的丑陋但我正在制作一些东西:)

public static string SmartNotePageFromTemplate(string patientVisitId, string followupType, bool copyPreviousNote)
    {
        // create custom object
        CustomStrings customStrings = new CustomStrings();//specify class for use in XSLT

        string noteXmlFile = ImSmartNotesConfig.GetSmartNotesXmlFile();//get xml data file name from config
        string noteXslFile = ImSmartNotesConfig.GetSmartNotesXsltFile();// get xslt file name from config

        string subXmlFile = "../SmartNotesXml/testData.xml";
        string subCSSFile = "../CSS/testCSS.css";
        string subJSFile = "../Js/testJS.js";

        try
        {
            XsltSettings settings = new XsltSettings(true, true);// allow the document() in the xslt

            XsltArgumentList xslArg = new XsltArgumentList();// create argument list for xslt
            xslArg.AddParam("subXmlFile", "", subXmlFile);
            xslArg.AddParam("subCSSFile", "", subCSSFile);
            xslArg.AddParam("subJSFile", "", subJSFile);
            //pass an instance of the custom object
            xslArg.AddExtensionObject("urn:customStrings", customStrings);

            XslCompiledTransform myXslt = new XslCompiledTransform(true);// we will use the compiled xslt processor
            myXslt.Load(noteXslFile, settings, new XmlUrlResolver());// load the xslt in
            StringWriter stWrite = new StringWriter();// create output writer

            //myXslt.Transform(noteXmlFile, xslArg, stWrite);// transform 
            SmartNote.BL.SmartNoteLogic logic = new SmartNote.BL.SmartNoteLogic();
            XmlDocument noteXml = logic.GetNewNoteXml(patientVisitId, followupType, copyPreviousNote);
            myXslt.Transform(noteXml, xslArg, stWrite);// transform 

            return stWrite.ToString();// put the StringWriter as a string to the output- html page in this instance, as used in the .aspx.cs file
        }
        catch (Exception e)
        {
            throw e;
        }
    }

1 个答案:

答案 0 :(得分:4)

事实证明这是方法服务器端我进行转换的结果。

无论XSLT说什么,因为.net方法超越了它。

我最终在我的c#代码中使用了以下代码片段:

XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;