title: How to store image width as a number in a variable with xsl
body: <p>I'm trying to store an image width as a number so I can add a class value to the figure wrapper so I can control the image placement with css, (small, medium, or large image).</p>
  
  <p>This is my xml example:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;root&gt;
      &lt;section&gt;
          &lt;figure class="informalfigure"&gt;
              &lt;img src="../images/image-fpo-1.png" alt="" width="250" height="800"/&gt;
          &lt;/figure&gt;
          &lt;figure class="informalfigure"&gt;
              &lt;img src="../images/image-fpo-2.png" alt="" width="650" height="800"/&gt;
          &lt;/figure&gt;
          &lt;figure class="informalfigure"&gt;
              &lt;img src="../images/image-fpo-3.png" alt="" width="1250" height="800"/&gt;
          &lt;/figure&gt;
      &lt;/section&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>This is my xslt:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0"&gt;
  
  &lt;xsl:variable name="small-image"&gt;
      &lt;xsl:number value="200"/&gt;
  &lt;/xsl:variable&gt;
  &lt;xsl:variable name="medium-image"&gt;
      &lt;xsl:number value="500"/&gt;
  &lt;/xsl:variable&gt;
  &lt;xsl:variable name="large-image"&gt;
      &lt;xsl:number value="1000"/&gt;
  &lt;/xsl:variable&gt;
  
  &lt;xsl:template match="node()"&gt;
      &lt;xsl:copy&gt;
          &lt;xsl:copy-of select="@*"/&gt;
          &lt;xsl:apply-templates/&gt;
      &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="figure"&gt;
      &lt;xsl:variable name="classvalue" select="@class"/&gt;
      &lt;xsl:variable name="img-width" select="number(img[@width])"/&gt;
  
      &lt;xsl:choose&gt;
          &lt;xsl:when test="number($img-width) &gt; number($large-image)"&gt;
              &lt;figure class="{$classvalue} large"&gt;
                  &lt;xsl:apply-templates/&gt;
              &lt;/figure&gt;
          &lt;/xsl:when&gt;
          &lt;xsl:when test="number($img-width) &gt; number($medium-image)"&gt;
              &lt;figure class="{$classvalue} medium"&gt;
                  &lt;xsl:apply-templates/&gt;
              &lt;/figure&gt;
          &lt;/xsl:when&gt;
          &lt;xsl:when test="number($img-width) &gt; number($small-image)"&gt;
              &lt;figure class="{$classvalue} small"&gt;
                  &lt;xsl:apply-templates/&gt;
              &lt;/figure&gt;
          &lt;/xsl:when&gt;
          &lt;xsl:otherwise&gt;
              &lt;figure class="{$classvalue} missedit" width="{$img-width}"&gt;
                  &lt;xsl:apply-templates/&gt;
              &lt;/figure&gt;
          &lt;/xsl:otherwise&gt;
      &lt;/xsl:choose&gt;
  &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>The variable "img-width" is causing the hangup. I clearly have something wrong there. How would I go about storing the image width as a number variable so my test will work? Any help is greatly appreciated. Thanks, Jon</p>