0
votes

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;
}
1
Do you really use XslTransform? If you use XslcompiledTransform then make sure your XsltSettings allow the use of the document function. - Martin Honnen
Yes I'm using XslCompiledTransform and I'm allowing the use of the document function. I updated the question. - Javier
I don't have time to test now but there is an additional overload of the Transform method msdn.microsoft.com/en-us/library/ms163443(v=vs.110).aspx taking an XmlResolver so try xslt.Transform(xmlReader, null, xws, xslResolver);. - Martin Honnen
Tested and now Issue is solved!. Please put it as an answer and I'll mark it as the correct one. Thanks a lot for the help. - Javier
I have put the suggestion into an answer. - Martin Honnen

1 Answers

0
votes

There are Transform method overloads of XslCompiledTransform that take an additional parameter with an XmlResolver to be used for the document function, in your case https://msdn.microsoft.com/en-us/library/ms163443(v=vs.110).aspx which allows you to call

xslt.Transform(xmlReader, null, xws, xslResolver);