我有一个 XSLT 转换,用于处理 XML 文件,将其插入到我的 aspx 页面的正文中。
参考以下背景信息:
我的 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
Run Code Online (Sandbox Code Playgroud)
这是相关的输出:
<div id="example" />
<?xml version="1.0" encoding="utf-8"?><div xmlns:myCustomStrings="urn:customStrings"><div id="imFormBody" class="imFormBody">
Run Code Online (Sandbox Code Playgroud)
我的问题与输出有关,特别<?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;
}
}
Run Code Online (Sandbox Code Playgroud)
事实证明,这是我进行转换的方法服务器端的结果。
XSLT 说什么并不重要,因为 .net 方法覆盖了它。
我在我的 c# 代码中得到了以下代码片段:
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2567 次 |
| 最近记录: |