Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify a "Custom Tool" for transforming a file in VS2010?

The Properties window in VS2010 for most file types (e.g. .cs, .xml, .xslt) allows one to specify a custom tool for transforming files. For reference, here is the tool tip that one gets when selecting the "Custom Tool" field.

Specifies the tool that transforms a file at design time and places the output of that transformation into another file. For example, a dataset (.xsd) file comes with a default custom tool.

I am looking for information as to how to set and use this property.

Here is the problem I am trying to solve. I am transforming and XML file by applying XSLT. I am using extension objects during the transformation as described here.

In doing so I have rendered Visual Studio useless as a tool for editing and debugging my XSLT. I am hoping that I can write a simple transformation engine that will allow us to use Visual Studio as we do for XSLT documents that do not use extension objects. I think (hope) that the Custom Tool property is the key to making this work.

TIA

like image 452
Michael J Avatar asked Jan 19 '11 16:01

Michael J


2 Answers

Debugging XSLT transformations with extension functions with the ability to have breakpoints in both the XSLT code and the extension functions code has been supported since VS2005.

Just use this XslCompiledTransform constructor overload.

Parameters enableDebug Type: System.Boolean true to generate debug information; otherwise false. Setting this to true enables you to debug the style sheet with the Microsoft Visual Studio Debugger.

Remarks

The following conditions must be met in order to step into the code and debug the style sheet:

The enableDebug parameter is set to true.

  • The style sheet is passed to the Load method either as a URI, or an implementation of the XmlReader class that implements the IXmlLineInfo interface. The IXmlLineInfo interface is implemented on all text-parsing XmlReader objects.

    In other words, if the style sheet is loaded using an IXPathNavigable object, such as an XmlDocument or XPathDocument, or an XmlReader implementation that does not implement the IXmlLineInfo interface, you cannot debug the style sheet.

  • The XmlResolver used to load the style sheet is a file-based XmlResolver, such as the XmlUrlResolver (this is the default XmlResolver used by the XslCompiledTransform class).

  • The style sheet is located on the local machine or on the intranet.

Here is a small code example:

// Enable XSLT debugging.
XslCompiledTransform xslt = new XslCompiledTransform(true);

// Load the style sheet.
xslt.Load("output.xsl");

// Create the writer.
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent=true;
XmlWriter writer = XmlWriter.Create("output.xml", settings);

// Execute the transformation.
xslt.Transform("books.xml", writer);
writer.Close();
like image 55
Dimitre Novatchev Avatar answered Sep 27 '22 20:09

Dimitre Novatchev


A workaround to debug XSLT files that use extension objects is to create a test .exe file project (such as a Console Application project), and call the XSLT (with all extension objects correctly referenced) from that project.

You can set breakpoints, then "Start Debugging" the test .exe project to step into the XSLT file.

like image 21
ckarras Avatar answered Sep 27 '22 20:09

ckarras