Reputation: 11508
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:
IXmlLineInfo
interface (XmlReader
& co.)XSLTCompiledTransform
constructor is file-based (XmlUriResolver
should work).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:
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
Upvotes: 11
Views: 2703
Reputation: 607
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.
Upvotes: 2
Reputation: 6772
Consider debugging using XML Spy XSLT debugger. It works for me all the time.
Upvotes: 0