3

I am creating an application which uses XSLT to transform incoming XML messages. I have multiple XSLT, some of which includes other XSLT.

e.g. B.XSLT includes A.XSLT which contains some common functions.

I have marked all XSLT as embedded resources, so it will be embedded in executable. Now, when I am loading XSLT using following code,

using (Stream objXSLTStream = Assembly.GetAssembly(this.GetType()).GetManifestResourceStream("MyNamespace.XSLContainer.Transaction.B.xslt"))
{
    using (XmlReader objXSLTReader = XmlReader.Create(objXSLTStream))
    {
        XslCompiledTransform objXSL = new XslCompiledTransform(true);
        objXSL.Load(objXSLTReader);
        XmlReader objXMLReader = XmlReader.Create(new StringReader(InputXML));
        StringBuilder sbOutputXML = new StringBuilder();
        XmlDocument docOutputXML = new XmlDocument();
        XmlWriter objXMLWriter = XmlWriter.Create(new StringWriter(sbOutputXML));
        objXSL.Transform(objXMLReader, ArgsList, objXMLWriter);
        docOutputXML.LoadXml(sbOutputXML.ToString());

        return docOutputXML;
     }
}

it gives me error

Could not find a part of the path '..\Bin_Folder_Location\General\A.xslt'.

In my XSLT B, I am including A.xslt using this statement

<xsl:include href ="../General/A.xslt"/>

Can anybody suggest me the proper workaround to included embedded xslt into another xslt in C#?

Rumit Parakhiya
  • 2,674
  • 3
  • 24
  • 33

1 Answers1

1

You need to either use the absolute path in your xsl:include or use a custom XmlUrlResolver as here:

class MyXmlUrlResolver : XmlUrlResolver
    {
        public override Uri ResolveUri(Uri baseUri, string relativeUri)
        {
            if (baseUri != null)
                return base.ResolveUri(baseUri, relativeUri);
            else
                return base.ResolveUri(new Uri("http://mypath/"), relativeUri);
        }
    }

And use it in load function of XslCompiledTransform,

resolver=new MyXmlUrlResolver();
xslt.Load(xR,null,resolver);

How to resolve XSL includes in a Transformation that loads XSL from a String?

Community
  • 1
  • 1
markoo
  • 708
  • 1
  • 6
  • 22