Convert column-oriented data to row-oriented HTML - xslt

My source XML provides data column-by-column; the cells have varying heights. Using XSLT 1.0, I need to pivot this around to match HTML's row-by-row table format.
Example: I need to convert input data like this
<source>
<column>
<cell height="1">col A row 1</cell>
<cell height="2">col A rows 2-3</cell>
</column>
<column>
<cell height="1">col B row 1</cell>
<cell height="1">col B row 2</cell>
<cell height="1">col B row 3</cell>
</column>
<column>
<cell height="3">col C rows 1-3</cell>
</column>
</source>
into an HTML table like this
<table>
<tr>
<td rowspan="1">col A row 1</td>
<td rowspan="1">col B row 1</td>
<td rowspan="3">col C rows 1-3</td>
</tr>
<tr>
<td rowspan="2">col A rows 2-3</td>
<td rowspan="1">col B row 2</td>
</tr>
<tr>
<td rowspan="1">col B row 3</td>
</tr>
</table>
How?
Edit: Here is another example, one where no single column has as many cells as there are table rows.
<source>
<column id="A">
<cell height="1">col A row 1</cell>
<cell height="2">col A rows 2-3</cell>
</column>
<column id="B">
<cell height="2">col B row 1-2</cell>
<cell height="1">col B row 3</cell>
</column>
<column id="C">
<cell height="3">col C rows 1-3</cell>
</column>
</source>
into an HTML table like this
<table>
<tr>
<td rowspan="1">col A row 1</td>
<td rowspan="2">col B row 1-2</td>
<td rowspan="3">col C rows 1-3</td>
</tr>
<tr>
<td rowspan="2">col A rows 2-3</td>
</tr>
<tr>
<td rowspan="1">col B row 3</td>
</tr>
</table>

I. XSLT 1.0 solution
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="vrtfPass1">
<xsl:apply-templates mode="pass1"/>
</xsl:variable>
<xsl:variable name="vPass1"
select="ext:node-set($vrtfPass1)"/>
<xsl:variable name="vMaxRow">
<xsl:for-each select=
"$vPass1/*/*/cell/#startRow">
<xsl:sort select="." data-type="number" order="descending"/>
<xsl:if test="position() = 1">
<xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:template match="node()|#*" mode="pass1">
<xsl:copy>
<xsl:apply-templates select="node()|#*" mode="pass1"/>
</xsl:copy>
</xsl:template>
<xsl:template match="cell" mode="pass1">
<cell height="{#height}"
startRow="{sum(preceding-sibling::*/#height) +1}">
<xsl:copy-of select="text()"/>
</cell>
</xsl:template>
<xsl:template match="/">
<table>
<xsl:call-template name="makeRows">
<xsl:with-param name="pmaxRow" select="$vMaxRow"/>
</xsl:call-template>
</table>
</xsl:template>
<xsl:template name="makeRows">
<xsl:param name="prowNum" select="1"/>
<xsl:param name="pmaxRow" select="1"/>
<xsl:if test="not($prowNum > $pmaxRow)">
<tr>
<xsl:apply-templates select=
"$vPass1/*/*/cell[#startRow = $prowNum]"/>
</tr>
<xsl:call-template name="makeRows">
<xsl:with-param name="prowNum" select="$prowNum+1"/>
<xsl:with-param name="pmaxRow" select="$pmaxRow"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template match="cell">
<td rowspan="{#height}"><xsl:value-of select="."/></td>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document:
<source>
<column>
<cell height="1">col A row 1</cell>
<cell height="2">col A rows 2-3</cell>
</column>
<column>
<cell height="2">col B row 1-2</cell>
<cell height="1">col B row 3</cell>
</column>
<column>
<cell height="3">col C rows 1-3</cell>
</column>
</source>
produces the wanted, correct result:
<table>
<tr>
<td rowspan="1">col A row 1</td>
<td rowspan="2">col B row 1-2</td>
<td rowspan="3">col C rows 1-3</td>
</tr>
<tr>
<td rowspan="2">col A rows 2-3</td>
</tr>
<tr>
<td rowspan="1">col B row 3</td>
</tr>
</table>
II. XSLT 2.0 solution:
This is similar to the XSLT 1.0 solution, but we use the max() function and also avoid the recursion by usig the to operator.
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="vPass1">
<xsl:apply-templates mode="pass1"/>
</xsl:variable>
<xsl:variable name="vMaxRow" as="xs:integer"
select="max($vPass1/*/*/cell/#startRow/xs:integer(.))"/>
<xsl:template match="node()|#*" mode="pass1">
<xsl:copy>
<xsl:apply-templates select="node()|#*" mode="pass1"/>
</xsl:copy>
</xsl:template>
<xsl:template match="cell" mode="pass1">
<cell height="{#height}"
startRow="{sum(preceding-sibling::*/#height) +1}">
<xsl:copy-of select="text()"/>
</cell>
</xsl:template>
<xsl:template match="/">
<table>
<xsl:for-each select="1 to $vMaxRow">
<tr>
<xsl:apply-templates select=
"$vPass1/*/*/cell[#startRow = current()]"/>
</tr>
</xsl:for-each>
</table>
</xsl:template>
<xsl:template match="cell">
<td rowspan="{#height}"><xsl:value-of select="."/></td>
</xsl:template>
</xsl:stylesheet>

