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 Style sheet 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 style sheet
3. Add a link to the style sheet
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”> |
Then you can start developing the XSL style sheet. How you can develop the style sheet will be covered later.
3. Add a link to the style sheet
Once you have created the style sheet now you can add a link to the style sheet 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 style sheet is linked in the XML document.
Example:
<?xml-stylesheet type=”text/xsl” href=”sample.xsl”?> |
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>
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=”/”> |
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’/> |
<?/DATA/DEPARTMENT/NAME?> |
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> |
<?for-each:/DATA/DEPARTMENT?> <?NAME?> <?end for-each?> |
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> |
Example:
<xsl:sort select=’NAME’ data-type=’text’ order=’descending’/> |
<?sort:NAME;’ascending’;data-type=’text’?> |
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> |
<?for-each:/DATA/DEPARTMENT?> <?if:NAME=’Consulting’?> <?NAME?> <?end if?> <?end for-each?> |
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> |
<?for-each:/DATA/DEPARTMENT?> <?choose:?> <?when:NAME=’Consulting’> <?MANAGER_NAME?> <?DEPARTMENT_NAME?> <?end when?> <?otherwise:?> <?DEPARTMENT_NAME?> <?end otherwise?> <?end choose?> <?end for-each?> |
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 functionality 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 visaversa. When you develop the RTF Template you can use the BI Publisher’s tags then at the run time 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 logic’s 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 logic’s 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.
Recent Comments