Using Scala as a replacement for XSLT?

We have a lot of legacy XSLT in my Scala project, and I was wondering if I needed to convert XSLT to Scala code.

I like the XSLT approach to applying templates to nodes, and I use it well for purely DOM transformations, but I don't think it is well suited for processing data in an XML document (hard to read and test) - I would rather use Scala for this.

Given Scala's native XML support and pattern matching, I thought this was a good replacement. Has anyone successfully converted XSLT scripts to Scala? Are there any patterns or best practices?

I know an old project for converting XSLT to Scala XSLT2src source code, but it has been inactive for a long time and does not support XSLT2.

thanks

+6
source share
2 answers

It's hard to say for sure, not knowing exactly which XML you want to convert and how, but, as the situation stands, the XSLT template / match is more powerful and readable than Scala XML template matching, which has a number of problems.

In particular:

  • any nontrivial comparison is either impossible or very difficult to do
  • namespaces are not supported

Thus, you might be better off calling Saxon from Scala to perform these conversions.

However, you can see the XML chapter from Scala programming.

Finally, the common belief these days is that Scala's native XML support is missing in many ways. See Anti-XML for a project whose goal is to create something better.

+6
source

Scales Xml provides matching by namespaces and attributes directly, and also provides more flexible internal dsl for handling XPaths (as well as string-based evaluation).

Perhaps the most interesting for conversions is its ability to collapse along paths, choose what you want in XPath, and convert the results in place. "These conversion rules can be combined.

You can still replicate most of the xslt approach using direct XPath in Scales and transform / build trees just like in xslt.

+1
source

Source: https://habr.com/ru/post/891272/


All Articles