Related

xsl:key how to select unmatched key values

I am trying to compare two xml using xsl:key, but not sure how to print unmatched keys. In this scenario I am keying b.xml and comparing with a.xml, but it does not print unmatched keys from b.xml.
a.xml
<root>
<metas>
<meta>
<name>x</name>
<value>0</value>
</meta>
<meta>
<name>y</name>
<value>1</value>
</meta>
<meta>
<name>z</name>
<value>1</value>
</meta>
</metas>
b.xml
<root>
<metas>
<info>
<name>a</name>
<value>0</value>
</info>
<info>
<name>y</name>
<value>1</value>
</info>
<info>
<name>z</name>
<value>1</value>
</info>
</metas>
Desired output:
Table
a.xml b.xml
name missing in a.xml a
value missing in a.xml 0
name x missing in b.xml
value 0 missing in b.xml
name y y
value 1 1
name z z
value 1 1
My xsl;
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:param name="uri" as="xs:string" select="'b.xml'"/>
<xsl:param name="b" as="document-node()" select="doc($uri)"/>
<xsl:key name="bCompare" match="root/metas/info" use="name"/>
<xsl:template match="/">
<html>
<head>
<title>Test</title>
</head>
<body>
<table>
<tr>
<td></td>
<th>a.xml</th>
<th>b.xml</th>
</tr>
<xsl:apply-templates/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="metas">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="meta">
<xsl:variable name="compare" select="key('bCompare', name, $b)"/>
<tr>
<th>Name</th>
<td>
<xsl:value-of select="name"/>
</td>
<td>
<xsl:value-of select="$compare/name"/>
<xsl:if test="empty($compare/name)">missing in b.xml</xsl:if>
</td>
</tr>
<tr>
<th>Value</th>
<td>
<xsl:value-of select="value"/>
</td>
<td>
<xsl:value-of select="$compare/value"/>
<xsl:if test="empty($compare/value)">missing in b.xml</xsl:if>
</td>
</tr>
</xsl:template>
You need to define a key for the other direction as well, i.e. <xsl:key name="aCompare" match="root/metas/meta" use="name"/>, then you need to make sure you process those elements of the second document with e.g. <xsl:apply-templates/><xsl:variable name="main-doc" select="/"/><xsl:apply-templates select="$b//info/meta[not(name = key('bCompare', $main-doc//meta/name, $main-doc))]"/> and then you need a template
<xsl:template match="info">
<xsl:variable name="compare" select="key('aCompare', name, $main-doc)"/>
<tr>
<th>Name</th>
<td>
<xsl:value-of select="$compare/name"/>
<xsl:if test="empty($compare/name)">missing in a.xml</xsl:if>
</td>
<td>
<xsl:value-of select="name"/>
</td>
</tr>
<tr>
<th>Value</th>
<td>
<xsl:value-of select="$compare/value"/>
<xsl:if test="empty($compare/value)">missing in a.xml</xsl:if>
</td>
<td>
<xsl:value-of select="value"/>
</td>
</tr>
</xsl:template>

