Search code examples
xsltobieebi-publisher

BI Publisher conditional sorting issue


I am having trouble getting BI Publisher to sort based on my parameter. The parameter name in the data model is SORTBY and has two options (NAME, BIRTH_DATE). The values come from a "list of values" in the data model that match field names in the result set. I want the report to sort by the field selected by the user.

Below is my code in the RTF template using BI Pub tags:

<?param@begin:SORTBY?>
<?for-each:G_1?>
<?if:SORTBY='NAME'?>
  <?sort:NAME;'ascending';'text'?>
<?end if?>
<?if:SORTBY='BIRTH_DATE'?>
  <?sort:BIRTH_DATE;'ascending';'text'?>
<?end if?>

And here is the relevant resulting xsl-fo exported from Microsoft Word:

<xsl:for-each select=".//G_1" xdofo:ctx="3">
  ...
  <xsl:if test=".//SORTBY = 'NAME'" xdofo:ctx="3">
    <xsl:sort select="(.//NAME)[1]" order="ascending" data-type="text"/>
  </xsl:if>
  <xsl:if test=".//SORTBY = 'BIRTH_DATE'" xdofo:ctx="3">
    <xsl:sort select="(.//BIRTH_DATE)[1]" order="ascending" data-type="text"/>
  </xsl:if>
  ...
</xsl:for-each>

Solution

  • With some peer programming, a colleague of mine found the answer. The for-each does not like having an if (or a choose for that matter) inside. So, you have to assign the parameter to a variable and use the variable in the sort.

    Outside the for loop:

    <?xdoxslt:set_variable($_XDOCTX,'Order',SORTBY)?>
    

    For loop:

    <?for-each:G_1?>
    <?sort:./*[name() = xdoxslt:get_variable($_XDOCTX,'Order')];'ascending';'text'?>