views:

270

answers:

5

Background: Having given up on the practical daily use of XSLT as a part of my programming toolkit, I was wondering if there were any implementations in other languages of the (only) two things I miss about that tool:

  • the ability to traverse data structures using "path" style statments via xpath
  • the ability to traverse template transformations using apply-templates instead of via an iterative or "looping" approach.

According to Google there are a couple of efforts out there to add "xpath-style" support to Javascript, but these have not apparently caught on very much. So far I haven't found anything where someone uses an "apply-templates" approach in another language

Question: Does anyone out there know of a programming language (hopefully one that is main-stream) that steals these two good ideas from XSLT, or applies the same or similar concepts using a different method?

+2  A: 

XPath, while essential to making XSLT work, is independent of it; libraries like libxml give you it for free. The style of template application you describe is a little trickier; that's what you would normally use XSLT for.

Any programming language that does this should be functional. You could try writing your own, less-verbose, XSLT dialect; Perl also may give you enough rope to emulate this feature convincingly (although the performance implications are unclear).

The tough answer, though, is that this doesn't really exist, except as libraries for already existing languages.

Edward Z. Yang
+4  A: 

the ability to traverse data structures using "path" style statments via xpath

I'm not aware of any other language that embeds XPath, but LINQ to XML is somewhat similar, particularly in its VB syntactic sugar incarnation. You could implement it in Common Lisp macros, or D templates, however.

the ability to traverse template transformations using apply-templates instead of via an iterative or "looping" approach.

No mainstream languages that I know of. Indeed, this feature is probably the main reason to use XSLT (and not e.g. XQuery, looking at closely related languages).

It's effectively extensible dynamic dispatch on receiver on arbitrary conditions - as such, I think you could probably do it in Common Lisp (CLOS, to be specific) - if I remember correctly, its multimethods can match arbitrary conditions, so if you have an XPath pattern evaluator, you could use it to emulate apply-templates, and even more - since apply-templates only dispatches on a single argument, while CLOS multimethods dispatch on multiple arguments.

Pavel Minaev
+1  A: 

For XPath, definitely. For C, there's Xalan-C++, for Java javax.xml.xpath (with multiple implementations), and C# has XPathNavigator and SelectNodes. If you want to use XPath for object hierarchies, look at JXPath.

For the template transformations, you should look at C#'s LINQ if you haven't already. It's not exactly the same thing, but it allows processing objects without explicit looping.

Matthew Flaschen
+1  A: 

I have found nothing like that. But why would anybody use anything else to transform XML ? XSLT does a perfect job once you understand the non procedural way of developing solutions. Our applications are largely XSLT based and it is a really powerful tool.

PetervanBoheemen
The purpose is not to transform XML, but to transform arbitrary data-structures (e.g., JSON) using any general-purpose programming language (instead of a non general-purpose language like XSLT)
dreftymac
With XSLT 2.0 there are a variety of new features that make it easier to parse non-XML files, such as JSON (e.g. unparsed-text(), Regular expressions, etc.). Rather than look for an alternative to XSLT, see if you can accomplish what you need with XSLT 2.0.
Mads Hansen
+1  A: 

A comment on your first requirement:

  • the ability to traverse data structures using "path" style statments via xpath

XPath makes a lot of assumptions on the data structure. If you're going to use it, you might as well convert your structure to XML because it's going to look like it anyway once you make it traversable via some XPath-like language unless you severely limit your XPath subset.

Also, keep in mind that the "only two things" that you are missing, XPath and template processing, are in-fact a huge part of what makes up Xslt. I'm curious why you decided to take it off of your tool-belt.

In spite of that fact that you wanted an Xslt alternative, I would still recommend Xslt and Xslt 2.0 in particular. With the addition of the unparsed-text and analyze-string you have a powerful text processing language. For example take a look at a CSV to XML stylesheet. Even though JSON isn't regular, you'd still be able to write a simple JSON to XML translator using recursive templates and transform the result at will.

16bytes
Thanks for your comment. You've already touched on one issue, converting every structure into an XML data packet has some rather cumbersome aspects. Moreover, XSLT is great for what it does, but there is something to be said for a purely general-purpose transformation language. It's not clear whether XSLT 2.0 bills itself as that. If so, the result remains to be seen.
dreftymac
I'm pretty sure that most would agree that XSLT isn't a general-purpose anything. I'm curious, however, if you had a specific use-case in mind in pseudo-code. If there was something that was general-purpose, how would you define it's structure and how to traverse it to the transformer? I would conjecture that the definition would be cumbersome enough to justify converting your data-structures to XML.If you had in mind a fairly elegant syntax, however, that would be a great idea for a project.
16bytes
If you can compare JSON with XML, there are some similarities. Both are used to specify arbitrary structures with potentially many levels of nesting. Also, "xpath" style addressing of JSON has been mentioned before (see e.g., http://goessner.net/articles/JsonPath/ )
dreftymac