Split a tag inside foreach

I have a xml below
<Report>
<rl>
<id>12345;12346</id>
<activity>a2/a3</activity>
<result>r2/r3</result>
<operator>test</operator>
<timestamp>12/18/2014 3:51:19 PM</timestamp>
<quantity>2</quantity>
</rl>
<rl>
<id>22345;22346</id>
<activity>a3/a4</activity>
<result>r3/r4</result>
<operator>test</operator>
<timestamp>12/18/2014 3:51:19 PM</timestamp>
<quantity>2</quantity>
</rl>
</Report>
and for my xsl,
<table border="1" style="border-width: 1px" width="90%" bordercolor="#C0C0C0" align="center">
<tr>
<th width="5%" align="center">
<font color="#000000" face="Verdana" size="3">Index</font>
</th>
<th width="15%" align="center">
<font color="#000000" face="Verdana" size="3">ID A:</font>
</th>
<th width="15%" align="center">
<font color="#000000" face="Verdana" size="3">ID B:</font>
</th>
</tr>
<xsl:for-each select="Report/rl">
<tr height="25">
<td width="5%" align="center" >
<font color="#000000" face="Verdana" size="2">
<xsl:value-of select="position()" />
</font>
</td>
<td align="center">
<font color="#000000" face="Verdana" size="2">
<xsl:value-of select="idA" />
</font>
</td>
<td align="center">
<font color="#000000" face="Verdana" size="2">
<xsl:value-of select="idB" />
</font>
</td>
</tr>
</xsl:for-each>
</table>
For 1st rl, there is 12345:12346 for tag, I want to split them into 12345 and 12346 and show them in the 'idA' and 'idB'. How should I do that?
My xslt version is 1.0.
Assuming there are always exactly two values, separated by a semicolon, use:
<xsl:value-of select="substring-before(id, ';')"/>
to populate the idA cell, and:
<xsl:value-of select="substring-after(id, ';')"/>
to populate the idB cell.
Added:
For the same example as posted, can you elaborate more about the
'recursive named template'?
The solution using a recursive named template would look something like this:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/Report">
<table border="1">
<tr>
<th>Index</th>
<th>ID A</th>
<th>ID B</th>
</tr>
<xsl:apply-templates select="rl"/>
</table>
</xsl:template>
<xsl:template match="rl">
<tr>
<td>
<xsl:value-of select="position()" />
</td>
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="id"/>
</xsl:call-template>
</tr>
</xsl:template>
<xsl:template name="tokenize">
<xsl:param name="text"/>
<xsl:param name="delimiter" select="';'"/>
<td>
<xsl:value-of select="substring-before(concat($text, $delimiter), $delimiter)"/>
</td>
<xsl:if test="contains($text, $delimiter)">
<!-- recursive call -->
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Note that for the header we assume that the number of columns is known beforehand. Otherwise you'd have to use a similar recursive template to generate the header cells too.
Check this example
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="*">
<xsl:for-each select="rl">
<node>
<idA>
<xsl:value-of select="substring-before(id,';')"/>
</idA>
<idB>
<xsl:value-of select="substring-after(id,';')"/>
</idB>
</node>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

XSLT union of attributes

