假设我有以下情况:
XSL文件“A”包含XSL文件“B”
< xsl:include href =“file-B.xsl”/>
XSL文件“A”调用document('file-C.xml')
< xsl:variable name =“myFileC”select =“document('file-C.xml')”/>
上面的所有外部资源都嵌入在程序集中,并使用以下函数解析
public class EmbeddedResourceResolver : XmlResolver
{
public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
{
var assembly = Assembly.GetExecutingAssembly();
return assembly.GetManifestResourceStream(Path.GetFileName(absoluteUri.AbsolutePath)");
}
}
结果是:
根据MSDN https://msdn.microsoft.com/en-us/library/0e96wzcy(v=vs.71).aspx
如果XSLT样式表包含< xsl:import>或< xsl:include>标签, 或者 document()函数,然后使用XmlResolver实现 找到外部资源。
我调试了GetEntity函数,我看到在加载File-B时调用了它,但是对于File-C,这个函数没有被触发。
有什么想法吗?
/更新:这是我完成转换的实际代码
public string MapIcsrToR2(Batch batch)
{
string xmlR2;
using (var xsl = Assembly.GetExecutingAssembly().GetManifestResourceStream("Safety.E2B.Mappers.Conversion.downgrade-icsr.xsl"))
using (var xmlR3 = new StringReader(MapIcsrToR3(batch)))
{
using (var xslReader = XmlReader.Create(xsl))
using (var xmlReader = XmlReader.Create(xmlR3))
{
var xslSettings = new XsltSettings(true, false); // document function is allowed
var xslResolver = new EmbeddedResourceResolver();
var xslt = new XslCompiledTransform();
xslt.Load(xslReader, xslSettings, xslResolver);
using (var sw = new StringWriter())
using (var xws = XmlWriter.Create(sw, new XmlWriterSettings {
Encoding = Encoding.UTF8
}))
{
xslt.Transform(xmlReader, xws);
xmlR2 = sw.ToString();
}
}
}
return xmlR2;
}
答案 0 :(得分:0)
Transform
的{{1}}方法重载会带有XslCompiledTransform
的附加参数,用于XmlResolver
函数,在您的情况下https://msdn.microsoft.com/en-us/library/ms163443(v=vs.110).aspx这允许你打电话
document