Tuesday, March 17, 2009

XSLT for BI Publisher

What is XSLT?

Today, I’ll cover the basic of the XSLT. Basic development of BI Publisher RTF Template doesn’t require the XSLT coding. However, when you start developing advanced reports having a good understanding of the XSLT will help you to develop the RTF Template efficiently and provide richer functionality. I’ll cover the benefit of using XSLT at the end of this posting.

XSLT stands for XSL Transformations and XSL stands for Extensible Stylesheet Language. XSLT is a language for transforming XML documents into another form of documents such as XML, HTML, XHTML, etc. When you use XSLT to transform the original XML documents you will also use XPath to navigate through the documents. So in this process of transforming the documents you use a combination of XSLT and XPath together.


How to use?

Here is a list of basic steps to use XSLT.

1. Declare XSL namespace
2. Create XSL stylesheet
3. Add a link to the stylesheet


1. Declare XSL namespace

First you need to declare XSL namespace like follows at the top of the XML document.

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


2. Create XSL stylesheet

Then you can start developing the XSL stylesheet. How you can develop the stylesheet will be covered later.

3. Add a link to the stylesheet

Once you have created the stylesheet now you can add a link to the stylesheet in the original XML document. Then when you process the XML document whatever the standard XSL processor you’re using would transform the XML document following the transformation rules designed in the XSL stylesheet is linked in the XML document.

Example:


<?xml-stylesheet type="text/xsl" href="sample.xsl"?>


XSL Element (or function)

XSL transformation rules are defined with XSL Elements. Here is a set of the most commonly used XSL elements.

  • <xsl:template>
  • <xsl:value-of>
  • <xsl:for-each>
  • <xsl:sort>
  • <xsl:if>
  • <xsl:choose>
  • <xsl:apply-templates>


Since most of the XSLT coding can be also done by the native BI Publisher coding, at each of the following Element example section I’ll also show how to do the same with the BI Publisher coding.

XSL:TEMPLATE

This element is used to specify what part of the XML document should be transformed or be applied with the rules described in the XSL stylesheet. You can use ‘match’ attribute specifying the section of the XML with XPath. For example, if you specify ‘/’ (Root) then the XSL stylesheet will be applied to the entire XML document.

Example:


<xsl:template match="/">

XSL:VALUE-OF

This element is used to retrieve a value from a specified Element or Attribute. For example, if you used this element specifying ‘/DATA/DEPARTMENT/NAME’ then you can retrieve a value that is presented in the NAME element in the XML document.

Example:


<xsl:value-of select=’/DATA/DEPARTMENT/NAME’/>

With BI Publisher tags, you can just type the following to do the same.


<?/DATA/DEPARTMENT/NAME?>

XSL:FOR-EACH

When you just use the <xsl:value-of> element you will get the first value of the specified element. With the above case, you will get the first Department name. But what if you have 5 departments and want to display all the names together?

You can use this ‘for-each’ element to repeat through a specified node. It works the same way as ‘for loop’ in any typical programming language such as C, Java. For example, if you specify ‘/DATA/DEPARTMENT’ as a node and use the ‘for-each’ element to repeat then the XSL processor will repeat through the Department node and display all the department names.

Example:

<xsl:for-each select=’/DATA/DEPARTMENT’>
  <xsl:value-of select=’NAME’/>
</xsl:for-each>


With BI Publisher tags, you can type the following to do the same.

<?for-each:/DATA/DEPARTMENT?>
  <?NAME?>
<?end for-each?>

XSL:SORT

Inside the previous ‘for-each’ loop you might want to sort the data by alphabetically or based on the ID, etc. You can use this ‘sort’ element to do the sorting.

Example:

<xsl:for-each select=’/DATA/DEPARTMENT’>
<xsl:sort select=’NAME’/>
  <xsl:value-of select=’NAME’/>
</xsl:for-each>


Also you can specify the data type and whether it should be ascending or descending order.

Example:

  <xsl:sort select=’NAME’ data-type=’text’ order=’descending’/>

With BI Publisher you can do the following to achieve the same.

  <?sort:NAME;'ascending';data-type='text'?>

XSL:IF

You can use this element to have a condition in the XSL transformation logic. This is also pretty much the same as other programming language’s ‘if’ condition. For example, if you want to display manager name only when Department name is ‘Consulting’ you can specify something like the below.

Example:

<xsl:for-each select=’/DATA/DEPARTMENT’>
  <xsl:if test=”NAME=’Consulting’”>
    <xsl:value-of select=’MANAGER_NAME’/>
  </xsl:if>
</xsl:for-each>


