When is XSLT the right solution for defining XML transformations instead of using a scripting language such as Python?

When is XSLT the right solution for defining XML transformations instead of using a scripting language such as Python?

+7
xml xslt transform
source share
7 answers

If you don’t want to consider things like getting to know developers, I would say almost always, assuming you have XSLT 2.0 or at least EXSLT, since XSLT 1.0 is pretty limited (if you only need structural transformations, 1.0 is enough, but if you need to do something with the content, you want to stay away from 1.0). XSLT is specifically designed for XML conversion, and I have found it much better for this task than general purpose XML libraries.

Of course, everyone assumes that you only need to convert the input XML to some other form. In the real world, requirements are changing, and suddenly you have to not only convert XML, but also perform some operations based on it. Although XSLT is Turing-complete, it is not a universal programming language, so it may be safer to use another language to provide future validation. However, I would probably implement the first version in XSLT, because I find it faster and more understandable, and only if XSLT later proves unsuitable, I would consider something else.

+3
source share

This is mainly due to the fact that XML is a hierarchical data structure in the form of a tree.

You can establish that operations are performed when certain templates are matched wherever they appear in the XML file tree.
You can also convert cascades so that some of them are applied / ignored depending on previous operations in the future on the tree.

If your XML data is relatively flat and the <X> means everywhere, then the benefits are not so clear, but if <X> means one in the root and something else inside <Y> and something else if inside <Z> after <Y> , then XSLT will become much easier.

0
source share

There are many overlaps.

  • If you can specify fewer template rules than are required to recursively define the same behavior with code, then XSLT makes sense.
  • If a person who follows the rules in the long run is more comfortable and familiar with (or has more effective support tools) than XML / XSLT, then code is the best way to go.
  • If you want to support massive parallelism for better performance, then XSLT (or a functional language such as F #) may be the way to go, although it may not be available right now (although it should be the future).
0
source share

I find that XSLT is very useful for simple tasks, and it is also platform independent (XSLT is built into many languages). However, I do not think that it scales easily, and since I also need to do domain-specific calculations, it is very tedious to write normal operations in recursive functional mode.

Also XSLT2.0 is really necessary for operations such as grouping (without it, you should remember the Muenchian method, which is smart but not intuitive). However, not all systems support XSLT2.0.

0
source share

In terms of performance, some XSL processors compile the conversion to a binary before running it. If the conversion will be used many times, this can be a big performance benefit.

0
source share

From the very beginning, if you transform XML into something other than HTML, XML, or text, XSLT is not what you should use.

In addition, I spent a lot of work with XSLT, and I still have to deal with the case when I used XSLT to transform XML and decided that I want me to choose something else. As in jk, my answer is "almost always."

0
source share

If you or someone you are working with is more comfortable with XSLT, or if there is any technical benefit to using XSLT (for example, to integrate XSLT into Cocoon), use XSLT. Otherwise, it is almost always a pain than you think. You will be much happier using a language such as Python (with lxml), Ruby (with rexml), or even PHP (with SimpleXML). You can even link it to a template library (like genshi with Python) to make the job even easier.

Having made both approaches on my own, I am sure that you will be happier in the long run using something other than XSLT.

0
source share

All Articles