tags:

views:

235

answers:

5

with apply-templates is easy to write difficult for understanding code.

are there rules to write apply-templates easy-to-read-and-maintain?

for example, "avoid using //* in select", "try to write all apply-templates in one template" or "don't use apply-templates if it's possible"

what looks natural, what ugly?

+5  A: 

Sorry if you find apply-templates to be hard to maintain - but that's the XSLT way of doing things. In fact you should use apply-templates much in favor of for-each as an example. That's what will you give best performance across the different XSLt processors. In general I would try to avoid having too many XPath segments/levels in the select clause. But there really is no rule of thumb here.

That "//*" is to be avoided becomes obvious if you understood the different axis. I would really advice to read up on those if you are working with XSLT and XPath.

tcurdt
+1  A: 

In my brief experience, organizing apply-templates functions is secondary to good documentation. Try to make your execution path obvious and enhance it with comments if needed to make tracing easier.

Otherwise there's nothing wrong with splitting your stylesheet into as many logical modules as needed.

  • Anything that's repeated should be in a template (obviously)
  • Path matching should be matchable by a string search. apply-templates and template match should at least look similar. This becomes essential the larger your template becomes.
Jweede
A: 

You have to think more in terms of what each template is selecting and what output you want from that. Don't worry so much about designing some execution path. Let XSLT take care of that for you.

Think about the structure of your document and how you want the output structured. Then start at the root and create templates that incrementally select to where you want to create output. It's useful to use intermediary templates that don't have any output to elucidate what you are selecting and why.

Avoid "for-each". It will only trick you into thinking you are working in a procedural domain.

Ishmael
A: 

<xsl:apply-templates select="//abc/xyz" /> Never start from the root in the inner apply-templates as it will coz stack overflow

Rashmi Pandit