6

I want to create a xsl:function with a few parameters that can return a boolean, i'm having troubles:

<xsl:if test="my:isEqual($Object1, $Object2)">SAME!</xsl:if>

<xsl:function name="my:isEqual">
    <xsl:param name="Object1" />
    <xsl:param name="Object2" />

    <xsl:variable name="Object1PostalCode select="$Object1/PostalCode" />
    <xsl:variable name="Object2PostalCode select="$Object2/PostalCode" />
    <xsl:if test="$Object1PostalCode = $Object2PostalCode">
        !!!What to do here!!!
    </xsl:if>
</xsl:function> 
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
Bas Hendriks
  • 609
  • 1
  • 7
  • 14

2 Answers2

12

I want to create a xsl:function with a few parameters that can return a boolean, i'm having troubles:

<xsl:function name="my:isEqual"> 

Your troubles start even here. As written, nothing guarantees that this function wouldn't return any XDM type of item or sequence of items.

Rule to remember: When writing an xsl:function do specify its return type. Also specify the types of the parameters. This spares you from run-time-type-mismatch problems. It also gives opportunity for more powerful and aggressive optimization.

So, don't write the following -- you can have difficult to catch run-time type mismatch problems:

<xsl:function name="my:isEqual">           
  <xsl:param name="Object1" />           
  <xsl:param name="Object2" /> 

Instead use the full power of XSLT 2.0 and specify the correct types:

<xsl:function name="my:isEqual" as="xs:boolean">           
  <xsl:param name="Object1" as="element()?" />           
  <xsl:param name="Object2" as="element()?" /> 

Finally, the end of the code:

    <xsl:if test="$Object1PostalCode = $Object2PostalCode">                     
      !!!What to do here!!!                 
    </xsl:if>             
</xsl:function>  

Simply return the comparisson - it evaluates exactly to either true() or false() :

    <xsl:sequence select="$Object1PostalCode eq $Object2PostalCode"/>                     
</xsl:function>
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
6

You simply want

<xsl:sequence select="$Object1PostalCode = $Object2PostalCode"/>

instead of the xsl:if.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110