我用谷歌搜索了这个错误,找不到任何东西。
System.Xml.Xsl.XslLoadException 'version' is an invalid attribute for the 'xsl:stylesheet' element.
它是在我们的实时系统中引发的 - 因为Windows更新在周末应用(或者至少会出现)。我的支持团队没有进一步的信息。
at System.Xml.Xsl.XslCompiledTransform.LoadInternal(Object stylesheet, XsltSettings settings, XmlResolver stylesheetResolver)
at Acme.EDF.Xslt.Compile() in S:\MySource\Business Tier\BaseClasses\Xslt.cs:line 193
at Acme.EDF.TransformDocumentUsingXslt.Process(String document) in S:\MySource\Business Tier\StandardClasses\TransformDocumentUsingXSLT.cs:line 66
at Acme.EDF.Engine.ImportDocument(String documentContents, Guid configurationGuid, String submissionDetails, Guid& contextGuid) in S:\MySource\Business Tier\BaseClasses\Engine.cs:line 148
我无法复制这个问题。我查看了代码,以下内容导致了问题 - 所以部署的代码被移动到一个简单的控制台来尝试复制:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Xsl;
namespace XsltConsoleApplication
{
class Program
{
static void Main(string[] args)
{
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "XsltConsoleApplication.stylesheet.xsl";
string xml;
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
{
xml = reader.ReadToEnd();
var b = XmlReader.Create(reader, null, "boom");
}
XmlDocument xsl = new XmlDocument();
xsl.XmlResolver = null;
xsl.LoadXml(xml);
XsltUrlResolver resolver = new XsltUrlResolver();
XslCompiledTransform compiled = new XslCompiledTransform();
compiled.Load(xsl, null, resolver);
Console.WriteLine("Done");
Console.ReadLine();
}
}
}
我也尝试过我们系统使用的自定义XmlUrlResolver。然而,在那里有一些静电学使其成为诊断的噩梦。所以我试过了:
public class XsltUrlResolver : System.Xml.XmlUrlResolver
{
public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
{
object entity = null;
entity = base.GetEntity(absoluteUri, role, ofObjectToReturn);
throw new Exception("Boo hoo hoo");
}
}
不会在控制台应用程序中抛出异常 - 不进一步检查XslCompiledTransform调用树 - 我希望可以通过引发异常来解决这个问题。
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:pro="http://www.acme.com/xml/xml-ns" version="1.0">
<xsl:template match="@*|node()[not(self::pro:Import)]">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="pro:Import">
<pro:Import xmlns:pro="http://www.acme.com/xml/xml-ns">
<xsl:apply-templates select="@*|node()" />
</pro:Import>
</xsl:template>
</xsl:stylesheet>
问题在于XslCompiledTransform。我已经反编译了Microsoft代码,但还没有什么突出的。我假设在LoadInternal中加载xslt时抛出异常:
private CompilerResults LoadInternal(object stylesheet, XsltSettings settings, XmlResolver stylesheetResolver)
{
if (stylesheet == null)
{
throw new ArgumentNullException("stylesheet");
}
if (settings == null)
{
settings = XsltSettings.Default;
}
this.CompileXsltToQil(stylesheet, settings, stylesheetResolver);
CompilerError firstError = this.GetFirstError();
if (firstError != null)
{
throw new XslLoadException(firstError);
}
if (!settings.CheckOnly)
{
this.CompileQilToMsil(settings);
}
return this.compilerResults;
}
我只是想复制这个问题 - 所以我可以抛出XslLoadException,其中包含有关导致问题的版本属性的消息。
我尝试过很多方法调整xsl但是我不能抛出这个异常/消息。
更新
看起来加载时会对异常进行整理。我通过代码跟踪错误字符串资源,看起来下面的类抛出了特定的异常:
编译器
internal string GetSingleAttribute(string attributeAtom)
{
NavigatorInput input = this.Input;
string localName = input.LocalName;
string value = null;
if (input.MoveToFirstAttribute())
{
do
{
string namespaceURI = input.NamespaceURI;
string str = input.LocalName;
if (!Keywords.Equals(namespaceURI, this.Atoms.Empty))
{
continue;
}
if (!Keywords.Equals(str, attributeAtom))
{
if (this.ForwardCompatibility)
{
continue;
}
string[] strArrays = new string[] { str, localName };
throw XsltException.Create("Xslt_InvalidAttribute", strArrays);
}
else
{
value = input.Value;
}
}
while (input.MoveToNextAttribute());
input.ToParent();
}
if (value == null)
{
throw XsltException.Create("Xslt_MissingAttribute", new string[] { attributeAtom });
}
return value;
}
CompiledAction
public void CompileAttributes(Compiler compiler)
{
NavigatorInput input = compiler.Input;
string localName = input.LocalName;
if (input.MoveToFirstAttribute())
{
do
{
if (!Keywords.Equals(input.NamespaceURI, input.Atoms.Empty))
{
continue;
}
try
{
if (!this.CompileAttribute(compiler))
{
string[] strArrays = new string[] { input.LocalName, localName };
throw XsltException.Create("Xslt_InvalidAttribute", strArrays);
}
}
catch
{
if (!compiler.ForwardCompatibility)
{
throw;
}
}
}
while (input.MoveToNextAttribute());
input.ToParent();
}
}
ContainerAction
internal void CompileKey(Compiler compiler)
{
NavigatorInput input = compiler.Input;
string localName = input.LocalName;
int num = -1;
int num1 = -1;
XmlQualifiedName xmlQualifiedName = null;
if (input.MoveToFirstAttribute())
{
do
{
string namespaceURI = input.NamespaceURI;
string str = input.LocalName;
string value = input.Value;
if (!Keywords.Equals(namespaceURI, input.Atoms.Empty))
{
continue;
}
if (Keywords.Equals(str, input.Atoms.Name))
{
xmlQualifiedName = compiler.CreateXPathQName(value);
}
else if (Keywords.Equals(str, input.Atoms.Match))
{
num = compiler.AddQuery(value, false, false, true);
}
else if (!Keywords.Equals(str, input.Atoms.Use))
{
if (compiler.ForwardCompatibility)
{
continue;
}
string[] strArrays = new string[] { str, localName };
throw XsltException.Create("Xslt_InvalidAttribute", strArrays);
}
else
{
num1 = compiler.AddQuery(value, false, false, false);
}
}
while (input.MoveToNextAttribute());
input.ToParent();
}
base.CheckRequiredAttribute(compiler, num != -1, "match");
base.CheckRequiredAttribute(compiler, num1 != -1, "use");
base.CheckRequiredAttribute(compiler, xmlQualifiedName != null, "name");
compiler.InsertKey(xmlQualifiedName, num, num1);
}
internal void CompileNamespaceAlias(Compiler compiler)
{
NavigatorInput input = compiler.Input;
string localName = input.LocalName;
string nsAlias = null;
string str = null;
string value = null;
string value1 = null;
if (input.MoveToFirstAttribute())
{
do
{
string namespaceURI = input.NamespaceURI;
string localName1 = input.LocalName;
if (!Keywords.Equals(namespaceURI, input.Atoms.Empty))
{
continue;
}
if (Keywords.Equals(localName1, input.Atoms.StylesheetPrefix))
{
value = input.Value;
nsAlias = compiler.GetNsAlias(ref value);
}
else if (!Keywords.Equals(localName1, input.Atoms.ResultPrefix))
{
if (compiler.ForwardCompatibility)
{
continue;
}
string[] strArrays = new string[] { localName1, localName };
throw XsltException.Create("Xslt_InvalidAttribute", strArrays);
}
else
{
value1 = input.Value;
str = compiler.GetNsAlias(ref value1);
}
}
while (input.MoveToNextAttribute());
input.ToParent();
}
base.CheckRequiredAttribute(compiler, nsAlias, "stylesheet-prefix");
base.CheckRequiredAttribute(compiler, str, "result-prefix");
base.CheckEmpty(compiler);
compiler.AddNamespaceAlias(nsAlias, new NamespaceInfo(value1, str, compiler.Stylesheetid));
}
internal void CompileStylesheetAttributes(Compiler compiler)
{
NavigatorInput input = compiler.Input;
string localName = input.LocalName;
string str = null;
string value = null;
if (input.MoveToFirstAttribute())
{
do
{
string namespaceURI = input.NamespaceURI;
string localName1 = input.LocalName;
if (!Keywords.Equals(namespaceURI, input.Atoms.Empty))
{
continue;
}
if (Keywords.Equals(localName1, input.Atoms.Version))
{
value = input.Value;
if (1 > XmlConvert.ToXPathDouble(value))
{
if (compiler.ForwardCompatibility)
{
continue;
}
string[] strArrays = new string[] { "version", value };
throw XsltException.Create("Xslt_InvalidAttrValue", strArrays);
}
else
{
compiler.ForwardCompatibility = value != "1.0";
}
}
else if (Keywords.Equals(localName1, input.Atoms.ExtensionElementPrefixes))
{
compiler.InsertExtensionNamespace(input.Value);
}
else if (!Keywords.Equals(localName1, input.Atoms.ExcludeResultPrefixes))
{
if (Keywords.Equals(localName1, input.Atoms.Id))
{
continue;
}
str = localName1;
}
else
{
compiler.InsertExcludedNamespace(input.Value);
}
}
while (input.MoveToNextAttribute());
input.ToParent();
}
if (value == null)
{
throw XsltException.Create("Xslt_MissingAttribute", new string[] { "version" });
}
if (str != null && !compiler.ForwardCompatibility)
{
string[] strArrays1 = new string[] { str, localName };
throw XsltException.Create("Xslt_InvalidAttribute", strArrays1);
}
}