• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SAX or XSLT

 
Hari babu
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
When i want to retrieve specific data from the XML, Which is better SAX or XSLT ? I feel XSLT is better, what do u guys feel ?
Hari
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SAX is better. Here is why.
If you use XSLT, the parser will have to do three things -
  • Parse the stylesheet and build a DOM tree.
  • Parse the input document and build a DOM tree.
  • Apply the transformation and generate the output - may involve building another DOM tree based on parser implementation.


  • Clearly, this is expensive compared to lightweight SAX parsing. The benefit of XSLT remains its flexibility. Using XSLT, if you need to change your result tree, you simply change the stylesheet and everything works fine. However, if you used SAX, you will have to change your code to add/delete nodes from the result tree, recompile and then redeploy.
     
    Mapraputa Is
    Leverager of our synergies
    Sheriff
    Posts: 10065
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I am note sure the question has one single right answer. SAX doesn't have to parse the whole tree and to build full-blown in-memory representation, true, but the effect is important on documents that are 1) big enough 2) are getting read regularly. If all you need is to fetch data out of small document or even a big one but only once, than flexibility of XSLT (it would be more correct to say XPath, because in regard to data fetching XSLT is little more than a wrapper around XPath) can decide the contest.
    Accidentaly, I am now in process of extracting data from our bunkhouse HTML, and I cannot imagine how I would do it with SAX. The pages were created by several generations of bunkhouse workers, and manifest anything but consistency. The book title, for example, resides inside a link in <td> element, and sometimes the path is td/a, sometimes it is td/font/a sometimes td/a/font and many other exciting combination are also possible. One page included the span tag somewhere between. I can deal with all these cases with one XPath expression:
    td[1]//a | font//font | a/text()
    This is admittedly a very special case of "XML" ("pseudo XML" is more appropriate) document that has irregular structure, but this is exactly when one may prefer XPath over SAX.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic