title: How to declare variable inside xsl:perform-sort
body: <p>I have a sophiscated sorting like this:</p>
  
  <pre><code>        &lt;xsl:perform-sort select="$nodes"&gt;
          &lt;xsl:sort select="if (p:isPreform(.) and not(p:getPreformPart(., $nodes))) then 1 else 0"/&gt;
          &lt;xsl:sort select="p:getTargetPart(., $nodes)/../@pos"/&gt;
          &lt;xsl:sort select="if (sort:isGroupElement(p:getTargetPart(., $nodes), $subsectionId)) then
                                  sort:getGroupId(p:getTargetPart(., $nodes), $subsectionId)
                            else
                                  p:getTargetPart(., $nodes)/@name"/&gt;
          &lt;xsl:sort select="if (sort:isGroupElement(p:getTargetPart(., $nodes), $subsectionId)) then 0 else 1"/&gt;
          &lt;xsl:sort select="replace(p:getTargetPart(., $nodes)/@number, '[А-Я]+$','')"/&gt;
          &lt;xsl:sort select="if (p:isPreform(.)) then 1 else 0"/&gt;
          &lt;xsl:sort select="@number"/&gt;
      &lt;/xsl:perform-sort&gt;
  </code></pre>
  
  <p>You can see repeating constructions inside. How I can refactor this? Specification not allow declare variables inside  if that were the case - same fragment may look like:</p>
  
  <pre><code>        &lt;xsl:perform-sort select="$nodes"&gt;
          &lt;xsl:variable name="isPreform" select="p:isPreform(.)" as="xs:boolean"/&gt;
          &lt;xsl:variable name="target" select="p:getTargetPart(., $nodes)" as="element()"/&gt;
          &lt;xsl:variable name="isGrouped" select="sort:isGroupElement($target, $subsectionId)" as="xs:boolean"/&gt;
  
          &lt;xsl:sort select="if ($isPreform and not(p:getPreformPart(., $nodes))) then 1 else 0"/&gt;
          &lt;xsl:sort select="$target/../@pos"/&gt;
          &lt;xsl:sort select="if ($isGrouped) then sort:getGroupId($target, $subsectionId) else $target/@name"/&gt;
          &lt;xsl:sort select="if ($isGrouped) then 0 else 1"/&gt;
          &lt;xsl:sort select="replace($target/@number, '[А-Я]+$','')"/&gt;
          &lt;xsl:sort select="if ($isPreform) then 1 else 0"/&gt;
          &lt;xsl:sort select="@number"/&gt;
      &lt;/xsl:perform-sort&gt;
  </code></pre>
  
  <p>Maybe I miss something? And I can sort this by another way?</p>
  
  <p>In advance thanks! Andrey.</p>