tags:

views:

54

answers:

3

How to check the integer value in XSL ??? I'm used the version 1.0

My Code:

<xsl:variable name="ShowEmailEventId"
     select="com.zoniac.emailevent.NewEmailEventBean/emailEventIdString"/>
<xsl:if test="$ShowEmailEventId !=48">
    <table align="center"
           width="96%"
           border="1"
           style="border-color:#2E73AD;border-collapse:collapse"
           cellspacing="0"
           cellpadding="10">
        <tr>
            <td width="10%"
                style="border-color:#2E73AD;color: black; font: 11px verdana;padding:2px"
                align="left"
                valign="top">
                <b>S.No</b>
            </td>
        </tr>
    </table>
</xsl:if>
+1  A: 

TO check if a value nameofint is an int... (you are obviously going to want to change the inside of the if condition.

<xsl:template match="CheckInt">
   <xsl:if test="not(string(.) castable as xs:integer)">
    <xsl:text>NOT AN INT: </xsl:text>
    <xsl:value-of select="."/>
  </xsl:if>
 </xsl:template>
Nix
@Nix: This is XSLT 2.0
Alejandro
So? I don't see a version specified?
Nix
Now i'm used the version 1.0
AdalArasan
@Nix, XSLT 1.0 doesn't have "castable as".
LarsH
A: 
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
         xmlns:xs="http://www.w3.org/2001/XMLSchema"
         version="2.0">

      <xsl:output method="text"/>

      <!-- Don't output text nodes. We just want error messages. -->
      <xsl:template match="text()"/>

      <!-- Make sure that element attributes get processed. -->
      <xsl:template match="*">
        <xsl:apply-templates select="@*|node()"/>
      </xsl:template>

      <!-- Type checking templates: -->

      <xsl:template match="quantity">
        <xsl:if test="not(string(.) castable as xs:integer)">
          <xsl:text>
    Following quantity value not an integer: </xsl:text>
          <xsl:value-of select="."/>
        </xsl:if>
      </xsl:template>

      <xsl:template match="shipped">
        <xsl:if test="not(string(.) castable as xs:boolean)">
          <xsl:text>
    Following shipped value not a boolean: </xsl:text>
          <xsl:value-of select="."/>
        </xsl:if>
      </xsl:template>

      <xsl:template match="price">
        <xsl:if test="not(string(.) castable as xs:decimal)">
          <xsl:text>
    Following price value not a decimal number: </xsl:text>
          <xsl:value-of select="."/>
        </xsl:if>
      </xsl:template>

  <xsl:template match="@shipDate">
    <xsl:if test="not(string(.) castable as xs:date)">
      <xsl:text>
Following shipDate value not a date: </xsl:text>
      <xsl:value-of select="."/>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>
Enrico Pallazzo
@Wouter van Oranje: This is XSLT 2.0
Alejandro
+1  A: 

This is probably the shortest expression, returning true() iff $x can be used as an integer:

Just use:

floor($x) = $x

The full test will be:

<xsl:if test="floor($x) = $x">
 <!-- $x is an integer -->
</xsl:if>

or

<xsl:when test="floor($x) = $x">
 <!-- $x is an integer -->
</xsl:when>

or

someXPathExpression[floor($x) = $x]
Dimitre Novatchev
Is this floor() method is used in XSL 1.0 ?
AdalArasan
@AdalArasan : Yes, this is a standard XPath 1.0 function and XSLT 1.0 uses XPath 1.0. See: http://www.w3.org/TR/xpath/#function-floor
Dimitre Novatchev