3 ms·
A master of XSLT once explained me that if your XSLT files are longer than 100 lines, you are doing something wrong. For years I struggled to create 100-line X
by gioele 15y ago
A master of XSLT once explained me that if your XSLT files are longer than 100 lines, you are doing something wrong.
For years I struggled to create 100-line XSLT files; it looked impossible. Then I realised what the problem was: I was trying to convert the source tree into the result tree in one big step. XSLT can do that, but it is not meant to do that and it is just bad software engineering. What I should have done (and I started to do) is to divide the transformation in smaller uniform transformation steps: for example, in one case I ended up with this series of XSTL files, each one taking care of a single step in the transformation pipeline:
* cleaning (remove duplicated and empty nodes);
* completion of partial data (where I go, fetch missing data and use it to make the original data more homogeneous);
* grouping and reordering (twist the data around to make it more similar to the destination format);
* data transformation (where the individual pieces of data are transformed into separate nodes in the destination format);
* layout integration (fill the holes in the layout template with the missing data).
Each file ended up being very small, readable and easily maintainable. The much shorter xpaths used in the templates are a clear indicator of how much better that project has become. Before the split I was doing things like fetching the missing data while putting the already transformed data in the layout template; that was forcing me to transform the newly retrieved data in place before being able to use it. It was a mess. It was a mess because I was not following one of the underlying and unspoken assumption of XSLT. Isn't it the same with any other language? Will an Haskell-based tree transformation language prevent me from writing huge single-file many-purposes-in-a-single-function spaghetti code?
That said, I do not like articles like this that do not explain why they do not like something and do not give examples of what is wrong and how it should be instead.