XSL - how to set variable as integer if it comes from string

10,593

First, a pure XML issue, nothing to do with XSLT: "<" must be escaped as "&lt;". (But with XSLT 2.0, it's often more convenient to use the "lt" operator rather than "<").

Second point: don't put your numbers in quotes. Write 50, not '50'.

Third: it's a good idea to declare the types of your variables, that way it's much clearer what's going on. e.g.

<xsl:variable name="JJJ" select="format-number(...)" as="xs:string"/>

Finally, if you want to add a string and a number, convert the string to a number first using xs:integer(), xs:double(), or the number() function.

Share:
10,593
MiG
Author by

MiG

Updated on July 23, 2022

Comments

  • MiG
    MiG almost 2 years

    XPTY0004 : The operator ' + ' is not defined for operands of type xs:string and xs:integer

    IMPUT:

    <?xml version="1.0" encoding="UTF-8"?>
    <LeveL Plan="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    
    
    <space2000>
    <values>
    <value jak="77" rak="12"</value>
    <value jak="66" rak="345"></value>
    <value jak="-50" rak="67"></value>
    <value jak="-30" rak="-89">1</value>
    </values> 
    
    </space2000>
    
    
    </LeveL>
    

    MY TRY:

    <xsl:stylesheet version="2.0" 
    xmlns:fn ="http://www.w3.org/2005/xpath-functions"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <xsl:template match="/">
    <xsl:for-each select="(LeveL/space2000/values)">
    <xsl:variable name="JJJ"  select="(format-number(((value/@jak) div 2),'0'))"/>
    <xsl:variable name="RRR"  select="format-number(((value/@rak) div 2),'0')"/>
    <xsl:text>
    O
    </xsl:text>
    <xsl:text>LA</xsl:text>
    <xsl:value-of select="$JJJ"></xsl:value-of>
    <xsl:text> </xsl:text><xsl:text>LO</xsl:text>
    <xsl:value-of select="$RRR"></xsl:value-of>
    <xsl:text>
    O
    </xsl:text>
    
    <xsl:choose>
         <xsl:when test="( $JJJ > '0')">
            <xsl:value-of select="A"/>
         </xsl:when>
         <xsl:when test="( $JJJ < '0')">
            <xsl:value-of select="B"/>
         </xsl:when>
           <xsl:when test="( $RRR > '0')">
            <xsl:value-of select="C"/>
         </xsl:when>
         <xsl:when test="( $RRR < '0')">
            <xsl:value-of select="D"/>
         </xsl:when>   
         <xsl:when test="( ( $RRR + '50') < '0' )">
            <xsl:value-of select="X"/>
         </xsl:when>
         <xsl:when test="( '-100' < ( $RRR + '1') < '100' )">
            <xsl:value-of select="X"/>
         </xsl:when>
    
    
    </xsl:choose>
    
    </xsl:for-each>
    
    </xsl:template>
    
    </xsl:stylesheet>
    

    so I would like ro recive something like: A C B D X

    but xsl

    dont't like + ( is not trusting its integer :)

    don"t like < ( the other way > is for him acceptable :)

    SO MY QUESTION IS

    a) How to set variable (or element) as integer able to do mathematics?

    b) How to test if variable is bigger than -100 and smaller than 100 ?

    PLEASE HEEEEEELP :)