简体   繁体   English

如何在Delphi应用程序中使用XSLT文档显示XML?

[英]How do I display XML using an XSLT document, in a Delphi app?

I've been given a sample XML file (ultimately my client will receive several of these each day), and an XSLT file which will apparently transform the XML into something with a meaningful layout, suitable for displaying in a browser or printing. 我已经获得了一个示例XML文件(最终我的客户端每天将收到其中的几个),以及一个XSLT文件,它显然会将XML转换为具有有意义布局的内容,适合在浏览器中显示或打印。

I'd like to put something into an existing legacy Delphi app, such that the user can pick one of the XML files from the drive and 'display' it (in a TWebBrowser or similar). 我想在现有的旧版Delphi应用程序中添加一些内容,以便用户可以从驱动器中选择一个XML文件并“显示”它(在TWebBrowser或类似文件中)。

I have no idea where to start, and Googling delphi, xml and xslt gets me examples that seem to be datamodule/database based; 我不知道从哪里开始,谷歌搜索delphi,xml和xslt给我看来似乎是基于数据模块/数据库的例子; I just want to transform a given XML file into something on-screen. 我只想将给定的XML文件转换为屏幕上的内容。 It looks like it might involve a PageProducer but I can't quite get my head around what I need to do. 看起来它可能涉及到PageProducer,但我无法理解我需要做的事情。 Anyone offer any pointers, or maybe a link to a quick tutorial? 有人提供任何指针,或者可能是快速教程的链接吗?

Many thanks! 非常感谢!

Uses
  XMLDoc, XMLIntf;

function Transform(XMLContent : string; XSLContent : string) : WideString;
var
  XML : IXMLDocument;
  XSL : IXMLDocument;
begin

  XML := LoadXMLData(XMLContent);
  XSL := LoadXMLData(XSLContent);

  XML.DocumentElement.TransformNode(XSL.DocumentElement, Result)

end;

I used an MSXML library to do the XSLT transformation in Delphi. 我使用MSXML库在Delphi中进行XSLT转换。 It was a long time ago. 很久以前了。 Worked like a charm! 像魅力一样工作!

I'm not sure what output format your XSLT will generate, but knowing that will help you figure out how to display it. 我不确定你的XSLT会产生什么样的输出格式,但知道这会帮助你弄清楚如何显示它。 We generated HTML from XML via XSLT, and displayed it using an ActiveX Web Browser (IE) control on a pane in our application. 我们通过XSLT从XML生成HTML,并在我们的应用程序的窗格上使用ActiveX Web浏览器(IE)控件显示它。

Here's a link on MSSXML and Delphi that might help. 这是MSSXML和Delphi上可能有用链接

BTW: If this is your first time working with XSLT, you can manually edit an XML file, and add in a directive to get it to display using a specific XSLT. 顺便说一句:如果这是您第一次使用XSLT,则可以手动编辑XML文件,并添加指令以使用特定的XSLT进行显示。 When you open the XML in Firefox after the edit, the XSLT will be applied, and it will show you what will be the output of your MSXML calls. 在编辑后在Firefox中打开XML时,将应用XSLT,它将显示MSXML调用的输出。 Here's the line you add manually to the xml: 这是您手动添加到xml的行:

<?xml-stylesheet type="text/xsl" href="myStyleSheet.xsl"?>

If you need only transformations, you can use TXMLDocument , save to disk and than display the result in a TWebBrowser (via Navigate('file:///...') ). 如果只需要转换,可以使用TXMLDocument ,保存到磁盘,然后在TWebBrowser显示结果(通过Navigate('file:///...') )。

Personally I had some problems with MSXML so I started to use DIXML . 我个人遇到了MSXML的一些问题,所以我开始使用DIXML

You can try install the AltovaXML, it can be call as a COM+. 您可以尝试安装AltovaXML,它可以作为COM +调用。 It is free and can be download from: http://www.altova.com/altovaxml.html 它是免费的,可以从http://www.altova.com/altovaxml.html下载

MsXml can only works with xslt 1.0 but AltovaXml can work with xslt 2.0 MsXml只能用于xslt 1.0,但AltovaXml可以用于xslt 2.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM