XML technologies / XSLT / XSLT Cookbook / From XML to text / Creating a columnar report
(Excerpt from “XSLT Kochbuch” by Sal Mangano)
Problem
You want to format data for display in columns.
Solution
There are two general ways to map XML to columns: assign different elements or attributes to separate columns, or map elements according to their relative positions.
First, create a generic template to align output text in a fixed-width column. Build the routine shown below on the str:dup template created in recipe Duplicating a string n times.
Example: Generic template for aligning text – 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' "/>
<!-- Abschneiden, falls zu lang -->
<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 name="count" select="$width - string-length($output)"/>
</xsl:call-template>
<xsl:value-of select="$output"/>
</xsl:when>
<xsl:when test="$align = 'center'">
<xsl:call-template name="str:dup">
<xsl:with-param name="input" select=" ' ' "/>
<xsl:with-param name="count" select="floor(($width - string-length($output)) div 2)"/>
</xsl:call-template>
<xsl:value-of select="$output"/>
<xsl:call-template name="str:dup">
<xsl:with-param name="input" select=" ' ' "/>
<xsl:with-param name="count" select="ceiling(($width - string-length($output)) div 2)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>INVALID ALIGN</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>With this template, columnar output is simply a matter of specifying data order and column layouts. The next two examples do this for person attributes in people.xml. A similar solution could handle the element encoding in people-elem.xml.
Example: people-to-columns.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">
<xsl:include href="text.justify.xslt"/>
<xsl:output method="text" />
<xsl:strip-space elements="*"/>
<xsl:template match="people">
Name | Alter | Geschlecht | Raucher |
----------------------------------------------------------
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="person">
<xsl:call-template name="text:justify">
<xsl:with-param name="value" select="@name"/>
<xsl:with-param name="width" select="20"/>
</xsl:call-template>
<xsl:text>|</xsl:text>
<xsl:call-template name="text:justify">
<xsl:with-param name="value" select="@age"/>
<xsl:with-param name="width" select="8"/>
<xsl:with-param name="align" select=" 'right' "/>
</xsl:call-template>
<xsl:text>|</xsl:text>
<xsl:call-template name="text:justify">
<xsl:with-param name="value" select="@sex"/>
<xsl:with-param name="width" select="11"/>
<xsl:with-param name="align" select=" 'center' "/>
</xsl:call-template>
<xsl:text>|</xsl:text>
<xsl:call-template name="text:justify">
<xsl:with-param name="value" select="@smoker"/>
<xsl:with-param name="width" select="10"/>
<xsl:with-param name="align" select=" 'center' "/>
</xsl:call-template>
<xsl:text> </xsl:text>
</xsl:template>
</xsl:stylesheet>Example: Output.
|
|
|
|
|
|
|
|
----------------------------------------------------------------------- | |||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Position-based transformation needs a different approach. First decide the number of columns: use a parameter and let the item count determine rows, or specify rows and let columns vary. Then decide how positions map to columns. The commonest mappings are row-major (row-major) and column-major (column-major). Row-major mapping puts the first item in the first column, the second in the second, and so on until the columns are filled, then starts a new row. Column-major mapping puts the first (N div Anzahl-der-Spalten) items in the first column, the next (N div Anzahl-der-Spalten) in the second, and so on. Think of this as transposing rows into columns.
Create two templates to output columns in either arrangement, as shown below.
Example: text.matrix.xslt
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:text="http://www.ora.com/XSLTCookbook/namespaces/text" extension-element-prefixes="text">
<xsl:output method="text"/>
<xsl:include href="text.justify.xslt"/>
<xsl:template name="text:row-major">
<xsl:param name="nodes" select="/.."/>
<xsl:param name="num-cols" select="2"/>
<xsl:param name="width" select="10"/>
<xsl:param name="align" select=" 'left' "/>
<xsl:param name="gutter" select=" ' ' "/>
<xsl:if test="$nodes">
<xsl:call-template name="text:row">
<xsl:with-param name="nodes" select="$nodes[position() <= $num-cols]"/>
<xsl:with-param name="width" select="$width"/>
<xsl:with-param name="align" select="$align"/>
<xsl:with-param name="gutter" select="$gutter"/>
</xsl:call-template>
<!-- Verarbeiten der verbleibenden Zeilen -->
<xsl:call-template name="text:row-major">
<xsl:with-param name="nodes" select="$nodes[position() > $num-cols]"/>
<xsl:with-param name="num-cols" select="$num-cols"/>
<xsl:with-param name="width" select="$width"/>
<xsl:with-param name="align" select="$align"/>
<xsl:with-param name="gutter" select="$gutter"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template name="text:col-major">
<xsl:param name="nodes" select="/.."/>
<xsl:param name="num-cols" select="2"/>
<xsl:param name="width" select="10"/>
<xsl:param name="align" select=" 'left' "/>
<xsl:param name="gutter" select=" ' ' "/>
<xsl:if test="$nodes">
<xsl:call-template name="text:row">
<xsl:with-param name="nodes" select="$nodes[(position() - 1) mod ceiling(last() div $num-cols) = 0]"/>
<xsl:with-param name="width" select="$width"/>
<xsl:with-param name="align" select="$align"/>
<xsl:with-param name="gutter" select="$gutter"/>
</xsl:call-template>
<!-- Verarbeiten der verbleibenden Zeilen -->
<xsl:call-template name="text:col-major">
<xsl:with-param name="nodes" select="$nodes[(position() - 1) mod ceiling(last() div $num-cols) != 0]"/>
<xsl:with-param name="num-cols" select="$num-cols"/>
<xsl:with-param name="width" select="$width"/>
<xsl:with-param name="align" select="$align"/>
<xsl:with-param name="gutter" select="$gutter"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template name="text:row">
<xsl:param name="nodes" select="/.."/>
<xsl:param name="width" select="10"/>
<xsl:param name="align" select=" 'left' "/>
<xsl:param name="gutter" select=" ' ' "/>
<xsl:for-each select="$nodes">
<xsl:call-template name="text:justify">
<xsl:with-param name="value" select="."/>
<xsl:with-param name="width" select="$width"/>
<xsl:with-param name="align" select="$align"/>
</xsl:call-template>
<xsl:value-of select="$gutter"/>
</xsl:for-each>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>The next three examples show these templates in use.
Example: input.
<numbers>
<number>10</number>
<number>3.5</number>
<number>4.44</number>
<number>77.7777</number>
<number>-8</number>
<number>1</number>
<number>444</number>
<number>1.1234</number>
<number>7.77</number>
<number>3.1415927</number>
<number>10</number>
<number>9</number>
<number>8</number>
<number>7</number>
<number>666</number>
<number>5555</number>
<number>-4444444</number>
<number>22.33</number>
<number>18</number>
<number>36.54</number>
<number>43</number>
<number>99999</number>
<number>999999</number>
<number>9999999</number>
<number>32</number>
<number>64</number>
<number>-64.0001</number>
</numbers>Example: stylesheet.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:text="http://www.ora.com/XSLTCookbook/namespaces/text">
<xsl:output method="text" />
<xsl:include href="text.matrix.xslt"/>
<xsl:template match="numbers">
Fünf Spalten mit Zahlen in zeilenweiser Anordnung:
<xsl:text/>
<xsl:call-template name="text:row-major">
<xsl:with-param name="nodes" select="number"/>
<xsl:with-param name="align" select=" 'right' "/>
<xsl:with-param name="num-cols" select="5"/>
<xsl:with-param name="gutter" select=" ' | ' "/>
</xsl:call-template>
Fünf Spalten mit Zahlen in spaltenweiser Anordnung:
<xsl:text/>
<xsl:call-template name="text:col-major">
<xsl:with-param name="nodes" select="number"/>
<xsl:with-param name="align" select=" 'right' "/>
<xsl:with-param name="num-cols" select="5"/>
<xsl:with-param name="gutter" select=" ' | ' "/>
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>Example: Output.
Fünf Spalten mit Zahlen in zeilenweiser Anordnung:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Fünf Spalten mit Zahlen in spaltenweiser Anordnung:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
|
|
|
| |
|
|
|
|
XSLT 2.0
The main improvement in XSLT 2.0 is converting template text:justify into a function, using XPath 2.0 features to make it more concise. Use function string-join with a for expression to build a padding function inserting the right amount of whitespace. Also overload text:justify to simulate a default alignment parameter:
<xsl:function name="text:dup" as="xs:string">
<xsl:param name="input" as="xs:string"/>
<xsl:param name="count" as="xs:integer"/>
<xsl:sequence select="string-join(for $i in 1 to $count return $input, '')"/>
</xsl:function>
<xsl:function name="text:justify" as="xs:string">
<xsl:param name="value" as="xs:string"/>
<xsl:param name="width" as="xs:integer" />
<xsl:sequence select="text:justify($value, $width, 'left')"/>
</xsl:function>
<xsl:function name="text:justify" as="xs:string">
<xsl:param name="value" as="xs:string"/>
<xsl:param name="width" as="xs:integer" />
<xsl:param name="align" as="xs:string" />
<!-- Abschneiden, falls zu lang -->
<xsl:variable name="output" select="substring($value,1,$width)" as="xs:string"/>
<xsl:variable name="offset" select="$width - string-length($output)" as="xs:integer"/>
<xsl:choose>
<xsl:when test="$align = 'left'">
<xsl:value-of select="concat($output, text:dup(' ', $offset))"/>
</xsl:when>
<xsl:when test="$align = 'right'">
<xsl:value-of select="concat(text:dup(' ', $offset), $output)"/>
</xsl:when>
<xsl:when test="$align = 'center'">
<xsl:variable name="before" select="$offset idiv 2"/>
<xsl:variable name="after" select="$before + $offset mod 2"/>
<xsl:value-of select="concat(text:dup(' ', $before), $output,text:dup(' ', $after))"/>
</xsl:when>
<xsl:otherwise>INVALID ALIGN</xsl:otherwise>
</xsl:choose>
</xsl:function>Discussion
Converting element- or attribute-encoded data to columns resembles the delimited-data problem in recipe Export XML as delimited data. The main difference is that delimited data targets machines, whereas this output targets people. People can be more demanding about alignment and visual aids. The same generic data-driven approach is possible, but needs more information about each column to format it correctly. The next three examples show the attribute-based solution.
Example: generic-attr-to-columns.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">
<xsl:include href="text.justify.xslt"/>
<xsl:param name="gutter" select=" ' ' "/>
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="columns" select="/.."/>
<xsl:template match="/">
<xsl:for-each select="$columns">
<xsl:call-template name="text:justify" >
<xsl:with-param name="value" select="@name"/>
<xsl:with-param name="width" select="@width"/>
<xsl:with-param name="align" select=" 'left' "/>
</xsl:call-template>
<xsl:value-of select="$gutter"/>
</xsl:for-each>
<xsl:text>
</xsl:text>
<xsl:for-each select="$columns">
<xsl:call-template name="str:dup">
<xsl:with-param name="input" select=" '-' "/>
<xsl:with-param name="count" select="@width"/>
</xsl:call-template>
<xsl:call-template name="str:dup">
<xsl:with-param name="input" select=" '-' "/>
<xsl:with-param name="count" select="string-length($gutter)"/>
</xsl:call-template>
</xsl:for-each>
<xsl:text>
</xsl:text>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="/*/*">
<xsl:variable name="row" select="."/>
<xsl:for-each select="$columns">
<xsl:variable name="value">
<xsl:apply-templates select="$row/@*[local-name(.)=current()/@attr]" mode="text:map-col-value"/>
</xsl:variable>
<xsl:call-template name="text:justify" >
<xsl:with-param name="width" select="@width"/>
<xsl:with-param name="align" select="@align"/>
</xsl:call-template>
<xsl:value-of select="$gutter"/>
</xsl:for-each>
<xsl:text>
</xsl:text>
</xsl:template>
<xsl:template match="@*" mode="text:map-col-value">
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>Example: people-to-cols-using-generic.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">
<xsl:import href="generic-attr-to-columns.xslt"/>
<!--Definiert die Zuordnung von Attributen zu Spalten -->
<xsl:variable name="columns" select="document('')/*/text:column"/>
<text:column name="Name" width="20" align="left" attr="name"/>
<text:column name="Alter" width="8" align="right" attr="age"/>
<text:column name="Geschlecht" width="12" align="left" attr="sex"/>
<text:column name="Raucher" width="9" align="left" attr="smoker"/>
<!-- Verarbeitet eigene Attributzuordnungen -->
<xsl:template match="@sex" mode="text:map-col-value">
<xsl:choose>
<xsl:when test=".='m'">männlich</xsl:when>
<xsl:when test=".='w'">weiblich</xsl:when>
<xsl:otherwise>error</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>Example: Output (with separator parameter “ | ”).
|
|
|
|
|
|
|
|
----------------------------------------------------------------------- | |||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Copyright © 2006 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “XSLT Kochbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O'Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de