saxon - 如何使用 Saxon API 从模式感知 xslt 获取警告
问题描述
我有一些编译然后执行 XSLT 的 c# 原型代码。
var samplesDir = new Uri(AppDomain.CurrentDomain.BaseDirectory);
// Create a Processor instance.
Processor processor = new Processor(true);
var compiler = processor.NewXsltCompiler();
try
{
// Create a transformer for the stylesheet.
Xslt30Transformer transformer = compiler.Compile(new Uri(samplesDir, "po.xsl")).Load30();
// Create a serializer, with output to the standard output stream
Serializer serializer = processor.NewSerializer();
serializer.SetOutputWriter(Console.Out);
// Transform the source XML and serialize the result document
transformer.SchemaValidationMode = SchemaValidationMode.Strict;
transformer.ApplyTemplates(File.OpenRead("po.xml"), serializer);
}
catch (Exception e)
{
Console.WriteLine(e);
Console.WriteLine("Schema compilation failed with " + compiler.ErrorList.Count + " errors");
foreach (StaticError error in compiler.ErrorList)
{
Console.WriteLine("At line " + error.LineNumber + ": " + error.Message);
}
}
和一个 XSLT 文件
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="text" />
<xsl:import-schema schema-location="file:///C:/Users/m_r_n/source/repos/SaxonEEExample/ValidateXslt/po1.xsd" />
<xsl:template match="schema-element(PurchaseOrder)">
<xsl:for-each select="item">
<xsl:value-of select="@id" />: <xsl:value-of select="title" />
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
<xsl:template match="*">
<xsl:message terminate="yes">Source document is not a purchase order
</xsl:message>
</xsl:template>
</xsl:stylesheet>
如果我将比赛更改为
<xsl:template match="schema-element(PurchaseOrder1)">
那么这将正确报告错误
At line 8: There is no declaration for element <PurchaseOrder1> in an imported schema
杰出的。
如果我将其改回并将 for-each 行修改为
<xsl:for-each select="item1">
然后是氧气编辑器(使用 Saxon EE),正确报告此行的警告
The complex type of element PurchaseOrder does not allow a child element named item1
但是我的 C# 代码会很高兴地编译并执行它(输出一个空字符串)。
如何让撒克逊编译器报告警告?Oxygen 做了什么我的代码没有做的事情?
解决方案
Saxon 可能正在向控制台输出警告,而您看不到控制台上的内容。
尝试通过ErrorList
设置.ErrorReporter
XsltCompiler
推荐阅读
- terminal - 如何解决“找不到带有 BLAS API 的必需库”的问题?
- javascript - 在 HTML 输出中强制 CSS Hexcode
- c# - 使用 javascript 在对象设计中定位元素
- c# - 如何在asp.net core razor页面中实现分页
- url - Activemq 代理地址
- php - 使用 laravelcollective/html 组件比使用常规的简单 HTML 标签构建视图元素有什么优势?
- symfony - 如何在 symfony 3.4 中管理多个防火墙
- protractor - 是否可以对“夜间构建”进行单独测试
- ionic-framework - 如何在 Ionic 中禁用深度链接器?
- angular - 在Angular中正确取消订阅帖子/放置/修补/删除