【发布时间】:2013-04-12 15:29:54
【问题描述】:
我正在使用 Saxon 9.4 家庭版 (Saxon-HE 9.4 .NET) 来获得对 XSLT 2.0 和 XPath 2.0 以及 .NET 中的 XQuery 1.0 的支持。当我加载没有 URI 的文件时,我的代码崩溃了。
- 是否可以在没有与所加载文档相关的 URI 的情况下加载 xml/xsl 文档?
- 如果没有,有没有办法为嵌入在 dll 文件中的元素定义 URI?
任何其他解决方案也将不胜感激,我唯一的说法是文件必须从 dll 文件中加载。
只要我从文件中加载 xml/xsl,我的代码就可以完美运行:
const string sourcePath = @"C:\test\TestInvoiceWithError.xml";
const string xsltpath = @"C:\test\UBL-T10-BiiRules.xsl";
当我尝试从嵌入式资源加载时,代码会抛出异常声明'未提供基本 URI':
Stream sourceStream = GetEmbeddedResource("TestProject1.testfiles.TestInvoice.xml");
Stream xsltStream = GetEmbeddedResource("TestProject1.testfiles.UBL-T10-BiiRules.xsl");
我还为具有引发异常的相对路径的资源创建了 Uri。'相对 URI 不支持此操作。':
Uri sourceUri = new Uri("/TestProject1;component/testfiles/TestInvoice.xml", UriKind.Relative);
Uri xsltUri = new Uri("/TestProject1;component/testfiles/UBL-T10-BiiRules.xsl.xml", UriKind.Relative);
这是我的代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Xml;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Saxon.Api;
namespace TestProject1
{
[TestClass]
public class XsltTest
{
[TestMethod]
public void SaxonTest()
{
Stream sourceStream = GetEmbeddedResource("TestProject1.testfiles.TestInvoice.xml");
Stream xsltStream = GetEmbeddedResource("TestProject1.testfiles.UBL-T10-BiiRules.xsl");
Uri sourceUri = new Uri("/TestProject1;component/testfiles/TestInvoice.xml", UriKind.Relative);
Uri xsltUri = new Uri("/TestProject1;component/testfiles/UBL-T10-BiiRules.xsl.xml", UriKind.Relative);
const string sourcePath = @"C:\test\TestInvoiceWithError.xml";
const string xsltpath = @"C:\test\UBL-T10-BiiRules.xsl";
Processor processor = new Processor();
XdmNode input = processor.NewDocumentBuilder().Build(new Uri(sourcePath));
XsltTransformer transformer = processor.NewXsltCompiler().Compile(new Uri(xsltpath)).Load();
transformer.InitialContextNode = input;
Serializer serializer = new Serializer();
StringBuilder sb = new StringBuilder();
TextWriter writer = new StringWriter(sb);
serializer.SetOutputWriter(writer);
transformer.Run(serializer);
XmlDocument xmlDocOut = new XmlDocument();
xmlDocOut.LoadXml(sb.ToString());
XmlNodeList failedAsserts = xmlDocOut.SelectNodes("/svrl:schematron-output/svrl:failed-assert",XmlInvoiceNamespaceManager());
if (failedAsserts == null)
return;
foreach (XmlNode failedAssert in failedAsserts)
{
if (failedAssert.Attributes == null)
continue;
XmlAttribute typeOfError = failedAssert.Attributes["flag"];
if (typeOfError.Value.Equals("warning"))
{/*Log something*/}
else if (typeOfError.Value.Equals("fatal"))
{/*Log something*/}
}
}
private XmlNamespaceManager XmlInvoiceNamespaceManager()
{
IDictionary<string, string> list = new Dictionary<string, string>
{
{"xml", "http://www.w3.org/XML/1998/namespace"},
{"xsi", "http://www.w3.org/2001/XMLSchema-instance"},
{"xsd", "http://www.w3.org/2001/XMLSchema"},
{"udt","urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2"},
{"qdt","urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2"},
{"ext","urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"},
{"ccts", "urn:un:unece:uncefact:documentation:2"},
{"cbc","urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"},
{"cac","urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"},
{"inv", "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"},
{"svrl", "http://purl.oclc.org/dsdl/svrl"}
};
XmlNameTable xmlNameTable = new NameTable();
XmlNamespaceManager xmlInvoiceNamespaceManager = new XmlNamespaceManager(xmlNameTable);
foreach (KeyValuePair<string, string> ns in list)
{
xmlInvoiceNamespaceManager.AddNamespace(ns.Key, ns.Value);
}
return xmlInvoiceNamespaceManager;
}
protected static Stream GetEmbeddedResource(string path)
{
Assembly asm = Assembly.GetExecutingAssembly();
Stream stream = asm.GetManifestResourceStream(path);
return stream;
}
}
}
【问题讨论】: