简体   繁体   English

如何使用 XSLT 转换 XML?

[英]How to transform an XML using an XSLT?

I have an XML file and an XSLT and I want to use the XSLT to trasform the XML into an HTML string which could be loaded into a TWebBrowser .我有一个 XML 文件和一个 XSLT,我想使用 XSLT 将 XML 转换为可以加载到TWebBrowser的 HTML 字符串。

For my tests, I'm using these example files.对于我的测试,我使用了这些示例文件。

XML File: 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 File: 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>

Attempt 1:尝试 1:

I've found this solution and tried the function:我找到了这个解决方案并尝试了这个功能:

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;

But it produces an unexpected output:但它产生了意想不到的输出:

Belgian Waffles$5.95Two of our famous Belgian Waffles with plenty of real maple syrup650Strawberry Belgian Waffles$7.95Light Belgian waffles covered with strawberries and whipped cream900Berry-Berry Belgian Waffles$8.95Light Belgian waffles covered with an assortment of fresh berries and whipped cream900French Toast$4.50Thick slices made from our homemade sourdough bread600Homestyle Breakfast$6.95Two eggs, bacon or sausage, toast, and our ever-popular hash browns950比利时华夫饼 5.95 美元两个我们著名的比利时华夫饼配大量真正的枫糖浆 650 草莓比利时华夫饼 7.95 美元覆盖草莓和鲜奶油的轻比利时华夫饼 900 浆果浆果比利时华夫饼 8.95 美元轻比利时华夫饼,上面覆盖着各种奶油和新鲜草莓切片 905 美元来自我们自制的酸面包600家庭式早餐6.95美元两个鸡蛋、培根或香肠、吐司和我们广受欢迎的土豆煎饼950


Attempt 2:尝试 2:

I've found the following function in the "Using the MSXML Parser/ Transform Engine" section of this page (Note: it's a Delphi 5 example but I'm using Delphi 2007).我发现的“使用MSXML分析器/转换引擎”一节中的以下功能页面(注:这是一个Delphi 5的例子,但我使用德尔福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;

I've imported the Microsoft XML type library...我已经导入了 Microsoft XML 类型库...

Component -> Import Component -> Import a Type Library -> Next -> Microsoft XML, v6.0组件 -> 导入组件 -> 导入类型库 -> 下一步 -> Microsoft XML,v6.0

...and added MSXML2_TLB to the uses clause but then some other errors occours: ...并将MSXML2_TLB添加到 uses 子句中,但随后出现了一些其他错误:

E2003 Undeclared identifier: 'CoFreeThreadedDOMDocument30' E2003 未声明的标识符:'CoFreeThreadedDOMDocument30'

E2003 Undeclared identifier: 'CoXSLTemplate30' E2003 未声明的标识符:'CoXSLTemplate30'

I've switched CoFreeThreadedDOMDocument30 to CoFreeThreadedDOMDocument60 and CoXSLTemplate30 to CoXSLTemplate60 and now it compiles without errors.我已经将CoFreeThreadedDOMDocument30切换到CoFreeThreadedDOMDocument60并将CoXSLTemplate30切换到CoXSLTemplate60 ,现在它可以编译没有错误。

At runtime, at this line:在运行时,在这一行:

Template.stylesheet := XSLDoc;

It raises the following exception (In italian):它引发以下异常(意大利语):

Il foglio di stile non include un elemento documento. Il Foglio di stile non include un elemento documento。 Il foglio di stile è vuoto oppure potrebbe essere un documento XML in formato non corretto. Il foxlio di stile è vuoto oppure potrebbe essere un documento XML in formato non corretto。

In english it should be something like this:用英语应该是这样的:

The stylesheet doesn't include a document element.样式表不包含文档元素。 The stylesheet is empty or it could be a bad formatted XML document.样式表为空,或者它可能是格式错误的 XML 文档。

I've tested with other example files and the error is always the same and I don't understand which could be the problem.我已经用其他示例文件测试过,错误总是一样的,我不明白这可能是问题所在。

Am I on the right way or are there better ways to do what I need?我是在正确的道路上还是有更好的方法来做我需要的事情?

I have the following code that works.我有以下有效的代码。 It is assuming a file containing the XSLT but that should be easy to change.它假设一个包含 XSLT 的文件,但应该很容易更改。

It was written a long time ago and I was in a hurry.这是很久以前写的,我很着急。 So there might be room for some improvements.所以可能会有一些改进的空间。 Hope it helps.希望能帮助到你。

uses
  Windows, ComObj, XMLDoc, msxmldom, msxml;

function DOMToMSDom(const Doc: IDOMDocument): IXMLDOMDocument3;
begin
  Result := ((Doc as IXMLDOMNodeRef).GetXMLDOMNode as IXMLDOMDocument3);
end;

function TransformXMLDocWithXSLTFile(const Doc: XMLIntf.IXMLDocument; const StyleSheetLocation: string; var TransformedData, Error: string): Boolean;
var
  MsxmlDoc: IXMLDOMDocument3;
  xslStyle : IXMLDOMDocument;
begin
  Result := False;
  if not FileExists(StyleSheetLocation) then 
  begin
    Error := 'Specified XSLT stylesheet file does not exist: ' + StyleSheetLocation;
    Exit;
  end;
  try
    MsxmlDoc := DOMToMSDom(Doc.DOMDocument);
    xslStyle := CoDOMDocument60.Create;
    xslStyle.load(StyleSheetLocation);
    IXMLDOMDocument3(xslStyle).setProperty('AllowXsltScript', True);
    TransformedData := MsxmlDoc.transformNode(xslStyle);
    Result := True;
  except
    on E: Exception do 
    begin
      Error := E.Message;
    end;
  end;
end;

There are a few problems;有几个问题;

Your XSL doesn't have a stylesheet, output or root template.您的 XSL 没有样式表、输出或根模板。 It needs to be like this:它需要是这样的:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:output method="html" encoding="UTF-8" />

    <xsl:template match="/">
        <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>
    </xsl:template>
</xsl:stylesheet>

In your delphi code, because you are loading strings, so you should use .loadXML, not .load在您的 delphi 代码中,因为您正在加载字符串,所以您应该使用 .loadXML,而不是 .load

    XMLDoc := CoFreeThreadedDOMDocument60.Create;
    XSLDoc := CoFreeThreadedDOMDocument60.Create;
    XMLDoc.loadXML(xml);
    XSLDoc.loadXML(xsl);
    
    Template := CoXSLTemplate60.Create;
    Template.stylesheet := XSLDoc;
    Processor := Template.createProcessor;
    Processor.input := XMLDoc;
    Processor.transform;
    s :=  Processor.output;

After doing .load or .loadXML you look at the parseerror to check whats going on (if you are having problems)执行 .load 或 .loadXML 后,您会查看 parseerror 以检查发生了什么(如果您遇到问题)

xsldoc.parseError.errorCode;  //will be 0 if all is well
xsldoc.parseError.reason; 

This is my solution tested on Delphi 2007 and Delphi XE7 .这是我在Delphi 2007Delphi 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;

生成的样式化 xml 的图片

Resulting HTML code:生成的 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>

For displaying the resulting string into a TWebBrowser I've used the following code:为了将结果字符串显示到TWebBrowser我使用了以下代码:

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;

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

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