Let's say I have the following scenario:
XSL File "A" includes XSL File "B"
<xsl:include href="file-B.xsl"/>
XSL File "A" calls document('file-C.xml')
<xsl:variable name="myFileC" select="document('file-C.xml')"/>
All external resources above are embedded in the assembly and are resolved using the function below,
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)"); } }
The result is:
- XSL File "B" is loaded successfully.
- XML File "C" is not found.
According to MSDN https://msdn.microsoft.com/en-us/library/0e96wzcy(v=vs.71).aspx
If an XSLT style sheet contains an <xsl:import> or <xsl:include> tag, or a document() function, then an XmlResolver implementation is used to locate the external resource.
I debugged the GetEntity function and I see that is called when loading File-B but for File-C this function is not being triggered.
Any ideas?
/Update: This is my actual code where the transformation is done
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;
}
XslTransform? If you use XslcompiledTransform then make sure yourXsltSettingsallow the use of thedocumentfunction. - Martin HonnenTransformmethod msdn.microsoft.com/en-us/library/ms163443(v=vs.110).aspx taking anXmlResolverso tryxslt.Transform(xmlReader, null, xws, xslResolver);. - Martin Honnen