How can I transform this XML:
<data>
<entry a="2" b="3" />
<entry a="2" c="3" />
<entry b="2" c="3" />
<entry a="1" b="2" c="3" />
</data>
Into a table containing a union of all the attributes of the entrys in the header, with either the values or blanks in the rows:
<table>
<tr><th>a</th><th>b</th><th>c</th></tr>
<tr><td>2</td><td>3</td><td> </td></tr>
<tr><td>2</td><td> </td><td>3</td></tr>
<tr><td> </td><td>2</td><td>3</td></tr>
<tr><td>1</td><td>2</td><td>3</td></tr>
</table>
<xsl:key name="attrs" match="data/entry/#*" use="local-name(.)"/>
<xsl:variable name="unique-attr-names" select="data/entry/#*[generate-id() = generate-id(key('attrs', local-name(.))[1])]" />
<xsl:template match="data">
<table>
<tr><xsl:call-template name="unique-attrs" /></tr>
<xsl:apply-templates />
</table>
</xsl:template>
<xsl:template name="unique-attrs">
<xsl:for-each select="$unique-attr-names">
<xsl:sort data-type="text" order="ascending" />
<th><xsl:value-of select="local-name(.)"/></th>
</xsl:for-each>
</xsl:template>
<xsl:template match="entry">
<xsl:variable name="e" select="." />
<tr>
<xsl:for-each select="$unique-attr-names">
<xsl:sort data-type="text" order="ascending" />
<xsl:variable name="a" select="." />
<th><xsl:value-of select="($e/#*[local-name() = local-name($a)])[1]" /></th>
</xsl:for-each>
</tr>
</xsl:template>
You may also try this (based on muenchian grouping):
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" />
<xsl:key name="kEntryAttr" match="entry/#*" use="name()" />
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="entry">
<xsl:variable name="e" select="." />
<tr>
<xsl:for-each select="//entry/#*[count(. | key('kEntryAttr', name() )[1] ) = 1]" >
<td>
<xsl:value-of select="$e/#*[name() = name(current() )]"/>
</td>
</xsl:for-each>
</tr>
</xsl:template>
<xsl:template match="*">
<table>
<tr>
<xsl:for-each select="//entry/#*[count(. | key('kEntryAttr', name() )[1] ) = 1]" >
<th>
<xsl:value-of select="name()"/>
</th>
</xsl:for-each>
</tr>
<xsl:apply-templates select="entry" />
</table>
</xsl:template>
</xsl:stylesheet>
Which will generate the following output.
<?xml version="1.0"?>
<table>
<tr>
<th>a</th>
<th>b</th>
<th>c</th>
</tr>
<tr>
<td>2</td>
<td>3</td>
<td/>
</tr>
<tr>
<td>2</td>
<td/>
<td>3</td>
</tr>
<tr>
<td/>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
</table>

xslt to create dynamic table with customizable headers

I would like to transform an XML document using xslt with a set of row nodes and column nodes into an xhtml table.
The column nodes define data about the associated row attribute. For example, the first column node specifies that the ID attribute of the Row node should be hidden (i.e. not show up in the table). The Caption element of the Column node defines what the column header text should be.
I've seen solutions where you know the attributes that you want to turn into columns ahead of time, but I'm not sure how to use the relevant column data to format the headers
Input:
<TableData>
<Columns>
<Column Name="ID" Hidden="true" />
<Column Name="Name" Caption="Item Name" />
<Column Name="Desc" Caption="Item Description" />
</Columns>
<Rows>
<Row ID="0" Name="A" />
<Row ID="1" Name="B" Desc="Some description"/>
<Row ID="3" Name="C" />
</Rows>
</TableData>
Desired output would be a table in (x)html something like this:
Item Name | Item Description
--------------------------------------
A |
B | Some Description
C |
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:key name="RowAttribsByName" match="Row/#*"
use="concat(generate-id(..), '|', name())"/>
<xsl:variable name="vColNames" select=
"/*/Columns/*[not(#Hidden = 'true')]/#Name"/>
<xsl:template match="/*">
<table border="1">
<tr>
<xsl:apply-templates select="Columns/*"/>
</tr>
<xsl:apply-templates select="Rows/Row"/>
</table>
</xsl:template>
<xsl:template match="Column[not(#Hidden = 'true')]">
<td><xsl:value-of select="#Caption"/></td>
</xsl:template>
<xsl:template match="Row">
<tr>
<xsl:apply-templates select="$vColNames">
<xsl:with-param name="pRowId"
select="generate-id()"/>
</xsl:apply-templates>
</tr>
</xsl:template>
<xsl:template match="Column/#*">
<xsl:param name="pRowId"/>
<td width="50%">
<xsl:value-of select=
"key('RowAttribsByName',
concat($pRowId, '|', .)
)
"/>
</td>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document:
<TableData>
<Columns>
<Column Name="ID" Hidden="true" />
<Column Name="Name" Caption="Item Name" />
<Column Name="Desc" Caption="Item Description" />
</Columns>
<Rows>
<Row ID="0" Name="A" />
<Row ID="1" Name="B" Desc="Some description"/>
<Row ID="3" Name="C" />
</Rows>
</TableData>
produces the wanted, correct result:
<table border="1">
<tr>
<td>Item Name</td>
<td>Item Description</td>
</tr>
<tr>
<td width="50%">A</td>
<td width="50%"/>
</tr>
<tr>
<td width="50%">B</td>
<td width="50%">Some description</td>
</tr>
<tr>
<td width="50%">C</td>
<td width="50%"/>
</tr>
</table>

