How to run an XSLT transform

Instructional HowTos, posted by users. No questions here please.

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
User avatar
rsperberg
Posts: 35
Joined: Thu Jul 29, 2004 2:26 pm
Location: NJ

How to run an XSLT transform

Post by rsperberg »

I use the msxsl engine from Microsoft. I don't know whether it handles XSLT 2.0, but it is a small, fast, lightweight command-line utility for XSLT 1.0 transformations.

You can download msxsl.exe from www.microsoft.com/downloads/details.aspx?familyid=2fb55371-c94e-4373-b0e9-db4816552e41&displaylang=en

I find it easiest to put applications called as TextPad tools in a folder whose name contains no spaces. So I put this utility in

c:\programs\msxsl.exe

Once msxsl.exe is installed, make a new tool in TextPad (I won't go through the basic steps).

I set the Tool preferences this way:
Parameters: c:\programs\msxsl.exe $Filename my-stylesheet-name.xsl -o $Basename.xtm
Initial folder: $FileDir

Regular expression to match output:

^(URL: file:///)([A-Z]:.+)\/(.+)\nLine:( *)([0-9]+)\nColumn: ([0-9]+)$

[X] Capture output

Registers:
File: 3 Line: 5 Column: 6
Several things need to be noted here.

First, I hardwire in the stylesheet being used. You may prefer to enter it each time as a parameter. (In that case, check "Prompt for parameters" too.) Or the XML file may itself specify the stylesheet.

What I find is that either I'm fiddling around with the XSL stylesheet and continually running it against sample files as I make changes, in which case hardwired works fine, or I'm fiddling with the XML, in which case hardwired works fine too. I just have to change the stylesheet name when I move to a new project.

Secondly, as you may be able to tell from the last parameter, I'm usually transforming one type of XML file into another, differentiated from the first by its extension. So I use the $Basename tool macro to name the resulting file. You'll need to fix this so it works for you -- maybe "Prompt for parameters" so you can supply a fresh name each time.

Thirdly, this setup automatically sticks the result into a new separate file. You may prefer that it be put into the Command Results window, where you can review it before manually saving and naming the file. In that case, remove everything from the Parameters line beginning at "-o". You'll also probably want to check "Suppress output until completed" to keep things at their speediest.

After using this process for a good while, I wanted to be able to view the resulting transformation in TextPad but still have it saved automatically.

You can do this by making a different sort of tool.

First, create a batch file with these lines (I used the name msxsl-transform.bat and placed it in c:\programs):
c:\programs\msxsl.exe %1 %2 %3 %4
c:\Progra~1\TextPa~1\TextPad.exe %4
Then set up the tool as a DOS command with these settings:
Parameters: c:\programs\msxsl-transform.bat $Filename my-stylesheet-name.xsl -o $Basename.xtm
Initial folder: $FileDir

Regular expression to match output:

^(URL: file:///)([A-Z]:.+)\/(.+)\nLine:( *)([0-9]+)\nColumn: ([0-9]+)$

Registers:
File: 3 Line: 5 Column: 6
You will note that the parameters add those from above to the batch file name, and that the other settings are the same as before.

The batch file sends back information on its progress to the Command Results window when things go well, and then TextPad opens the results file and makes it the active window.

If there's a problem with your XML file, the Command Results window display shows the error from the XSL engine. Double-clicking on the file error line will jump you back to the source XML file.

(As noted in a different thread, I cannot get the Line and Column to work with this multi-line error message. Don't know if it's the regex or TextPad that's accountable.)

Roger Sperberg

Thanks to Tallyrand for suggesting the use of a batch file to accomplish this!
Post Reply