Re: [xsl] Avoiding boneheaded mistakes in XSLT?

Subject: Re: [xsl] Avoiding boneheaded mistakes in XSLT?
From: Andrew Welch <andrew.j.welch@xxxxxxxxx>
Date: Thu, 30 Dec 2010 17:47:23 +0000
> As a use case I'm trying to catch typos and other 'man made'
> errors. How might a transformatron catch those?

Two defensive techniques are:

1. Use the as attribute on your template:

<xsl:template match="foo" as="element(p)">
  <xsl:apply-templates select="/foo/bar"/>

If the apply-templates call doesn't result in a <p> (for whatever
reason) then you will get an error.

2. Use a variable with the as attribute:

<xsl:variable name="bar" select="/foo/bar" as="element(bar)"/>

If the variable doesn't select a bar element, then you'll get an error.

Ultimately though, the best defense is result validation and a tight schema.

Andrew Welch

Current Thread