使用Delphi XE8进行XSLT转换的替代方法 [英] Alternatives for XSLT transformations with Delphi XE8

查看:132
本文介绍了使用Delphi XE8进行XSLT转换的替代方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用Delphi XE8执行XSLT转换,并且遇到了问题。我的XSL文件使用导入语句引用外部XSL文件,该导入语句的href属性具有相对路径。转换失败,错误为命名模板'skrivUtDate'未出现在样式表中。定义 skrivUtDate位于外部XSL文件中。定义如下所示:

I am trying to perform an XSLT transformation with Delphi XE8 and am running into problems. My XSL file refers to an external XSL file using an import statement whose href attribute has a relative path. The transform fails with the error "Named template 'skrivUtDate' does not appear in the stylesheet." The definition 'skrivUtDate' is located in the external XSL file. The definition looks like the following:

<xsl:template name="skrivUtDate">
    <xsl:param name="oppgittTid"/>
    <xsl:if test="string-length($oppgittTid)!=0">
        <xsl:value-of select="substring($oppgittTid,9,2)"/>.<xsl:value-of select="substring($oppgittTid,6,2)"/>.<xsl:value-of select="substring($oppgittTid,3,2)"/>
    </xsl:if>
</xsl:template>

我已将外部XSL文件放在适当的目录中(相对于原始XSL文件),但我得到相同的错误(我也尝试过此外部文件的所有其他可能的目录,但没有一个起作用)。但是,我可以使用EditX XML Editor执行转换,因此我认为XSL无效(请注意,此XSL文件是由第三方提供的,并且显然,某些人使用此开发平台而非

I have placed the external XSL file in the appropriate directory (relative to the original XSL file) but I get the same error (I have also tried all other conceivable directories for this external file, but none have worked). However, I can perform the transformation using EditX XML Editor, so I do not think that the XSL is invalid (Note that this XSL file was provided by a third party, and apparently it is being used successfully by some folks using development platforms other than Delphi).

以下是XSL文件的开头部分:

Here is the opening segment of the XSL file:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
  xmlns:ref="http://www.kith.no/xmlstds/henvisning/2012-02-15" 
  xmlns:xhtml="http://www.w3.org/1999/xhtml" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:po="http://www.kith.no/xmlstds/po/poKomponent/2009-06-30" 
  xmlns:fk1="http://www.kith.no/xmlstds/felleskomponent1" 
  exclude-result-prefixes="ref xhtml po fk1">
    <xsl:import href="../../Felleskomponenter/funksjoner.xsl"/>

是的,funksjoner.xsl文件位于名为Felleskomponenter的目录中。

And, yes, the funksjoner.xsl file is in a directory named Felleskomponenter.

我非常简单的测试使用类似于以下伪代码的东西:

My very simple test uses something similar to the following pseudo code:

var
  XML : IXMLDocument;
  XSL : IXMLDocument;
  s: WideString;
begin
  XML := TXMLDocument.Create(self);
  XML.FileName := 'C:\somepath\some.xml';
  XML.Active := True;
  XSL := TXMLDocument.Create(Self);
  XSL.FileName := 'C:\someotherpath\somefile.xsl';
  XSL.Active := True;
  XML.DocumentElement.TransformNode(XSL.DocumentElement, s);

我知道Delphi XE7开始与Omni XML DOM以及ADOM一起交付,但是这些都是Delphi XE8显然不存在。我希望使用其他文档对象模型可以解决此问题。

I know that Delphi XE7 started shipping with the Omni XML DOM as well as the ADOM, but those are apparently absent from Delphi XE8. I was hoping that using a different document object model might address the problem.

XML和XSL一样,XML相当复杂。

The XML is pretty complicated, as is the XSL.

是否有人对使用Delphi XE8执行XSLT转换的替代方法有任何建议?

Does any body have any suggestions for alternative ways to perform XSLT transformations with Delphi XE8?

推荐答案

为使XSLT在Delphi中正常运行,您需要使用MSXML来加载XML和XSL文件。这篇文章提供了一个 n有关如何在Delphi中使用MSXML的示例 TransformNode 在后台使用MSXML。

For XSLT to work properly in Delp you need to use MSXML to load the XML and XSL files. This post gives an example on how to use MSXML with Delphi. The TransformNode uses MSXML under the hood.

以下内容应该可以正常工作(从此处,不完整的代码段):

Something along the following should work (taken from here, incomplete snippet):

var
  xmlEmployees, xslStyle : IXMLDOMDocument;
begin
  xmlEmployees := CoDOMDocument.Create;
  xslStyle     := CoDOMDocument.Create;
  xmlEmployees.load('employees.xml');
  xslStyle.load('empuksna.xsl');
  result := xmlEmployees.transformNode(xslStyle);
end; 

同样,如果要通过字符串加载XML和XSL,则原始位置会丢失。大多数处理器支持设置基本uri,但它看起来像 IXSLProcessor 没有这种选择。

Likewise, if you were to load your XML and XSL through a string, the original location gets lost. Most processors support setting the base uri, but it looks like IXSLProcessor does not have such option.

您还要求其他选择。 DIXML支持libxslt,在某些方面它要比MSXML更好(尽管如果您希望与.NET互操作,则不能超过.NET的XSLT,后者是另一种选择)。 DIXML软件包随附了许多演示和示例,演示文件夹。

You also ask for an alternative. DIXML support libxslt which is in quite some respects superior to MSXML (though not to .NET's XSLT, which is another alternative, if you like interop'ing with .NET). The package of DIXML comes with a bunch of demo's and examples in the demo folder.

如果可以切换到.NET,则 DrDobb的这篇文章(相当古老)展示了如何使用.NET和Delphi进行XSLT ,还展示了转换的另一种方法(尽管它假定使用Delphi 7,但不确定它是否仍然适用)。

If you can switch to .NET, this (rather old) DrDobb's article shows how to do XSLT with .NET and Delphi and also shows yet another alternative for a transformation (though it assumed Delphi 7, not sure it still applies).

这篇关于使用Delphi XE8进行XSLT转换的替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