Can you apply XSLT transforms using Sublime Text 2?

6

Scenario: I have a project with a number of XML files and I would like to apply a XSLT transform on these and then use that output for other purposes.

There is a variety of tools to do this, but it would be nice to be be able to stay in the editor while debugging/using the transform.

erikxiv

Posted 2013-04-05T10:05:55.803

Reputation: 1 804

What tools do you use and what system are you on? I ended up making a custom (quite complex) build scripts for knitr and others. – mreq – 2013-04-13T20:59:07.493

I am using a variety of tools and platforms, many of which have XSLT capabilities, so my question was whether there exists a plugin or similar that would smoothly allow me to apply an XSLT transform inside of the editor. External scripts might be OK as long as it is a one-time effort that can later be used on arbitrary XML/XSLT-files. – erikxiv – 2013-04-26T12:29:38.473

If you're able to create a custom bash script, it's easy to run it from sublime. If you're able to, I can link you to some further reading. – mreq – 2013-04-26T15:34:25.590

1I guess one of the tricks is that (1) the script requires two inputs, the xslt and the xml, and (2) that the output is a new file. If I write a script that takes three filenames as input (incl output name), how would I execute that and view the output without leaving the editor? If possible, could you write an answer so I can accept it? – erikxiv – 2013-04-30T08:53:26.033

Answers

1

If you have 3 different files you need, you can use first-line (or second if you need utf8 stuff) comments. I use it to compile the main TeX file from one of the children.

%!../main_file.tex
\documentclass[12pt,a4paper]{scrartcl}

\usepackage[czech,english]{babel}

I have a script, which looks at the first line:

match=`head -n1 $1 | grep %!`

if [[ $match ]]
    then
        # do stuff with the parent's name, which is ${match:2:100}
    else
        # no match :/
fi

and a simple build file aiming at my custom script:

{
    "cmd": ["/path/to/build/script.sh", "$file"],
    "selector": "whatever"
}

This way, you can have as many "references" in your files as you want. Just switch the value of head -n1.

In XML, you would probably use <!--%somefilename --> with head -n1 $1 | grep '<!--%' and ${match:5:100}.

To end with, I present to you my XeLaTeX build script ;)

#!/bin/bash
file="$1"
flag="-halt-on-error"

match=`head -n1 $file | grep %!`

if [[ $match ]]
    then
        if [ ${match:2:3} = ../ ]
            then
                cd .. &&
                target=${match:5:100}
            else
                target=${match:2:100}
        fi
    else
        target=$file
fi
rubber -c 'set arguments -shell-escape' -f -m xelatex -W all $target

exit 0

mreq

Posted 2013-04-05T10:05:55.803

Reputation: 1 082

Smart thinking. During testing of my xslt I just add comments pointing to input/output filenames and then use the build script to execute the xslt. Thanks! – erikxiv – 2013-05-07T13:26:21.100