views:

335

answers:

2

I'm currently working on a project that involves a lot of XSLT transformations and I really need a debugger (I have XSLTs that are 1000+ lines long and I didn't write them :-).

The project is written in C# and makes use of extension objects:

xslArg.AddExtensionObject("urn:<obj>", new <Obj>());

From my knowledge, in this situation Visual Studio is the only tool that can help me debug the transformations step-by-step. The static debugger is no use because of the extension objects (it throws an error when it reaches elements that reference their namespace). Fortunately, I've found this thread which gave me a starting point (at least I know it can be done).

After searching MSDN, I found the criteria that makes stepping into the transform possible. They are listed here. In short:

  • the XML and the XSLT must be loaded via a class that has the IXmlLineInfo interface (XmlReader & co.)
  • the XML resolver used in the XSLTCompiledTransform constructor is file-based (XmlUriResolver should work).
  • the stylesheet should be on the local machine or on the intranet (?)

From what I can tell, I fit all these criteria, but it still doesn't work. The relevant code samples are posted below:

// [...]

xslTransform = new XslCompiledTransform(true);

xslTransform.Load(XmlReader.Create(new StringReader(contents)), null, new BaseUriXmlResolver(xslLocalPath));

// [...]

// I already had the xml loaded in an xmlDocument 
// so I have to convert to an XmlReader
XmlTextReader r = new XmlTextReader(new StringReader(xmlDoc.OuterXml));

XsltArgumentList xslArg = new XsltArgumentList();
xslArg.AddExtensionObject("urn:[...]", new [...]());
xslTransform.Transform(r, xslArg, context.Response.Output);

I really don't get what I'm doing wrong. I've checked the interfaces on both XmlReader objects and they implement the required one. Also, BaseUriXmlResolver inherits from XmlUriResolver and the stylesheet is stored locally. The screenshot below is what I get when stepping into the Transform function. First I can see the stylesheet code after stepping through the parameters (on template-match), I get this:

The error I get when I step into the stylesheet

If anyone has any idea why it doesn't work or has an alternative way of getting it to work I'd be much obliged :).

Thanks,
Alex

A: 

Dear Alex, I'm not sure about usage of extension objects but as I understand your problem is with debugging of XSLT transformation in code in VS2010. Here is the function that we use to debug XSLT transformation:

 public string ApplyTransformation(string inputFilePath, string xsltFileContent)
    {
        XslCompiledTransform transform = new XslCompiledTransform(debugEnabled);

        File.WriteAllText(xsltTempFilePath,xsltFileContent);
        transform.Load(xsltTempFilePath, XsltSettings.TrustedXslt, new XmlUrlResolver());

        XmlReader reader = XmlReader.Create(inputFilePath);
        StringWriter output = new StringWriter();
        XmlWriter writer =  XmlWriter.Create(output,transform.OutputSettings);
        transform.Transform(reader,writer);
        return output.ToString();
    }

Unfortunately, there is a bug with VS2010 XSLT debugger which will make your debugging experience worse than in VS2008.

Yours sincerely, Maxim Filimonov.

mfilimonov
Regarding the bug: Has this been working with VS 2008? I was only getting it to work with the XSLT being loaded from a file on disc.
0xA3
I've just tested it with VS 2008 SP1. It works.
mfilimonov
+1  A: 

Consider debugging using XML Spy XSLT debugger. It works for me all the time.

Gad D Lord
After a quick look, I couldn't find anything related to extension objects. Does it support them?
Alex Ciminian