0

I have a parameter in my XSLT which usually is a proper set of nodes that I apply templates on.

<apply-templates select="$conferences" />

However, sometimes something goes wrong and it comes in as a string. In this case I just want to skip applying templates. But what is the proper way of checking this? I could check that it's not a string of course, but how can I check that the parameter is... "templatable"?

<if test=" ? ">
    <apply-templates select="$conferences" />
</if>
Svish
  • 152,914
  • 173
  • 462
  • 620
  • Beside the answers that you already received this may also be interesting to you: http://stackoverflow.com/questions/14118670/check-type-of-node-in-xsl-template . – Marcus Rickert May 22 '14 at 11:34

2 Answers2

3

Since you're in XSLT 2.0 you can simply do

<xsl:if test="$conferences instance of node()*">
Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
1

You can do:

<apply-templates select="$conferences/*" />

Which will only apply if there is an XML in it. Strings will not be applied.

If you want to do a condition up front, do something like:

<xsl:choose>
    <xsl:when test="count($conferences/*) &gt; 0"> <!-- it is XML -->
        <xsl:apply-templates select="$conferences/*" />
    </xsl:when>
    <xsl:otherwise>
        <xsl:apply-templates select="$conferences" /> <!-- it is not XML -->
    </xsl:otherwise>
</xsl:choose>
Mark Veenstra
  • 4,691
  • 6
  • 35
  • 66
  • If the variable `conferences` is a string or sequence of stringss then `$conferences/*` would give an error `XPTY0019: Required item type of first operand of '/' is node()`. – Martin Honnen May 22 '14 at 12:06
  • @MartinHonnen you are probably right, but when I did a test in Altova XMLSpy it did accept this – Mark Veenstra May 22 '14 at 13:23
  • I only have Saxon 9 (which gives the error message already posted) and AltovaXML 2013 which says "Type error XPTY0004: Expected a node". – Martin Honnen May 22 '14 at 13:30
  • @MarkVeenstra what version of XMLSpy? If it's one that only speaks XSLT 1.0 then that is less strict on types than 2.0 - there are things that would raise a type error in 2.0 but simply return an empty node set in 1.0. – Ian Roberts May 22 '14 at 15:49
  • @IanRoberts I currently have version 2014 rel. 2 sp1 – Mark Veenstra May 23 '14 at 11:27