0

I've read in a technical paper that using XSLT for making amendments in an XML file is a more efficient and preferable way than using the XML Parser like DOM, JDOM, SAX, JSoup etc. So I wanted to know on which parameters XSLT is more efficient and preferable to use than XML parser. (Does XSLT provide more generalized and easier solution?) Thanking you.

E.g.: If I've one XML file:

<node1>
<node2> TEXT </node2>
</node1>

and I need the output as:

<node1>
<node2> TEXT </node2>
<script src="xyz.js"></srcipt>
</node>

Then either I can write an XSLT for this to get the desired output or I can use XML-parser (DOM, SAX etc.) to write a Java program to insert the required element at the desired place and get the desired output. So for this I have read that XSLT is considered more preferable and effective.

RahulD
  • 709
  • 2
  • 16
  • 39
  • 1
    An XML parser doesn't perform any transformation. Therefore one cannot say that an XSLT processor is more or less efficient than an XML parser. Please edit the question and make it meaningful. – Dimitre Novatchev Aug 10 '12 at 12:08
  • @Dimtre Thanks for the comment. I've edited my question, please have a look. – RahulD Aug 10 '12 at 12:28
  • @DimitreNovatchev Thanks for the comment. I've edited my question, please have a look. – RahulD Aug 10 '12 at 12:34

2 Answers2

6

Efficiency is a concept of perspective....

A good XSLT transformation document could be really small, and would take an experienced 'XSLT expert' a few moments to put together. From a 'development' perspective, it would be very efficient.

In terms of CPU cycles though, the XSLT option is not likely to be the most efficient.

A good 'direct' (DOM/JDOM/SAX/whatever) programming of the process would be faster than XSLT, and even faster yet would be a non-XML-based process... (like 'sed' or 'awk').

So, I would challenge the conclusions of your 'technical paper'. in many instances XSLT is 'preferable' to other solutions because it is easier (for an expert) to maintain, alter, etc. But as for 'efficiency', the technical paper must first define what it means by that.

Bottom line is that XSLT sits on top of some XML model (SAX/DOM/JDOM/etc.) so it will never be as efficient as just the raw model.... and then all the XML models have inefficiencies, and if you get rid that layer too.

Rolf

rolfl
  • 17,539
  • 7
  • 42
  • 76
  • Thanks for the answer(+1). In that paper for their model they have used parser only but in future work they have mentioned that 'use of more efficient and preferable method XSLT'. – RahulD Aug 10 '12 at 13:17
  • 2
    One thing missing from the analysis is that an XSLT engine will use an XML tree representation optimized for XSLT processing, which is probably much more efficient than a general-purpose DOM. For example, because the tree is read-only, sorting nodes into document order can be very much faster; so can handling of namespaces. – Michael Kay Aug 10 '12 at 17:16
5

For many small transformations, the cost is dominated by parsing and serialization, and is independent of whether the transformation logic is written in XSLT or Java. (So write it in XSLT, because that is less work).

If the transformation becomes more complex then a smart Java programmer will probably outperform an XSLT engine but an average Java programmer won't. OK, you know you are smart. But on average, you're probably average.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164