Win a flower (🌹) or copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java (📚) this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
master stewards:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
garden masters:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
gardeners:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

apply-template params vs accessing parent attributes

 
pioneer
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi if I have XML like

And in XSL template that match <Parent> if I want to apply <Child> template, where <Child> requires parent's id and name,
Is it better to pass the parent attribute to child template as PARAMs or have child template access parent's attribute using XPATH. which performs better?
Thanks
 
Marshal
Posts: 25174
64
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can always access a parent node from a child node using a simple XPath expression, whereas explicitly passing the parent as a parameter is clumsy and error-prone. So I would say the XPath version performs better.
 
Babji Reddy
pioneer
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul
So even if the XML/XSL file considerably complex ( nested nodes), accessing parent attributes using ../../../@xxx (a DOM tree traversal) is efficient than defining a variable x in parent as x = @xxx (Memory allocation, garbage collection)and pass it to the necessary child templates?

And does this decision varies in a Transactional process like web page rendering Vs a batch process like document generation?

Also it will be grateful if you could suggest some references/best practises in this area.

[ August 24, 2006: Message edited by: Babji Reddy ]
[ August 24, 2006: Message edited by: Babji Reddy ]
 
Paul Clapham
Marshal
Posts: 25174
64
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One best practice, which I have already alluded to, is to write simple code that can be easily understood rather than more complex code that can easily be broken by careless programming. The short name of this practice is "Premature optimization is evil".

Now if you think that code version A is going to be more "efficient" (whatever you might mean by that) than code version B, then you should try both in your own environment and see. But in the example you gave, I don't see any reason at all to believe there would be any difference in speed or memory utilization between the two options you described.
 
pioneer
Posts: 1241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Paul Clapham:
One best practice, which I have already alluded to, is to write simple code that can be easily understood rather than more complex code that can easily be broken by careless programming.

Absolutely. Any time saved in the processing could well be offset by time spent trying to debug problems at a later stage. XSL isn't exactly simple to debug in the first place, so making it simple is a good idea.

Besides, unless the XML file is biiiiiig, the difference in processing speed between either strategy will probably be negligible.
 
Babji Reddy
pioneer
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I too agree with the priciples of "keeping it simple".
May be the example I gave is simple, but the XML does get big ( 40 to 80 KB) and hence options A/B might make difference.
And since the time came for prioritizing efficiency over simplicity, I am looking for the opinions/observations of developers who took the approach A/B.
And to me the XMLSpy debugger is lot helpful to troubleshoot the XSLT process.
 
Paul Clapham
Marshal
Posts: 25174
64
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Babji Reddy:
the XML does get big ( 40 to 80 KB) and hence options A/B might make difference.

Did you mean 40 to 80 megabytes? That might be considered "big" in some cases. But 40 to 80 KB is trivially small.

But don't let me deter you from doing your own benchmarking. I'm just saying, for small files and for changes (like the one you suggest) that don't look as if they would make any difference, I wouldn't spend any time on that myself. And given the optimizations that XSLT products try to do, I wouldn't try to predict anything in terms of running time or memory usage.
 
I'm THIS CLOSE to ruling the world! Right after reading this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!