Home > Mobile >  XSLT greater than / less than working backwords...?
XSLT greater than / less than working backwords...?

Time:02-26

This is kinda driving me crazy... I have an xslt that is passing a value based on choose/when... It works, but the problem is it's working backwards. Unless I'm thinking of the equation the wrong way... Here is a section from the XML...

<LayoutIntent Class="Intent" Status="Available" ID="id00010">
         <FinishedDimensions DataType="ShapeSpan" Preferred="867.330708661417 498.897637795276 0.0"/>
</LayoutIntent>

and here is the xslt section...

<xsl:variable name="width">
    <xsl:value-of select="format-number(number(substring(ResourcePool/LayoutIntent/FinishedDimensions/@Preferred,1,16))div 72,'#.##')"/>
</xsl:variable>
<xsl:variable name="height">
    <xsl:value-of select="format-number(number(substring(ResourcePool/LayoutIntent/FinishedDimensions/@Preferred,18,16))div 72,'#.##')"/>
</xsl:variable>
<finishedWidth>
    <xsl:value-of select="$width"/>
</finishedWidth>
<finishedHeight>
    <xsl:value-of select="$height"/>
</finishedHeight>
<xsl:variable name="code">
    <xsl:choose>
        <xsl:when test="$width &gt; $height">1</xsl:when>
        <xsl:when test="$width &lt; $height">0</xsl:when>
    </xsl:choose>
</xsl:variable>
<orientation>
    <xsl:value-of select="$code"/>
</orientation>

If the width is greater than the height I would expect 1, but I get 0 and vice versa

CodePudding user response:

Compare numbers, not formatted strings. You have two issues causing it to compare greater than or less than of strings.

  1. You are using xsl:value-of to select a string
  2. You are applying format-number() to produce a string

Remove the value-of and have the variable select the numeric product of the division, and move the format-number() down to where you want to print the formatted number.

And you might consider changing that second xsl:where to xsl:otherwise. No sense in repeating the logic for the oppposite if it's going to be one or the other.

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" indent="yes"/>
    
    <xsl:template match="/">    
        
        <xsl:variable name="width" as="item()" select="number(substring(ResourcePool/LayoutIntent/FinishedDimensions/@Preferred,1,16))div 72"/>
        <xsl:variable name="height" as="item()" select="number(substring(ResourcePool/LayoutIntent/FinishedDimensions/@Preferred,18,16))div 72"/>

    <finishedWidth>
        <xsl:value-of select="format-number($width,'#.##')"/>
    </finishedWidth>
    <finishedHeight>
        <xsl:value-of select="format-number($height,'#.##')"/>
    </finishedHeight>
    <xsl:variable name="code">
        <xsl:choose>
            <xsl:when test="$width &gt; $height">1</xsl:when>
            <xsl:otherwise>0</xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
    <orientation>
        <xsl:value-of select="$code"/>
    </orientation>
    </xsl:template>
    
</xsl:stylesheet>
  • Related