Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

XSLT greater than / less than working backwords…?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading