Question

Does there exist an XSLT / XPath style guide / coding standard / best practice reference?

In particular I'm maintaining a bunch of XSLT scripts which are demonstrably fragile and unmaintainable.

eg. Adding a single level of nesting to the XML requires hundreds of changes to the scripts, even on templates that are operating on subtree fragments that are unchanged.

In a procedural language there is a well establish literature of Object Oriented Design Principles (SOLID, LSP, ....) and Coding Standards. (Don't use global variables, reduce coupling, improve cohesion, encapsulate state...)

Where do I find the equivalent for XSLT?

No correct solution

OTHER TIPS

There's no single compact document of the kind you are looking for. Any book on XSLT is likely to be packed with advice, but most of it is of the kind that a good programmer will do anyway. You can't turn a bad programmer into a good programmer by writing coding standards, in my view.

On the particular problem that a small change to the XML requires large changes to the XSLT, this is specifically what the rule-based template approach of XSLT is designed to prevent. Beginners in XSLT are often slow to adopt this coding style, and instead use a more "procedural" style (for-each, if, choose, call-template) because it's closer to what they have encountered with other languages. Forums like this one are full of advice from experienced developers to use template rules and apply-templates more extensively, and this is precisely the reason. So a one-line style guide for XSLT would be simply: use template rules as much as you possibly can.

One problem with coding standards is of course that there are conflicting objectives. You will often find people advising against use of "//x", but that's actually a trade-off: //x improves flexibility (resilience to source document change) at the expense of performance (with some XSLT processors), so any such advice reduced to a one-liner can be unhelpful.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top