使用XSLT将XML转换成HTML

时间:2022-06-11 21:49:24

I saw several questions related to XML, XSLT and HTML on *, but I beleive my question is a little different. Here is what I need:

我在*上看到了几个关于XML、XSLT和HTML的问题,但我想我的问题有点不同。这是我需要的:

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.

场景一:我们已经为您开通了XXX号机票/ XXX号的XXX号电话。请在XXX打电话给我们跟踪进度。

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.

场景二:感谢您对我们的产品XXX感兴趣。如所讨论的,我们将派我们的代表到你位于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:

此外,可能需要根据数据可用性对格式进行一些修改。例如,如果我需要发送与场景1对应的电子邮件,并且我没有可用的参考编号,我想要在动态(而不是数据库中)完全删除参考编号部分,也就是说,我想要这样的东西:

Scenario 3: We have opened Ticket XXX for your call on XXX. Kindly call us at XXX to track progress.

场景三:我们已经为你在XXX打电话开了XXX机票。请在XXX打电话给我们跟踪进度。

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.

场景1和场景2的格式作为与上面看到的字符串对应的XSLT存储在数据库中。场景3的格式必须动态生成。XXX部分必须用实际数据代替。此数据可在我拥有的XML序列化对象中使用。

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.

我想这个对象序列化,在内存中生成XML,修改XSLT一点(动态)来反映数据,内存中的XML转换成HTML使用XSLT的场景,然后通过HTML电子邮件作为字符串参数的方法。电子邮件的部分作品。我需要在内存中使用对象->XML,使用适当的XSLT对> HTML进行简单的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);
             }

2 个解决方案

#1


0  

The formats for scenarios 1 and 2 are stored in the database as XSLT

场景1和场景2的格式作为XSLT存储在数据库中

I think I would be quite inclined to store the formats as XML rather than as XSLT:

我认为我更倾向于将格式存储为XML而不是XSLT:

<message>Thanks for your interest in our Product <product/>. As discussed we will send our representative on <date/> to your office located at <officeLocation/>.</message>

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.

说到这里,我只推荐这一点,因为我是按照您描述的方式来做的,并为此感到遗憾,因为进行更改或引入新的变体太难了。

#2


0  

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.

根据@harpo、@Alexei Levenkov和@Alejandro的评论,我设计出了一个使用多个模板的代码工作版本。由于我不能将注释标记为答案,我将标记为答案,并在我的问题中添加代码。

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);
             }

#1


0  

The formats for scenarios 1 and 2 are stored in the database as XSLT

场景1和场景2的格式作为XSLT存储在数据库中

I think I would be quite inclined to store the formats as XML rather than as XSLT:

我认为我更倾向于将格式存储为XML而不是XSLT:

<message>Thanks for your interest in our Product <product/>. As discussed we will send our representative on <date/> to your office located at <officeLocation/>.</message>

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.

说到这里,我只推荐这一点,因为我是按照您描述的方式来做的,并为此感到遗憾,因为进行更改或引入新的变体太难了。

#2


0  

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.

根据@harpo、@Alexei Levenkov和@Alejandro的评论,我设计出了一个使用多个模板的代码工作版本。由于我不能将注释标记为答案,我将标记为答案,并在我的问题中添加代码。

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);
             }