Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create a xsl:function that returns a boolean

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> 
like image 505
Bas Hendriks Avatar asked Apr 24 '12 11:04

Bas Hendriks


2 Answers

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>
like image 96
Dimitre Novatchev Avatar answered Sep 27 '22 21:09

Dimitre Novatchev


You simply want

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

instead of the xsl:if.

like image 24
Martin Honnen Avatar answered Sep 27 '22 21:09

Martin Honnen