Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Mapping - XSLT or Code?

I have had a number of discussions recently over whether to use XSLT or code to write mapping functionality from one XML format to another or even when converting to something other than XML. Now I am of the mindset that XSLT's purpose is exactly for this type of thing and would be the most suitable option.

However, other people are suggesting it wouldn't be appropriate when you need something a little more complex, such as when you need to start looking up data from external repositories. They were also suggesting that XSLT can be as complex as writing the code, so that negates that argument. And testing would be easier with a code solution by utilizing TDD and CI practices.

The basis for this discussion is the design of a common transformation service that should be utilized by WCF services when any mapping is required. For example, when converting an incoming message to a canonical form. I thought it would be best to write this service to perform some matching of the XML message against an XSLT map. You can then easily drop in/out these maps without code recompilation and it is far easier to get at these maps and understand what is going on outside of the code.

I was wondering what you guys thought and whether anyone had any experience writing something similar? I know I could go out and buy a product, but would rather hear about bespoke solutions.

Thanks

like image 416
Jon Archway Avatar asked Dec 30 '22 08:12

Jon Archway


1 Answers

First, just to be clear, XSLT is code. ;) It's a Turing complete, functional programming language.

When input and output is XML I generally prefer XSLT; there is a lot of boiler plate code involved when transforming in a general purpose language. Exceptions are when the input needs to be processed sequentially due to its size (XSLT requires a full in-memory tree structure for both input and output). Also, emitting e.g. plain text via XSLT is a pain (mainly due to white space/line feed issues).

However, a valid concern is whether the skills to read and maintain the XSLT program is generally available on the team. If people are not used to the functional paradigm, learning XSLT can be challenging (i.e. won't happen on Project/Company Time), and often the solution has to be immediately readable to the other developers/maintainers on the project. In that case, I'd (grudgingly ;) go for the general purpose language solution.

XSLT does not prevent unit testing either; transformations can be tested by asserting on the output of transformation test cases.

like image 72
Cumbayah Avatar answered Jan 09 '23 16:01

Cumbayah