use-when

Inasmuch as use-when is one of the standard attributes in XSLT 2.0 (and later) rather than being on a particular XSLT element, it seldom gets much of a mention; e.g., currently only 568 mentions on the XSL-List according to MarkMail (and some of those are false positives).

use-when (and xsl:use-when on non-XSLT elements) is for “conditional element inclusion” and is very useful for excluding elements that either aren’t currently useful or that will cause errors if acted upon.  The use-when in the example below causes the xsl:value-of to be used only when saxon:line-number() is available, thereby avoiding the Saxon extension functions are not available under Saxon-HE message from more recent versions of Saxon HE where saxon:line-number() is no longer available but producing a useful result on older Saxon HE versions where the function is available.

<!-- Leftover intermediate elements. -->
<xsl:template match="t:*">
  <xsl:if test="$debug">
    <xsl:message>
      <xsl:value-of select="t:node-basename(.)" />
      <xsl:value-of
        use-when="function-available('saxon:line-number')"
        select="concat(':', saxon:line-number())"
        xmlns:saxon="http://saxon.sf.net/" />
      <xsl:text> :: </xsl:text>
      <xsl:copy-of select="." />
    </xsl:message>
  </xsl:if>
  <xsl:apply-templates />
</xsl:template>

The use-when expression is evaluated very early in the processing of the stylesheet, and you can’t use variable references in the expression.  So don’t be tempted to try:

<xsl:message use-when="$debug">
...
</xsl:message>

xslide alive!

Inasmuch as you, like me, may have missed xslide’s “Template” menu when editing XSLT using Emacs’ nXML-mode, I’ve made available my “xslide2″ XSLT mode for Emacs that I’ve been using for a while.  This new xslide is a derived mode that uses nXML-mode for nearly everything and adds back some of the XSLT-specific parts of xslide.

Future development is happening on the Trac and Subversion for the xslide SourceForge project.  See https://sourceforge.net/apps/trac/xslide/wiki/WikiStart.

Saxon feature keys for initial template and mode

Inasmuch as a “feature key” is how you configure a Java JAXP XSLT processor, it’s good to see that Saxon 9.3 has added feature keys for setting the initial template and initial mode. In fact, Saxon has gone from 30 feature keys in Saxon 9.0 to 70 in 9.2 and now 79 in 9.3.

Where this becomes useful (outside of your own Java programs) is being able to specify the initial template and/or mode in the <xslt> task in Ant build files. Continue reading

XSpec

I forgot to mention that I’m also a member of the project for Jeni Tennison’s XSpec project at http://xspec.googlecode.com/, which is a Behavior Driven Development (BDD) framework for XSLT.  Mind you, the few changes that I have made would only count as gilding the lily.

Break your own tests

Some days you may be the best XSLT exponent on the planet; other days you may make a simple mistake and not realise it.

Particularly when you are writing tests after someone else wrote the stylesheet, if you find yourself on a roll with all your new tests coming up green first time, it can be useful to occasionally break a working test a little so that it’s bound to fail. Continue reading

Juxy

Juxy (http://juxy.tigris.org/), by Pavel Sher, is a library for unit testing XSLT stylesheets from Java. After a long time spent threatening to do so, I finally contributed an XML format for Juxy tests.

I’ve been riding my hobby horse for a while about the usefulness, nay necessity, of using more than XSLT when testing XSLT (and I’ll be back in that saddle again at XML Prague). Juxy fits that bill, since it’s in Java, but it always seems to me that writing tests in Java is more than many XSLT practitioners would want to do. So I wrote a stylesheet to generate Java from XML descriptions of the tests. Continue reading

xs3p is not the secret sauce

I used to think that the open source xs3p schema documentation generator stylesheet from the now-defunct http://titanium.dstc.edu.au/ was the secret sauce behind the remarkably similar graphical XML schema representations of both <oXygen/> and XML Spy.  I was wrong: a modified version of xs3p is bundled with <oXygen/> and is used when generating printed documentation, and xs3p may still be included in XML Spy (though it’s unlikely since its currently not listed on their third-party licenses page), but even in its titanium days, it didn’t do any graphical representations of a schema.

Does anybody know of an open source toolkit that can produce that sort of graphical representation?