Debug XSLT with extension objects


  • I have a stylesheet that uses an extension object and I'd like to debug the stylesheet in Visual Studio.  I've added the namespace to the stylesheet node like this:


    but I get an exception and this error:

    Cannot find the script or external object that implements prefix 'hw:HwHtmlFunctions'

    I've copied the DLL into the directory where the stylesheet is.

    Is there any way to use the transformation engine in Visual Studio with stylesheets that reference extension objects?

    Thank you.,
    Wednesday, November 07, 2007 7:16 PM


All replies

  • Are tou using this method to add the extension object and are you passing the XsltArgumentList instance to the Transform method?

    Thursday, November 08, 2007 2:06 PM
  • The problem isn't that I can't get the extension object to work.  It works fine with a little C# console app I wrote that uses XslCompiledTransform, like this:

    XslCompiledTransform xslt = new XslCompiledTransform();

    XsltArgumentList argList = new XsltArgumentList();

    SomeName.Accelerator.Xslt.Html.HtmlFunctions funcs = new  SomeName.Accelerator.Xslt.Html.HtmlFunctions();

    argList.AddExtensionObject("hw:HwHtmlFunctions", funcs);

    xslt.Transform(args[0], argList, System.Console.Out);

    The problem is when I try to debug a my stylesheet using Visual Studio that uses the extension object.  As soon as I get to a line that calls into the extension object I get the error in my original post.  It doesn't happen just for debugging; it also happens when I click the "Show XSLT Output" button.

    I don't see how I can let the Visual Studio transformation engine know about the extension object.  I'm looking for the analogous method to "AddExtensionObject" for Visual Studio.  Seems like it would be some type of property on the stylesheet, like "Use this extension object".

    Thank you.

    Thursday, November 08, 2007 5:34 PM
  • A quick test and this works fine for me.


    Creating the XsltArgumentList here:


    XsltArgumentList args = new XsltArgumentList();

    args.AddExtensionObject("", new ExtensionMethods());


    The extension method class here:


    public class ExtensionMethods
        public XPathNavigator ParseXml(string xml)
            System.IO.StringReader sr = new System.IO.StringReader(xml);
            XPathDocument doc = new XPathDocument(sr);
            return doc.CreateNavigator();

    In the XSLT file declare the namespace:




    And then use it like this:


    <xsl:copy-of select="myStick out tonguearseXml(string(.))" />


    Try changing the "hw:HwHtmlFunctions" string to a a valid URN (e.g. "urn:HwHtmlFunction" or "http://HwHtmlFunction".

    Friday, November 09, 2007 3:49 PM
  • I must not be explaining myself well.  I want to debug in the XSLT code, not the C# extension object code.  Pretend I don't even have the source of the extension object. 

    In your example are you able to load the XSLT with the <xsl:copy-of> statement in Visual Studio, set a breakpoint one line after the <xsl:copy-of> statement and break at that point?  I can't do any transformations, let alone set a breakpoint and debug, using Visual Studio 2005 when an extension object is used.  I get the error in my original post.  My extension object and my stylesheet work fine together if I do not use VS2005 (for example if I use the sample code in my 2nd post).

    Monday, November 12, 2007 4:47 AM
  • In that case, have you tried using the XmlCompliledTransform(bool enableDebug) constructor?

    There are details of the requirements to be able to debug on ths link:
    Monday, November 12, 2007 7:43 AM
  • Beautiful...that's exactly what I was looking for.

    It pointed out that I was approaching the debugging of the stylesheet incorrectly.  I was loading the stylesheet in VS and pressing F5.  You point out you actually need to do the transformation using an outside application.  When I debugged my console app with breakpoints in the stylesheet it worked great.

    Thank you.
    Monday, November 12, 2007 5:59 PM
  • I had a very similar issue.  I wanted to take a stylesheet that I had created in XmlSpy, with an extension, and just open and execute it in VS2005.  VS2005 refused to recognize the external dll that was invokved.  Does anybody know how to make an external DLL part of th XSLT debug process instead of screwing around with a driver application and all that?


    Here's the stylesheet header:

    <?xml version="1.0" encoding="utf-8"?>

    <xslTongue Tiedtylesheet version="1.0" xmlns:xsl=""


    <xslSurpriseutput method="html" omit-xml-declaration="yes" indent="yes"/>





    and here is the call to a function in the dll from within stylesheet:






    <span style="text-align:left; font-family:monospace; font-size:12px">

    <xsl:value-of disable-output-escaping="yes" select="utils:replace_CRLF_BR(string(.))"/>

    <!--<xsl:call-template name="br-replace">

    <xsl:with-param name="word" select="."/>






    This works fine in XmlSpy, which has no problem loading the DLL (once I got the clitype: .... syntax down)

    Saturday, November 08, 2008 1:01 PM
  • You cannot debug or the transformation does not work at all? If this is the latter - are you adding the object implementing replace_CRLF_BR method to the transformation using AddExtensionObject method ( Note that namespaces used in stylesheet and as a parameter passed to AddExtensionObject function have to match.



    Sunday, November 09, 2008 7:06 AM
  • You need to pass in an extension object, see


    Sunday, November 09, 2008 11:59 AM
  • It appears from all the responses that it is impossible to simply open the XSLT document in VS and then have it do the right thing without going through creating a small C# program to drive it.  The desire is to NOT do all the steps outlined by others, but simply have VS2005 follow the header directive to load the extensions DLL I want to use when I select XML/Debug XSLT.  Regrettably, VS 2005 will not do this while XmlSpy will, happily.


    So, this forces either fixing the XSLT once it is all set, if laying out in XmlSpy,  so that it can be used with VS2005 or not bothering with XmlSpy to begin with.


    Am I correct in this conclusion?


    Monday, November 10, 2008 1:42 PM
  • Sorry, extensions are not portable. That header might be meaningful to XML Spy's XSLT processor to load the DLL but it is not to XslCompiledTransform. So you need to use an extension object with XslCompiledTransform respectively Visual Studio.

    Monday, November 10, 2008 1:52 PM
  • It seems there are two approaches to debugging an XSLT in Visual Studio.  One approach is to use the debugging features of the XML Editor that is integrated into Visual Studio, and a completely separate approach is to write an application that uses an XslCompiledTransform and step into it.  See both described here:

    In terms of XSLT debugging when extensions objects are leveraged, the latter approach is fairly straight forward to accomplish.  The former approach is not.  The former approach, using the native Visual Studio XML Editor directly, does not give you programmatic access to an XslCompiledTransform object nor does it give you (as far as I can see) a user interface to register extension objects into the transform that Visual Studio is using.  Of course I may be mistaken, so I ask...

    Is anyone aware of how to use Visual Studio's integrated XML Editor to debug an XSLT that relies on extension objects?

    Matt Poland - Manager of Application Development
    • Edited by Matt Poland Thursday, January 08, 2009 7:31 PM aid readbility
    Thursday, January 08, 2009 7:24 PM