0

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>
Mark P.
  • 1,827
  • 16
  • 37
mark3569957
  • 32
  • 1
  • 2
  • 7

1 Answers1

1

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'?>
mark3569957
  • 32
  • 1
  • 2
  • 7
  • To be clear, I believe the statement should be you cannot conditionally apply a sort. Certainly you can have conditional (if, choose/when) formatting inside a for-each. But you cannot conditionally apply a sort. The solution presented is a good one and this was also covered here: http://stackoverflow.com/questions/16695538/xslsort-does-not-work-together-with-xslchoose-or-if – Kevin Brown Apr 28 '15 at 23:20