使用 XSLT 将 XML 转换为 HTML
我在 stackoverflow 上看到了几个与 XML、XSLT 和 HTML 相关的问题,但我相信我的问题有点不同。这是我需要的:
我需要向我的客户发送一封电子邮件。电子邮件的措辞可能会根据情况而有所不同。每个场景都有相应的格式保存在数据库中。例如,一种情况可能需要此电子邮件:
情况 1:我们已为您 XXX 的呼叫开立票证 XXX/参考号 XXX。请致电 XXX 联系我们以跟踪进度。
另一种情况可能需要此电子邮件:
情况 2:感谢您对我们的产品 XXX 感兴趣。正如所讨论的,我们将在 XXX 派我们的代表到您位于 XXX 的办公室。
此外,根据数据可用性,格式可能需要稍作更改。例如,如果我需要发送与场景 1 相对应的电子邮件,并且我没有可用的参考号,我想完全即时删除参考号部分(不在数据库中),即我想要这样的内容:
场景3:我们已为您 XXX 的来电开出 XXX 票。请致电 XXX 联系我们以跟踪进度。
场景 1 和 2 的格式以 XSLT 形式存储在数据库中,与您在上面看到的字符串相对应。场景 3 的格式必须即时生成。 XXX部分必须替换为实际数据。该数据在我拥有的 XML 可序列化对象中可用。
我想序列化这个对象,在内存中生成一个 XML,稍微修改一下 XSLT(即时)以反映我拥有的数据,使用场景的 XSLT 将内存中的 XML 转换为 HTML,然后将 HTML 作为我拥有的电子邮件方法的字符串参数。电子邮件部分有效。我需要处理内存中的对象->XML->轻微的XSLT修改->使用适当的 XSLT 的 HTML。
如果您可以包含代码示例而不仅仅是我需要遵循的方法,我将不胜感激。
编辑:
这是工作代码:
using (xsltStream = new MemoryStream(emailInfo.Body))
{
// Create an XmlReader from the Stream
XmlReader reader = XmlReader.Create(xsltStream);
// Create and load the transform with document function enabled.
XslCompiledTransform transform = new XslCompiledTransform();
XsltSettings settings = new XsltSettings();
settings.EnableDocumentFunction = true;
transform.Load(reader, settings, null);
// Execute the transformation.
transform.Transform(doc, writer);
}
I saw several questions related to XML, XSLT and HTML on stackoverflow, but I beleive my question is a little different. Here is what I need:
I need to send an email to my customers. The wordings of the email can differ based upon the scenario. Each scenario has a corresponding format saved in the database. For example, one scenario might require this email:
Scenario 1: We have opened Ticket XXX/Reference Number XXX for your call on XXX. Kindly call us at XXX to track progress.
Another scenario might required this email:
Scenario 2: Thanks for your interest in our Product XXX. As discussed we will send our representative on XXX to your office located at XXX.
Also, the format might need to be altered a bit depending upon data availability. e.g. if I need to send email corresponding to scenario 1, and I don't have Reference Number available, I want to remove the reference number part completely on the fly (not in database) i.e. I want something like this:
Scenario 3: We have opened Ticket XXX for your call on XXX. Kindly call us at XXX to track progress.
The formats for scenarios 1 and 2 are stored in the database as XSLT corresponding to the strings you see above. Format for Scenario 3 has to be produced on the fly. The XXX part has to be replaced with actual data. This data is available in an XML serializable object that I have.
I want to serialize this object, produce an XML in memory, modify the XSLT a little (on the fly) to reflect the data I have, transform the XML in memory to HTML using the XSLT for the scenario and then pass the HTML as a string parameter to an email method I have. The email part works. I need to work on the Object->XML in memory->Slight XSLT modification-> HTML using appropriate XSLT.
I would appreciate if you can include code examples and not just the approach I need to follow.
EDIT:
Here is the working code:
using (xsltStream = new MemoryStream(emailInfo.Body))
{
// Create an XmlReader from the Stream
XmlReader reader = XmlReader.Create(xsltStream);
// Create and load the transform with document function enabled.
XslCompiledTransform transform = new XslCompiledTransform();
XsltSettings settings = new XsltSettings();
settings.EnableDocumentFunction = true;
transform.Load(reader, settings, null);
// Execute the transformation.
transform.Transform(doc, writer);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想我更倾向于将格式存储为 XML 而不是 XSLT:
然后,您使用标准样式表来转换此文档,使用来自另一个文档的数据。
话虽如此,我只是推荐这个,因为我自己按照你描述的方式做了,并且很遗憾,因为做出改变或引入新的变化太困难了。
I think I would be quite inclined to store the formats as XML rather than as XSLT:
Then you use a standard stylesheet to transform this document, using data from another document.
Having said this, I only recommend this because I did it myself the way you are describing and regret it, as it's too difficult to make changes or introduce new variations.
根据 @harpo、@Alexei Levenkov 和 @Alejandro 的评论,我能够制定出使用多个模板的代码的工作版本。由于我无法将评论标记为答案,因此我会将其标记为答案并在我的问题中添加代码。
Based upon comments from @harpo, @Alexei Levenkov and @Alejandro, I was able to work out a working version of the code which uses multiple templates. Since I can't mark the comments as answers, I will mark this as answer and add the code in my question.