XML to HTML table with XSLT with dynamic headings

My question is a bit similar to XML to HTML table with XSLT .
I have a dictionary defined as follows in XML:
<dictionary>
<languages>
<language>en</language>
<language>ja</language>
</languages>
<entries>
<entry id="1">
<en>Test</en>
<ja>テスト</ja>
</entry>
<entry id="2">
<en>Test2</en>
<ja>テスト2</en>
</entry>
</entries>
</dictionary>
And I would like the following output in XHTML:
<table>
<thead>
<tr>
<th>en</th>
<th>ja</th>
</tr>
</thead>
<tbody>
<tr>
<td>Test</td>
<td>テスト</td>
</tr>
<tr>
<td>Test2</td>
<td>テスト2</td>
</tr>
</tbody>
</table>
I adapted the answer from XML to HTML table with XSLT as follows:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="//dictionary/entries">
<table><xsl:apply-templates select="entry"/></table>
</xsl:template>
<xsl:template match="entry[1]">
<thead><tr><xsl:apply-templates select="*" mode="header"/></tr></thead>
<xsl:call-template name="standardRow"/>
</xsl:template>
<xsl:template match="entry" name="standardRow">
<tbody><tr><xsl:apply-templates select="*"/></tr></tbody>
</xsl:template>
<xsl:template match="entry/*">
<td><xsl:apply-templates select="node()"/></td>
</xsl:template>
<xsl:template match="entry/*" mode="header">
<th><xsl:value-of select="name()"/></th>
</xsl:template>
</xsl:stylesheet>
The thing is that I might have inputs as follows:
<dictionary>
<languages>
<language>en</language>
<language>ja</language>
<language>id</language>
</languages>
<entries>
<entry id="1">
<en>Test</en>
<ja>テスト</ja>
</entry>
<entry id="2">
<ja>テスト2</ja>
<en>Test2</en>
<id>uji2</id>
</entry>
</entries>
</dictionary>
As you might have understood, XSLT takes the first entry node to define the column names and the column id is not generated. Moreover, if the language order is changed in entry the <td> do not appear in order.
With the input above, I would like the following output:
<table>
<thead>
<tr>
<th>en</th>
<th>ja</th>
<th>id</th>
</tr>
</thead>
<tbody>
<tr>
<td>Test</td>
<td>テスト</td>
<td></td>
</tr>
<tr>
<td>Test2</td>
<td>テスト2</td>
<td>Uji2</td>
</tr>
</tbody>
</table>
This is my first time using XSLT and I do not really know how I could do this. I guess I could use the languages node. Please note that the XML input format is flexible and I would welcome any suggestions even if I need to change the format.
Here is a sample stylesheet:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html" indent="yes"/>
<xsl:key name="k1" match="entry/*" use="concat(generate-id(..), '|', local-name())"/>
<xsl:variable name="languages" select="/dictionary/languages/language"/>
<xsl:template match="dictionary">
<xsl:apply-templates select="entries"/>
</xsl:template>
<xsl:template match="entries">
<table>
<thead>
<tr>
<xsl:apply-templates select="$languages" mode="header"/>
</tr>
</thead>
<tbody>
<xsl:apply-templates/>
</tbody>
</table>
</xsl:template>
<xsl:template match="language" mode="header">
<th>
<xsl:value-of select="."/>
</th>
</xsl:template>
<xsl:template match="entry">
<tr>
<xsl:apply-templates select="$languages">
<xsl:with-param name="entry" select="current()"/>
</xsl:apply-templates>
</tr>
</xsl:template>
<xsl:template match="language">
<xsl:param name="entry"/>
<td>
<xsl:value-of select="key('k1', concat(generate-id($entry), '|', .))"/>
</td>
</xsl:template>
</xsl:stylesheet>