You can do the same with BI Publisher tags as follows.

<?for-each:/DATA/DEPARTMENT?>
  <?if:NAME=’Consulting’?>
    <?NAME?>
  <?end if?>
<?end for-each?>


XSL:CHOOSE

As an alternative or for better reasons you can also use CHOOSE/WHEN elements to do the condition. One thing to note is that XSL doesn’t support IF/ELESE condition as native, so if you have multiple conditions to use together in a form of IF/ELSE then CHOOSE/WHEN/OTHERWISE elements would serve you better.

Example:

<xsl:for-each select=’/DATA/DEPARTMENT’>
  <xsl:choose>
  <xsl:when ”NAME=’Consulting’”>
    <xsl:value-of select=’MANAGER_NAME’/> 
    <xsl:value-of select=’DEPARTMENT_NAME’/>
  </xsl:when>
  <xsl:otherwise>
    <xsl:value-of select=’DEPARTMENT_NAME’/>
  </xsl:otherwise>
</xsl:for-each>

With BI Publisher you can do the below to have the same condition.

<?for-each:/DATA/DEPARTMENT?>
  <?choose:?>
  <?when:NAME=’Consulting’>
    <?MANAGER_NAME?>
    <?DEPARTMENT_NAME?>
  <?end when?>
  <?otherwise:?>
    <?DEPARTMENT_NAME?>
  <?end otherwise?>
  <?end choose?>
<?end for-each?>


Why XSLT is needed for BI Publisher?

Though you can type any XSLT coding and BI Publisher can understand it, typical RTF Template development doesn’t require the use of the XSLT. In fact BI Publisher’s tags, which are surrounded by ‘?’ marks, covers the native XSLT functionalities for the most so that you don’t need to type the XSLT code. As you have seen, what you can do with XSLT can also be done by the BI Publisher tags and visa versa. When you develop the RTF Template you can use the BI Publisher’s tags then at the runtime BI Publisher translate the tags to the XSL codes internally. BI Publisher’s tags are there to make the template development much easier for those who do not have any XML/XSL or programming experience.

However, there are certain situations where you might want to use XSLT over BI Publisher’s tags. Such situation includes the Chart development and variable handling. As you might have known behind the Chart definition is XSLT code. You can insert a chart from the Template Builder (MS Word Add-in) Chart wizard to start with. But once you want to customize the default chart formatting or logics to handle the data then you need to modify the XSLT code behind the Chart definition.

Also, sometimes it’s very useful when you want to use variables. There is a BI Publisher’s tag for variable handling but sometimes I find using XSLT’s variable is easier though it depends on the requirements.

Lastly, you can create a set of custom functions in XSL template and call them from your RTF Template as external functions. This is very useful especially when you have common logics that contain custom functions or calculations and can be used in many different BI Publisher’s text forms or different templates, yet don’t want to maintain them in each text form or each template. Once you have developed a single XSL template where you create such logics or functions then you can import the XSL template from any of your RTF template and call any of the custom function to do the same process.

I’ll cover XSL-FO tomorrow, but based on my experience the last two languages, XPath & XSLT are the most useful to advance our BI Publisher RTF Template development. That says you can start take advantage of the XPath & XSLT now and have a lot of fun with your BI Publisher template development! Enjoy!

5 comments:

  1. ITemplatez.com offers professional web templates, flash templates ,swish templates, dreamweaver templates, and other web design productsavailable for immediate download.

    ReplyDelete
  2. Hi.
    What if you wanted to show some measure based on multiple conditions? For example: amount(SUM) if account number is X, Y or Z?

    ReplyDelete
  3. Hi,
    We have a requirement where we need to get the pagecount and assign the value to a variable. { NUMPAGES } can be used to get the value however its value cannot be assigned to any variable. We have declared a variable which increments at each pages header. However since the initialization is also in the same header the variable again resets to 1 and the final page count comes to be 1. How can we intialize this variable before the header?

    ReplyDelete
  4. Hi,

    We are doing an upgrade from Actuate to BI publisher 10.1.3.4.1.In that we have date in format :09/23/2010 08:02:57. Now our requirement is to get the Week number for the date, but we can use the format-date if the date is in format YYYY-MM-DDThh:mm:ss+HH:MM.
    But i am not able to convert the date in above format and use the format date.
    So kinldy help me in getting the week number for the above format.

    regards,
    Shaddy.

    ReplyDelete
  5. @shadab ahmad, I am not sure if you found answer for your query. The solution should look something like [code] [/code]
    in your RTF or XSL template.
    This function works for me.

    ReplyDelete