我有一个XML文件和一个XSLT,我想使用XSLT将XML转换成HTML字符串,然后可以将其加载到中TWebBrowser
。
对于我的测试,我正在使用这些示例文件。
XML档案:
<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>Light Belgian waffles covered with strawberries and whipped cream</description>
<calories>900</calories>
</food>
<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
<calories>900</calories>
</food>
<food>
<name>French Toast</name>
<price>$4.50</price>
<description>Thick slices made from our homemade sourdough bread</description>
<calories>600</calories>
</food>
<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
<calories>950</calories>
</food>
</breakfast_menu>
XSLT文件:
<?xml version="1.0" encoding="UTF-8"?>
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<xsl:for-each select="breakfast_menu/food">
<div style="background-color:teal;color:white;padding:4px">
<span style="font-weight:bold"><xsl:value-of select="name"/> - </span>
<xsl:value-of select="price"/>
</div>
<div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
<p>
<xsl:value-of select="description"/>
<span style="font-style:italic"> (<xsl:value-of select="calories"/> calories per serving)</span>
</p>
</div>
</xsl:for-each>
</body>
</html>
尝试1:
我找到了此解决方案并尝试了该功能:
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;
但是会产生意外的输出:
比利时华夫饼5.95美元,我们有两个著名的比利时华夫饼,有大量的真正的枫糖浆650草莓比利时华夫饼$ 7.95轻质比利时华夫饼,上面覆盖有草莓和鲜奶油900浆果-莓果比利时华夫饼$ 8.95轻质比利时华夫饼上覆盖着各种新鲜的浆果和鲜奶油4,900法语吐司$ 0来自我们的自制酸面包600欧陆式早餐$ 6.95两个鸡蛋,培根或香肠,烤面包和我们倍受欢迎的马铃薯煎饼950
尝试2:
我在此页面的“使用MSXML分析器/转换引擎”部分中找到了以下功能(注意:这是一个Delphi 5示例,但我使用的是Delphi 2007)。
function DoTransform(const xml, xsl : string ): string;
var
XMLDoc : IXMLDOMDocument;
XSLDoc : IXMLDOMDocument;
Template : IXSLTemplate;
Processor : IXSLProcessor;
begin
Result := '';
try
XMLDoc := CoFreeThreadedDOMDocument30.Create;
XSLDoc := CoFreeThreadedDOMDocument30.Create;
XMLDoc.load(xml);
XSLDoc.load(xsl);
Template := CoXSLTemplate30.Create;
Template.stylesheet := XSLDoc;
Processor := Template.createProcessor;
Processor.input := XMLDoc;
Processor.transform;
result := Processor.output;
finally
XMLDoc := nil;
XSLDoc := nil;
end;
end;
我已经导入了Microsoft XML类型库...
组件->导入组件->导入类型库->下一步-> Microsoft XML,v6.0
...并添加MSXML2_TLB
到uses子句中,但随后发生其他一些错误:
E2003未声明的标识符:“ CoFreeThreadedDOMDocument30”
E2003未声明的标识符:“ CoXSLTemplate30”
我已经切换CoFreeThreadedDOMDocument30
到CoFreeThreadedDOMDocument60
和CoXSLTemplate30
到CoXSLTemplate60
现在能顺利完成编译。
在运行时,在此行:
Template.stylesheet := XSLDoc;
它将引发以下异常(意大利语):
样式表不包含文档元素。样式表为空,或者它可能是格式不正确的XML文档。
用英语应该是这样的:
样式表不包含document元素。样式表为空,或者它可能是格式错误的XML文档。
我已经对其他示例文件进行了测试,并且错误始终是相同的,并且我不知道这可能是问题所在。
我是在正确的方法上吗,还是有更好的方法来做我需要的事情?
这是我在Delphi 2007和Delphi XE7上测试过的解决方案。
uses
msxml;
function Transform(const AXMLContent : string; const AXSLContent : string) : string;
var
XML : IXMLDOMDocument;
XSL : IXMLDOMDocument;
begin
XML := CoDOMDocument.Create;
XML.loadXML(AXMLContent);
XSL := CoDOMDocument.Create;
XSL.loadXML(AXSLContent);
Result := XML.TransformNode(XSL);
end;
产生的HTML代码:
<html>
<body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
<div style="background-color:teal;color:white;padding:4px"><span style="font-weight:bold">Belgian Waffles - </span>$5.95</div>
<div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
<p>Two of our famous Belgian Waffles with plenty of real maple syrup<span style="font-style:italic"> (650 calories per serving)</span></p>
</div>
<div style="background-color:teal;color:white;padding:4px"><span style="font-weight:bold">Strawberry Belgian Waffles - </span>$7.95</div>
<div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
<p>Light Belgian waffles covered with strawberries and whipped cream<span style="font-style:italic"> (900 calories per serving)</span></p>
</div>
<div style="background-color:teal;color:white;padding:4px"><span style="font-weight:bold">Berry-Berry Belgian Waffles - </span>$8.95</div>
<div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
<p>Light Belgian waffles covered with an assortment of fresh berries and whipped cream<span style="font-style:italic"> (900 calories per serving)</span></p>
</div>
<div style="background-color:teal;color:white;padding:4px"><span style="font-weight:bold">French Toast - </span>$4.50</div>
<div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
<p>Thick slices made from our homemade sourdough bread<span style="font-style:italic"> (600 calories per serving)</span></p>
</div>
<div style="background-color:teal;color:white;padding:4px"><span style="font-weight:bold">Homestyle Breakfast - </span>$6.95</div>
<div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
<p>Two eggs, bacon or sausage, toast, and our ever-popular hash browns<span style="font-style:italic"> (950 calories per serving)</span></p>
</div>
</body>
</html>
为了将结果字符串显示为TWebBrowser
I,我使用了以下代码:
procedure LoadHTMLCode(AWebBrowser : TWebBrowser; const AHTMLCode: string);
var
Doc: Variant;
begin
if not Assigned(AWebBrowser.Document) then
AWebBrowser.Navigate('about:blank');
Doc := AWebBrowser.Document;
Doc.Clear;
Doc.Write(AHTMLCode);
Doc.Close;
end;
...
var
XMLContent : string;
XLSContent : string;
HTMLCode : string;
begin
//loading XML content
XMLContent := ...;
//loading XLS content
XLSContent := ...;
//transforming
HTMLCode := Transform(XMLContent, XLSContent);
//displaying
LoadHTMLCode(WebBrowser1, HTMLCode);
end;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句