views:

249

answers:

4

I was researching alternatives to using Microsoft's XslCompiledTransform and everything seemed to point towards Saxon primarily and secondly XQSharp. As I started to look at documentation for Saxon I saw that XQuery could do the equivalent of my XSLTs that are no where near as terse as XQuery's markup is.

What advantages do XSLTs offer over XQuery to deserve the much more detailed syntax?

Would it be the templating functionality that can be created?

+5  A: 

In general, there's a lot of overlap; both are rooted in an underlying XPath implementation. As for whether to use XSLT or XQuery, the proof is in the pudding: XSLT is better at transforms, and XQuery is better at queries.

So, use XQuery for:

  • smaller, less complicated things
  • highly structured data (XQuery is very strongly typed and prone to complaining)
  • data from a database
  • extracting a small piece of information

Conversely, use XSLT for:

  • copying a document with only incremental changes
  • larger, more complicated things
  • loosely (or badly) structured data
John Feminella
I disagree with your claims that XQuery is very strongly typed and is not good for larger more complicated things. While this is true of some database implementations, this is not true of XQuery in general.
Oliver Hallam
Oliver: I'm only drawing a comparison between XQuery and XSLT in a subjective sense, relative to each other. That is, just because I think XSLT is better for larger jobs doesn't mean that I think that XQuery is bad at larger jobs.
John Feminella
+1  A: 

XSLT and XQuery do two different things. XSLT, as the name suggests, is used to transform data from one form into another (e.g. from XML to HTML). On the other hand, XQuery is a language used to find and extract certain XML nodes from an XML document, which can then be used for any number of purposes.

XSLT actually relies on the functionality of XQuery, take a look at the tutorials on www.w3schools.com; when used correctly, they are both very powerful technologies.

Rory
+2  A: 

XSLT is designed to take one xml document and transform it into something else, e.g. csv, html or a different xml format, e.g. xhtml.

XQuery is designed to extract information from one or more xml documents, and combine the result into a new xml document.

Both XQuery and XSLT rely heavily on XPath. If your output is based on one input xml document and **one output xml document, the two can pretty much be interchanged.

The FLWR syntax of XQuery is quite intuitive, if you have an SQL back-ground, IMO XSLT is the more powerful language when dealing with one input/one output situations, especially if the output will not be xml.

Personally I find the xml based syntax and the declarative nature of XSLT slightly difficult to read and maintain.

It really boils down to choice, although using XQuery for "simple" formatting is slightly unusual. If your input is based on more than one xml document, you are pretty much stuck with XQuery, if your output is not xml based, you are pretty much stuck with XSLT.

Anton
I think the use of sequences eg. ( $s , "" , concat(...) ) allows text output instead of XML so it is possible to use it as a text templating processor.Usually an option to turn off the XML declaration is useful too.
philcolbourn
+1  A: 

The biggest reason to move away from XslCompiledTransform is that it is merely an XSLT 1.0 processor.

The majority of the functionality of XSLT 2.0 and XQuery 1.0 overlaps, and for the most part they are similar languages with different syntax (a little like C# and VB).

XSLT is a lot more verbose, but its templating features add a lot of functionality that can be fairly cumbersome to replicate in XQuery, particularly making small changes to node trees. The main features that are particularly cumbersome in XQuery are things like <xsl:template match="..." /> and <xsl:copy>...</xsl:copy>.

XQuery has a much cleaner syntax (IMHO) as long as the templating features are not needed, and I find it is a lot better for more advanced computations, and retrieving data from large documents.

XQuery is often viewed as a database language. Whilst a lot of databases use it this way, it is not the only use for it. Some implementations of the language in databases are very restricted. Another commentor claims that XQuery is "very strongly typed". Unless you are using the static typing feature, XQuery is no more strongly typed than XSLT. Whilst some database implementations force you to use the static typing features, most other implementations are moving away from this.

He also claims that XQuery is not very good for "larger, more complicated things". I would have argued exactly the opposite. The conciseness and flavour of the syntax makes it far easier to write complicated functions and computations in XQuery. I have written a raytracer in XQuery, which feels really quite natural; I think it would be a lot harder (certainly more verbose) to write something this computationally complex in XSLT.

In summary:

XSLT is more natural for transformation. It is better if you have a document with roughly the right structure and you want to transform the components, for example rendering an HTML version of an XML file.

XQuery is more natural for building a new XML document from various sources, or for changing the structure of a document.

Note that the overlap is rather large and there is often no "right" choice, but if you make the wrong choice then you to tend to find yourself working against the grain.

Oliver Hallam