Creating a Columnar Report

Problem

You want to format data into columns for presentation.

Solution

There are two general kinds of XML-to-columnar mappings. The first maps different elements or attributes into separate columns. The second maps elements based on their relative position.

Before tackling these variations, you need a generic template that will help justify output text into a fixed-width column. You can build such a routine, shown in Example 5-17, on top of the str:dup template you created in Recipe 1.5.

Example 5-17. Generic text-justification template—text.justify.xslt

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:str="http://www.ora.com/XSLTCookbook/namespaces/strings" xmlns:text="http://www.ora.com/XSLTCookbook/namespaces/text" extension-element-prefixes="text"> <xsl:include href="../strings/str.dup.xslt"/> <xsl:template name="text:justify"> <xsl:param name="value" /> <xsl:param name="width" select="10"/> <xsl:param name="align" select=" 'left' "/> <!-- Truncate if too long --> <xsl:variable name="output" select="substring($value,1,$width)"/> <xsl:choose> <xsl:when test="$align = 'left'"> <xsl:value-of select="$output"/> <xsl:call-template name="str:dup"> <xsl:with-param name="input" select=" ' ' "/> <xsl:with-param name="count" select="$width - string-length($output)"/> </xsl:call-template> </xsl:when> <xsl:when test="$align = 'right'"> <xsl:call-template name="str:dup"> <xsl:with-param name="input" select=" ' ' "/> <xsl:with-param ...

Get XSLT Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.