我使用的是 Win7 并将 VSCODE# 项目设置为 .NET Framework 4。
然后下载 SaxonHE9-8-0-7N-setup.exe 并安装。
然后将 saxon9he-api.dll 引用到 C# 项目并using Saxon.Api;
这是我的program.cs
:
static void Main(string[] args)
{
var xslt = new FileInfo(Path.GetFullPath(Path.Combine(Environment.CurrentDirectory.ToString(), @"..\..\..")) + @"\TEST.xslt");
var input = new FileInfo(Path.GetFullPath(Path.Combine(Environment.CurrentDirectory.ToString(), @"..\..\..")) + @"\TEST.xml");
var output = new FileInfo(Path.GetFullPath(Path.Combine(Environment.CurrentDirectory.ToString(), @"..\..\..")) + @"\result.txt");
var processor = new Processor();
var compiler = processor.NewXsltCompiler();
var executable = compiler.Compile(new Uri(xslt.FullName));
var transformer = executable.Load();
var serializer = new Serializer();
FileStream outStream = new FileStream(output.ToString(), FileMode.Create, FileAccess.Write);
serializer.SetOutputStream(outStream);
using (var inputStream = input.OpenRead())
{
transformer.SetInputStream(inputStream, new Uri(Path.GetTempPath()));
transformer.SetParameter(new QName("password"), new XdmAtomicValue("secret"));
transformer.Run(serializer);
outStream.Close();
}
}
这是我的TEST.xslt
:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:math="http://www.w3.org/2005/xpath-functions/math"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
xmlns:array="http://www.w3.org/2005/xpath-functions/array"
exclude-result-prefixes="xs math map array"
version="3.0">
<xsl:output method="json" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="root">
<xsl:map>
<xsl:map-entry key="local-name()">
<xsl:apply-templates/>
</xsl:map-entry>
</xsl:map>
</xsl:template>
<xsl:template match="items">
<xsl:variable name="items" as="item()*">
<xsl:apply-templates/>
</xsl:variable>
<xsl:sequence select="map { local-name() : array { $items }}"/>
</xsl:template>
<xsl:template match="item">
<xsl:sequence select="map { 'foo' : xs:integer(foo), 'bar' : string(bar) }"/>
</xsl:template>
</xsl:stylesheet>
在运行之前我收到两条错误消息:
命名空间 ' 中的元素 'template'http://www.w3.org/1999/XSL/Transform http://www.w3.org/1999/XSL/Transform' 具有无效的子元素 'map'
and
未声明“as”属性。
运行时我收到一条错误消息:
xsl:map-entry/@key TEST.xslt:FOTY0013 中出现错误:无法在未命名模式下将功能项写入 /root 的内置模板规则中的 XML 树**
那么我应该怎么做才能运行这段代码而不出错呢?
更改创建 Transformer 的行以创建Xslt30变压器 https://www.saxonica.com/html/documentation/dotnetdoc/Saxon/Api/Xslt30Transformer.html
var transformer = executable.Load30();
使用 XSLT 3 及其各种不同且更灵活的输入和输出选项,然后运行样式表使用
using (var inputStream = input.OpenRead())
{
transformer.ApplyTemplates(inputStream, serializer);
outStream.Close();
}
这样,我之前发布的、您用作示例的 XSLT 代码就可以正常运行(我必须调整文件路径,但这显然取决于文件与 C# 项目的关系方式/位置)。
请注意,一般来说,对于 XSLT 3,初始匹配选择和用于初始化全局变量的全局上下文项可能不同,示例样式表没有任何全局变量或参数,但如果有的话,您还需要设置 GlobalContextItem (https://www.saxonica.com/html/documentation/dotnetdoc/Saxon/Api/Xslt30Transformer.html#GlobalContextItem https://www.saxonica.com/html/documentation/dotnetdoc/Saxon/Api/Xslt30Transformer.html#GlobalContextItem) 到 XdmValue。
至于在 Visual Studio 中编辑 XSLT 3 时收到的各种编辑器警告或错误消息,好吧,仅在系统上安装 XSLT 3 处理器不会将 VS 转换为 XSLT 3 编辑器,您需要检查是否/如何可以设置 Visual Studio 以使用 XSLT 3 架构,XSLT 3 规范有一个链接:https://www.w3.org/TR/xslt-30/schema-for-xslt30.xsd https://www.w3.org/TR/xslt-30/schema-for-xslt30.xsd但我认为它使用模式语言 1.1,而 Microsoft 仅支持模式语言版本 1.0,因此找到并安装 VS 的模式以使其识别并支持 XSLT 3 编辑可能有点困难。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)