XSL-FO - how to continue numbers in two list-blocks? - list

Does somebody know how to achieve this with XSL-FO transformation? The question details should be clear from the codes below.
Input:
<section>
<title>Section 1</title>
<orderedlist>
<listitem><para>item 1.1</para></listitem>
<listitem>
<para>item 1.2</para>
<orderedlist>
<listitem><para>item a</para></listitem>
<listitem><para>item b</para></listitem>
</orderedlist>
</listitem>
</orderedlist>
</section>
<section>
<title>Section 2</title>
<orderedlist>
<listitem><para>item 2.1</para></listitem>
<listitem><para>item 2.2</para></listitem>
</orderedlist>
</section>
Desired output:
Section 1
1. item 1.1
2. item 1.2
a. item a
b. item b
Section 2
3. item 2.1
4. item 2.2
Here is the XSL file for lists:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<!-- templates for lists - supports numbered and itemized types -->
<!-- the maximum depth is currently 2 -->
<xsl:template match="orderedlist">
<fo:list-block start-indent="0.5cm" space-before="0.2cm"
provisional-distance-between-starts="0.7cm">
<xsl:apply-templates />
</fo:list-block>
</xsl:template>
<xsl:template match="orderedlist//orderedlist">
<fo:list-block start-indent="1.2cm" provisional-distance-between-starts="0.7cm"
padding-top="-0.2cm" padding-bottom="0.2cm">
<xsl:apply-templates />
</fo:list-block>
</xsl:template>
<xsl:template match="itemizedlist">
<fo:list-block start-indent="0.5cm" space-before="0.2cm"
provisional-distance-between-starts="0.7cm">
<xsl:apply-templates />
</fo:list-block>
</xsl:template>
<xsl:template match="itemizedlist//itemizedlist">
<fo:list-block start-indent="1.2cm" provisional-distance-between-starts="0.7cm"
padding-top="-0.2cm" padding-bottom="0.2cm">
<xsl:apply-templates />
</fo:list-block>
</xsl:template>
<xsl:template match="orderedlist/listitem">
<fo:list-item margin-top="0.1cm">
<fo:list-item-label end-indent="label-end()">
<fo:block>
<xsl:number count="listitem" format="1." />
</fo:block>
</fo:list-item-label>
<fo:list-item-body start-indent="body-start()">
<fo:block>
<xsl:apply-templates />
</fo:block>
</fo:list-item-body>
</fo:list-item>
</xsl:template>
<xsl:template match="orderedlist//orderedlist/listitem">
<fo:list-item>
<fo:list-item-label end-indent="label-end()">
<fo:block>
<xsl:number count="listitem" format="a." />
</fo:block>
</fo:list-item-label>
<fo:list-item-body start-indent="body-start()">
<fo:block>
<xsl:apply-templates />
</fo:block>
</fo:list-item-body>
</fo:list-item>
</xsl:template>
<xsl:template match="itemizedlist/listitem">
<fo:list-item margin-top="0.1cm">
<fo:list-item-label end-indent="label-end()">
<fo:block>•</fo:block>
</fo:list-item-label>
<fo:list-item-body start-indent="body-start()">
<fo:block>
<xsl:apply-templates />
</fo:block>
</fo:list-item-body>
</fo:list-item>
</xsl:template>
<xsl:template match="itemizedlist//itemizedlist/listitem">
<fo:list-item>
<fo:list-item-label end-indent="label-end()">
<fo:block>•</fo:block>
</fo:list-item-label>
<fo:list-item-body start-indent="body-start()">
<fo:block>
<xsl:apply-templates />
</fo:block>
</fo:list-item-body>
</fo:list-item>
</xsl:template>
</xsl:stylesheet>

Assuming you input sample is (added the sections topmost element to make the sample well-formed):
<sections>
<section>
<title>Section 1</title>
<orderedlist>
<listitem><para>item 1.1</para></listitem>
<listitem>
<para>item 1.2</para>
<orderedlist>
<listitem><para>item a</para></listitem>
<listitem><para>item b</para></listitem>
</orderedlist>
</listitem>
</orderedlist>
</section>
<section>
<title>Section 2</title>
<orderedlist>
<listitem><para>item 2.1</para></listitem>
<listitem><para>item 2.2</para></listitem>
</orderedlist>
</section>
</sections>
at a given listitem of first level you can use:
count(
preceding-sibling::listitem
|
../../preceding-sibling::section/orderedlist/listitem)
+ 1
For example:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output indent="yes"/>
<xsl:template match="sections">
<fo:block>
<xsl:apply-templates select="section"/>
</fo:block>
</xsl:template>
<xsl:template match="section">
<fo:list-block>
<xsl:apply-templates select="title | orderedlist/listitem"/>
</fo:list-block>
</xsl:template>
<xsl:template match="title">
<fo:list-item>
<xsl:value-of select="."/>
</fo:list-item>
</xsl:template>
<xsl:template match="listitem">
<fo:list-item>
<xsl:number
value="count(
preceding-sibling::listitem
|
../../preceding-sibling::section/orderedlist/listitem)
+ 1" format="1. "/>
</fo:list-item>
</xsl:template>
</xsl:stylesheet>
produces:
<fo:block xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:list-block>
<fo:list-item>Section 1</fo:list-item>
<fo:list-item>1. </fo:list-item>
<fo:list-item>2. </fo:list-item>
</fo:list-block>
<fo:list-block>
<fo:list-item>Section 2</fo:list-item>
<fo:list-item>3. </fo:list-item>
<fo:list-item>4. </fo:list-item>
</fo:list-block>
</fo:block>

Do you have a particular reason for not using DocBook and the DocBook-XSL stylesheets? That would give you a lot "for free" (perhaps you know that already).
In DocBook, what you ask for is already implemented. There is a continuation attribute on <orderedlist> that indicates whether the numbering in a list continues from the preceding list. This is supported in the DocBook-XSL styleheets for FO output (check out fo/lists.xsl and common/common.xsl to see how it's done).

Related

XSLT 1.0 - repeating table using child context doesn't work

I am using the XML below:
<root>
<Products name="product01">
<Instruments name="ins01"/>
<Instruments name="ins02"/>
<Instruments name="ins01"/>
</Products>
<Products name="product02">
<Instruments name="random text A">
<Bill name="A"/>
</Instruments>
<Instruments name="random text B and C">
<Bill name="B">
<Notes>some text</Notes>
</Bill>
<Bill name="C">
<Notes>some text</Notes>
</Bill>
</Instruments>
<Instruments name="random text A and B">
<Bill name="A">
<Notes>some text</Notes>
</Bill>
<Bill name="B">
<Notes>some text</Notes>
</Bill>
</Instruments>
<Instruments name="random text B">
<Bill name="B">
<Notes>some text</Notes>
</Bill>
</Instruments>
<Instruments name="random text C">
<Bill name="C">
<Notes>some text</Notes>
</Bill>
</Instruments>
<Instruments name="random text C and A">
<Bill name="C">
<Notes>some text</Notes>
</Bill>
<Bill name="A">
<Notes>some text</Notes>
</Bill>
</Instruments>
</Products>
</root>
First, I am grouping after Products, then grouping after Bills, on the left table-column, while the right table-column should only display the Instruments name, based on the Bill Type grouping from first column. As an example of above XML, I am trying to achieve this:
product02
Bill Type: A random text A
random text A and B
random text C and A
Bill Type: B random text B and C
random text A and B
random text B
Bill Type: C random text B and C
random text C
random text C and A
A sample of my template is below:
<xsl:key name="groupProducts" match="/root/Products/Instruments/Bill" use="../../#name" />
<xsl:key name="groupBilling" match="/root/Products/Instruments/Bill" use="concat(../../#name,#name)" />
<xsl:template name="myTemplate">
<fo:block>
<fo:table>
<fo:table-column column-width="proportional-column-width(1)" />
<fo:table-body>
<xsl:for-each select="/root/Products/Instruments/Bill[generate-id(.) = generate-id(key('groupProducts',../../#name)[1])]">
<fo:table-row>
<fo:table-cell>
<fo:block font-weight="bold" margin-top="4pt">
<xsl:value-of select="../../#name" />
</fo:block>
<fo:block>
<fo:table>
<fo:table-column column-width="50.000" column-number="1" />
<fo:table-column column-width="50.000" column-number="2" />
<fo:table-body>
<xsl:for-each select="/root/Products/Instruments/Bill[generate-id(.) = generate-id(key('groupBilling',concat(current()/../../#name,#name))[1])]">
<fo:table-row>
<fo:table-cell>
<fo:block color="blue">
Bill Type: <xsl:value-of select="#name" /></fo:block>
<fo:block />
</fo:table-cell>
<fo:table-cell>
<fo:block>
<fo:table>
<fo:table-column column-width="proportional-column-width(1)" />
<fo:table-body>
<xsl:for-each select="../.">
<fo:table-row>
<fo:table-cell>
<fo:block>
<xsl:value-of select="#name" />
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:for-each>
</fo:table-body>
</fo:table>
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:for-each>
</fo:table-body>
</fo:table>
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:for-each>
</fo:table-body>
</fo:table>
</fo:block>
</xsl:template>
For the for-each on the right table-column, I also tried the following:
<xsl:for-each select="../.">
../*[score/#naam = current()/score/#naam]
../node()[score/#naam = current()/score/#naam]
/root/Products/Instruments, but none works.
Any help is greatly appreciated.
While I cannot follow your attempted XSLT with fo namespace, consider this shortened XSLT running the Muenchian Method rendering an HTML version of desired output:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" omit-xml-declaration="no" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="bill_key" match="Bill" use="#name"/>
<xsl:template match="/root">
<html>
<xsl:apply-templates select="Products[descendant::Bill]"/>
</html>
</xsl:template>
<xsl:template match="Products">
<table>
<tr>
<td><xsl:value-of select="#name"/></td>
<td></td>
</tr>
<xsl:apply-templates select="Instruments"/>
<tr></tr>
</table>
</xsl:template>
<xsl:template match="Instruments">
<xsl:apply-templates select="Bill[generate-id() =
generate-id(key('bill_key', #name))]"/>
</xsl:template>
<xsl:template match="Bill">
<xsl:for-each select="key('bill_key', #name)">
<tr>
<xsl:if test="position() = 1">
<td><xsl:value-of select="concat('Bill Type: ', #name)"/></td>
</xsl:if>
<xsl:if test="position() > 1">
<td></td>
</xsl:if>
<td><xsl:value-of select="../#name"/></td>
</tr>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Hopefully you can adjust the <table>, <tr>, <td> tags to your fo styling.
Online Demo (click HTML to view output)
HTML Table Output
product02
Bill Type: A random text A
random text A and B
random text C and A
Bill Type: B random text B and C
random text A and B
random text B
Bill Type: C random text B and C
random text C
random text C and A

Grouping by column row values in xsl:fo table

I am trying to group values of individual column in xsl fo:table, some values not grouped as expected, it get grouped according to previous column's grouped value,
i need individual grouping in columns, check with my xsl and xml file, i am using these files to generate PDF file using apache FOP.
My XSL File
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
exclude-result-prefixes="#all"
version="3.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="data">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="simple"
page-height="8.5in" page-width="11in" margin-top=".5in"
margin-bottom=".5in" margin-left=".5in" margin-right=".5in">
<fo:region-body margin-top="2cm" margin-bottom="2cm" />
<fo:region-before extent="2cm" overflow="hidden" />
<fo:region-after extent="1cm" overflow="hidden" />
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="simple"
initial-page-number="1">
<fo:static-content flow-name="xsl-region-before">
<fo:block font-size="13.0pt" font-family="serif"
padding-after="2.0pt" space-before="4.0pt" text-align="center"
border-bottom-style="solid" border-bottom-width="1.0pt">
<xsl:text>PDF Test</xsl:text>
</fo:block>
</fo:static-content>
<fo:static-content flow-name="xsl-region-after">
<fo:block font-size="12.0pt" font-family="sans-serif"
padding-after="2.0pt" space-before="2.0pt" text-align="center"
border-top-style="solid" border-bottom-width="1.0pt">
<xsl:text>Page</xsl:text>
<fo:page-number />
</fo:block>
</fo:static-content>
<fo:flow flow-name="xsl-region-body">
<xsl:apply-templates select="data-body" />
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<xsl:template match="data-body">
<fo:block text-align="center">
<fo:table table-layout="fixed" width="100%"
border-style="dashed">
<fo:table-column border-style="solid" />
<fo:table-column border-style="solid" />
<fo:table-column border-style="solid" />
<fo:table-header>
<xsl:apply-templates select="table-header" />
</fo:table-header>
<fo:table-body>
<xsl:for-each-group select="table-data" group-adjacent="column-two">
<xsl:apply-templates select="current-group()">
<xsl:with-param name="row-span" select="count(current-group())" tunnel="yes"/>
</xsl:apply-templates>
</xsl:for-each-group>
</fo:table-body>
</fo:table>
</fo:block>
</xsl:template>
<xsl:template match="table-header">
<fo:table-row keep-together.within-page="always"
border-style="solid">
<fo:table-cell>
<fo:block font-size="10pt" font-family="sans-serif"
padding-top="3pt">
<xsl:value-of select="column-one"></xsl:value-of>
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block font-size="10pt" font-family="sans-serif"
padding-top="3pt">
<xsl:value-of select="column-two"></xsl:value-of>
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block font-size="10pt" font-family="sans-serif"
padding-top="3pt">
<xsl:value-of select="column-three"></xsl:value-of>
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:template>
<xsl:template match="table-data">
<fo:table-row keep-together.within-page="always"
border-style="solid">
<xsl:apply-templates>
<xsl:with-param name="row-group-index" tunnel="yes" select="position()"/>
</xsl:apply-templates>
</fo:table-row>
</xsl:template>
<xsl:template match="table-data/*">
<fo:table-cell>
<fo:block font-size="10pt" font-family="sans-serif"
padding-top="3pt">
<xsl:value-of select="."></xsl:value-of>
</fo:block>
</fo:table-cell>
</xsl:template>
<xsl:template match="table-data/column-two">
<xsl:param name="row-span" tunnel="yes"/>
<xsl:param name="row-group-index" tunnel="yes"/>
<xsl:choose>
<xsl:when test="$row-span = 1">
<xsl:next-match/>
</xsl:when>
<xsl:when test="$row-span > 1 and $row-group-index = 1">
<fo:table-cell number-rows-spanned="{$row-span}">
<fo:block font-size="10pt" font-family="sans-serif"
padding-top="3pt">
<xsl:value-of select="."></xsl:value-of>
</fo:block>
</fo:table-cell>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template match="table-data/column-three">
<xsl:param name="row-span" tunnel="yes"/>
<xsl:param name="row-group-index" tunnel="yes"/>
<xsl:choose>
<xsl:when test="$row-span = 1">
<xsl:next-match/>
</xsl:when>
<xsl:when test="$row-span > 1 and $row-group-index = 1">
<fo:table-cell number-rows-spanned="{$row-span}">
<fo:block font-size="10pt" font-family="sans-serif"
padding-top="3pt">
<xsl:value-of select="."></xsl:value-of>
</fo:block>
</fo:table-cell>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
My XML File
<?xml version="1.0" encoding="UTF-8"?>
<data>
<data-body>
<table-header>
<column-one>Column One</column-one>
<column-two>Column Two</column-two>
<column-three>Column Three</column-three>
</table-header>
<table-data>
<column-one>One</column-one>
<column-two>5000</column-two>
<column-three>Three</column-three>
</table-data>
<table-data>
<column-one>One</column-one>
<column-two>5000</column-two>
<column-three>Three</column-three>
</table-data>
<table-data>
<column-one>One</column-one>
<column-two>1200</column-two>
<column-three>Three</column-three>
</table-data>
<table-data>
<column-one>One</column-one>
<column-two>2000</column-two>
<column-three>Four</column-three>
</table-data>
<table-data>
<column-one>One</column-one>
<column-two>2000</column-two>
<column-three>Four</column-three>
</table-data>
<table-data>
<column-one>One</column-one>
<column-two>1234</column-two>
<column-three>Five</column-three>
</table-data>
<table-data>
<column-one>One</column-one>
<column-two>5666</column-two>
<column-three>Five</column-three>
</table-data>
<table-data>
<column-one>One</column-one>
<column-two>5666</column-two>
<column-three>Five</column-three>
</table-data>
</data-body>
</data>
I think one way to solve that is to use two passes, one which computes and adds the row-span to columns, and the second that transforms to XSL-FO.
In https://xsltfiddle.liberty-development.net/3NSSEuY/2 I have used XSLT 3 and xsl:iterate to implement the first step for a sequence of column names provided as an xs:string* parameter:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
exclude-result-prefixes="#all"
version="3.0">
<xsl:param name="columns-to-group" as="xs:string*" select="'column-two', 'column-three'"/>
<xsl:mode name="compute-row-spans" on-no-match="shallow-copy"/>
<xsl:mode name="compute-row-span" on-no-match="shallow-copy"/>
<xsl:template match="data-body" mode="compute-row-spans">
<xsl:iterate select="$columns-to-group">
<xsl:param name="table" select="."/>
<xsl:on-completion select="$table"/>
<xsl:next-iteration>
<xsl:with-param name="table">
<xsl:apply-templates select="$table" mode="compute-row-span">
<xsl:with-param name="column-to-group" tunnel="yes" select="."/>
</xsl:apply-templates>
</xsl:with-param>
</xsl:next-iteration>
</xsl:iterate>
</xsl:template>
<xsl:template match="data-body" mode="compute-row-span">
<xsl:param name="column-to-group" as="xs:string" tunnel="yes"/>
<xsl:copy>
<xsl:apply-templates select="#*"/>
<xsl:apply-templates select="table-header"/>
<xsl:for-each-group select="table-data" group-adjacent="*[name() = $column-to-group]">
<xsl:apply-templates select="current-group()" mode="compute-row-span">
<xsl:with-param name="column-to-group" tunnel="yes" select="$column-to-group"/>
<xsl:with-param name="row-span" select="count(current-group())" tunnel="yes"/>
</xsl:apply-templates>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
<xsl:template match="table-data" mode="compute-row-span">
<xsl:copy>
<xsl:apply-templates select="#*" mode="#current"/>
<xsl:apply-templates select="*" mode="#current">
<xsl:with-param name="current-row-index" tunnel="yes" select="position()"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="table-data/*" mode="compute-row-span">
<xsl:param name="column-to-group" tunnel="yes"/>
<xsl:param name="row-span" tunnel="yes"/>
<xsl:param name="current-row-index" tunnel="yes"/>
<xsl:choose>
<xsl:when test="name() = $column-to-group and $row-span > 1">
<xsl:if test="$current-row-index = 1">
<xsl:copy>
<xsl:apply-templates select="#*" mode="#current"/>
<xsl:attribute name="row-span" select="$row-span"/>
<xsl:apply-templates mode="#current"/>
</xsl:copy>
</xsl:if>
</xsl:when>
<xsl:otherwise>
<xsl:next-match/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="data">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="simple"
page-height="8.5in" page-width="11in" margin-top=".5in"
margin-bottom=".5in" margin-left=".5in" margin-right=".5in">
<fo:region-body margin-top="2cm" margin-bottom="2cm" />
<fo:region-before extent="2cm" overflow="hidden" />
<fo:region-after extent="1cm" overflow="hidden" />
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="simple"
initial-page-number="1">
<fo:static-content flow-name="xsl-region-before">
<fo:block font-size="13.0pt" font-family="serif"
padding-after="2.0pt" space-before="4.0pt" text-align="center"
border-bottom-style="solid" border-bottom-width="1.0pt">
<xsl:text>PDF Test</xsl:text>
</fo:block>
</fo:static-content>
<fo:static-content flow-name="xsl-region-after">
<fo:block font-size="12.0pt" font-family="sans-serif"
padding-after="2.0pt" space-before="2.0pt" text-align="center"
border-top-style="solid" border-bottom-width="1.0pt">
<xsl:text>Page</xsl:text>
<fo:page-number />
</fo:block>
</fo:static-content>
<fo:flow flow-name="xsl-region-body">
<xsl:apply-templates select="data-body" />
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<xsl:template match="data-body">
<fo:block text-align="center">
<fo:table table-layout="fixed" width="100%"
border-style="dashed">
<fo:table-column border-style="solid" />
<fo:table-column border-style="solid" />
<fo:table-column border-style="solid" />
<fo:table-header>
<xsl:apply-templates select="table-header" />
</fo:table-header>
<xsl:variable name="table-with-row-spans">
<xsl:apply-templates select="." mode="compute-row-spans"/>
</xsl:variable>
<fo:table-body>
<xsl:apply-templates select="$table-with-row-spans/data-body/table-data"/>
</fo:table-body>
</fo:table>
</fo:block>
</xsl:template>
<xsl:template match="table-header">
<fo:table-row keep-together.within-page="always"
border-style="solid">
<fo:table-cell>
<fo:block font-size="10pt" font-family="sans-serif"
padding-top="3pt">
<xsl:value-of select="column-one"></xsl:value-of>
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block font-size="10pt" font-family="sans-serif"
padding-top="3pt">
<xsl:value-of select="column-two"></xsl:value-of>
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block font-size="10pt" font-family="sans-serif"
padding-top="3pt">
<xsl:value-of select="column-three"></xsl:value-of>
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:template>
<xsl:template match="table-data">
<fo:table-row keep-together.within-page="always"
border-style="solid">
<xsl:apply-templates/>
</fo:table-row>
</xsl:template>
<xsl:template match="table-data/*">
<fo:table-cell>
<fo:block font-size="10pt" font-family="sans-serif"
padding-top="3pt">
<xsl:apply-templates/>
</fo:block>
</fo:table-cell>
</xsl:template>
<xsl:template match="table-data/*[#row-span]">
<fo:table-cell number-rows-spanned="{#row-span}">
<fo:block font-size="10pt" font-family="sans-serif"
padding-top="3pt">
<xsl:apply-templates/>
</fo:block>
</fo:table-cell>
</xsl:template>
</xsl:stylesheet>

Grid-table format in XSLT

I have the following XML:
<Pattern name="Form" Date="12/18/2015 3:25 AM CST">
Swap_Conversion
<CurrentLocale />
<Patterns>
<Pattern name="Section">
Swap_Conversion
<Patterns>
<Pattern name="GroupResult" status="AUTH" inputType="19">
Ultrasound Duration
<Patterns>
<Pattern name="GridDTA">23350691</Pattern>
<Pattern name="GridDTA">56468381</Pattern>
<Pattern name="GridDTA">20218422</Pattern>
<Pattern name="GridDTA">21058661</Pattern>
<Pattern name="GridDTA">4156900</Pattern>
<Pattern name="GridDTA">20008930</Pattern>
<Pattern name="GridDTA">21197198</Pattern>
</Patterns>
<Patterns>
<Pattern name="GroupResult" status="AUTH" inputType="">
Ear Irrigation Solution
<Patterns />
<Patterns>
<Pattern name="CodedResult" status="AUTH" display="Ace Bandage :" taskAssayCode="23350691">2 inch</Pattern>
</Patterns>
</Pattern>
</Patterns>
<Patterns>
<Pattern name="GroupResult" status="AUTH" inputType="">
Frame Order Priority
<Patterns />
<Patterns>
<Pattern name="CodedResult" status="AUTH" display="Ace Bandage :" taskAssayCode="23350691">3 inch</Pattern>
</Patterns>
</Pattern>
</Patterns>
</Pattern>
</Patterns>
</Pattern>
</Patterns>
</Pattern
Currently I have the transpose which look something like:
But I want it to look something like:
Currently I had tried with which worked for transpose:
<xsl:choose>
<xsl:when test="$inputType='19'"
<xsl:variable name="groupNodeSet" select="Patterns/Pattern[#name='GroupResult']"/>
<xsl:for-each select="$groupNodeSet[position() <= ((last() + $tablecolumns - 1) div $tablecolumns)]">
<!-- loopCount indicates which table of the multiple tables that a grid control may be split into that we are currently generating-->
<xsl:variable name="loopCount" select="position()"/>
<fo:table border="1pt solid black">
<fo:table-column/>
<fo:table-column/>
<fo:table-column/>
<fo:table-body>
<fo:table-row>
<fo:table-cell border-width="thin">
<fo:block/>
</fo:table-cell>
<fo:table-cell>
<xsl:if test="$groupNodeSet[position()=((($loopCount - 1) * $tablecolumns) + 1)]">
<xsl:attribute name="border-width">
<xsl:text>thin</xsl:text>
</xsl:attribute>
</xsl:if>
<fo:block font-size="{$regularfontsize}" font-family="sans-serif" text-align="left" font-style="italic">
<xsl:value-of select="$groupNodeSet[position()=((($loopCount - 1) * $tablecolumns) + 1)]/text()"/>
</fo:block>
</fo:table-cell>
<fo:table-cell>
<xsl:if test="$groupNodeSet[position()=((($loopCount - 1) * $tablecolumns) + 2)]">
<xsl:attribute name="border-width">
<xsl:text>thin</xsl:text>
</xsl:attribute>
</xsl:if>
<fo:block font-size="{$regularfontsize}" font-family="sans-serif" text-align="left" font-style="italic">
<xsl:value-of select="$groupNodeSet[position()=((($loopCount - 1) * $tablecolumns) + 2)]/text()"/>
</fo:block>
</fo:table-cell>
</fo:table-row>
<xsl:for-each select="../../Patterns/Pattern[#name='GridDTA' and ../..//#taskAssayCode=text()]">
<xsl:variable name="taskassay">
<xsl:value-of select="node()"/>
</xsl:variable>
<fo:table-row>
<fo:table-cell border-width="thin">
<fo:block>
<xsl:value-of select="../..//Pattern/#display[../..//Pattern/#taskAssayCode=$taskassay]"/>
</fo:block>
</fo:table-cell>
<xsl:call-template name="GenerateGridTableCells">
<xsl:with-param name="count" select="1"/>
<xsl:with-param name="maxcount" select="$tablecolumns"/>
<xsl:with-param name="taskassay" select="$taskassay"/>
<xsl:with-param name="groupNodeSet" select="$groupNodeSet"/>
<xsl:with-param name="rowNumber" select="$loopCount"/>
</xsl:call-template>
</fo:table-row>
</xsl:for-each>
</fo:table-body>
</fo:table>
</xsl:for-each>
</xsl:when>
</xsl:choose>
I have tried multiple approach for normal one (not the transpose) which isn't working for.
Can anyone help me in having the grid matrix without transpose for the above XML?
Since you're using XSLT 1.0, you're stuck with using Meunchian grouping (muenchian grouping, http://www.jenitennison.com/xslt/grouping/muenchian.html). Using XSLT 2.0 would have made it easier.
The version below can cope with a variations in the 'CodedResult' within each 'GroupResult'.
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:variable name="regularfontsize" select="'12pt'"/>
<xsl:output indent="yes" />
<xsl:key name="coded-results"
match="Pattern[#name = 'CodedResult'][#status = 'AUTH']"
use="#display" />
<xsl:template match="/">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:axf="http://www.antennahouse.com/names/XSL/Extensions">
<fo:layout-master-set>
<fo:simple-page-master master-name="a">
<fo:region-body/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="a">
<fo:flow flow-name="xsl-region-body">
<xsl:apply-templates select="Pattern/Patterns/Pattern[#name = 'Section']/Patterns/Pattern" />
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<xsl:variable name="inputType" select="'19'" />
<xsl:template match="Pattern[#inputType = '19']">
<xsl:variable name="all-coded-results"
select="Patterns/Pattern[#inputType = '']/Patterns/Pattern[#name = 'CodedResult']
[count(. | key('coded-results', #display)[1]) = 1]" />
<fo:table border="1pt solid black">
<fo:table-column/>
<xsl:for-each
select="$all-coded-results">
<fo:table-column/>
</xsl:for-each>
<fo:table-body>
<fo:table-row>
<fo:table-cell />
<xsl:for-each
select="$all-coded-results">
<xsl:sort />
<fo:table-cell>
<fo:block>
<xsl:value-of select="#display" />
</fo:block>
</fo:table-cell>
</xsl:for-each>
</fo:table-row>
<xsl:apply-templates select="Patterns/Pattern[#name = 'GroupResult']">
<xsl:with-param name="all-coded-results" select="$all-coded-results" />
</xsl:apply-templates>
</fo:table-body>
</fo:table>
</xsl:template>
<xsl:template match="Pattern[#name = 'GroupResult'][#inputType = '']">
<xsl:param name="all-coded-results" />
<fo:table-row>
<fo:table-cell>
<fo:block>
<xsl:value-of select="normalize-space(text())" />
</fo:block>
</fo:table-cell>
<xsl:variable name="this-pattern" select="." />
<xsl:for-each
select="$all-coded-results">
<xsl:sort />
<xsl:variable name="this-result" select="."/>
<fo:table-cell>
<xsl:if test="$this-pattern/Patterns/Pattern[#display = $this-result/#display]">
<fo:block>
<xsl:value-of select="$this-pattern/Patterns/Pattern[#display = $this-result/#display]" />
</fo:block>
</xsl:if>
</fo:table-cell>
</xsl:for-each>
</fo:table-row>
</xsl:template>
</xsl:stylesheet>

xsl:fo template match not firing on nested list

I need to use a hyphen for the bullet on a nested list applying xsl:fo. I have two templates to match but only one is being applied. If I use just the first template, the outer list gets the template applied. If I use just the second template, the nested list gets the template applied. The pattern I am attempting to match in the second template is any unordered list with a list item as a parent. Any help on getting my desired output is greatly appreciated.
XML
<ThisNode>
<ul>
<li>Item One</li>
<li>Item Two</li>
<li>Item Three
<ul>
<li>Sub-Item One</li>
<li>Sub-Item Two</li>
</ul>
</li>
<li>Item Four</li>
<li>Item Five</li>
</ul>
</ThisNode>
XSLT
<xsl:template match="ul">
<fo:list-block>
<xsl:for-each select="./li">
<fo:list-item>
<fo:list-item-label end-indent="label-end()">
<fo:block font-weight="bold">•</fo:block>
</fo:list-item-label>
<fo:list-item-body start-indent="8pt">
<fo:block><xsl:value-of select="."/></fo:block>
</fo:list-item-body>
</fo:list-item>
</xsl:for-each>
</fo:list-block>
</xsl:template>
<xsl:template match="li//ul">
<fo:list-block start-indent="8pt">
<xsl:for-each select="./li">
<fo:list-item>
<fo:list-item-label end-indent="label-end()">
<fo:block font-weight="bold">-</fo:block>
</fo:list-item-label>
<fo:list-item-body start-indent="16pt">
<fo:block><xsl:value-of select="."/></fo:block>
</fo:list-item-body>
</fo:list-item>
</xsl:for-each>
</fo:list-block>
</xsl:template>
<fo:block text-align="left">
<xsl:apply-templates select="./ThisNode"/>
</fo:block>
DESIRED OUTPUT
• Item One
• Item Two
• Item Three
- Sub-Item One
- Sub-Item Two
• Item Four
• Item Five
ACTUAL OUTPUT USING EITHER JUST THE 1ST TEMPLATE OR USING BOTH
• Item One
• Item Two
• Item ThreeSub-Item OneSub-Item Two
• Item Four
• Item Five
ACTUAL OUTPUT USING ONLY THE 2ND TEMPLATE
Item One Item Two Item Three
- Sub-Item One
- Sub-Item Two
Item Four Item Five
This looks like a good place for a call-template with a mode, to distinguish between the 2 cases. The outer template calls the inner template:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/">
<fo:block text-align="left">
<xsl:apply-templates select="ThisNode"/>
</fo:block>
</xsl:template>
<xsl:template match="ul">
<fo:list-block>
<xsl:for-each select="li">
<fo:list-item>
<fo:list-item-label end-indent="label-end()">
<fo:block font-weight="bold">•</fo:block>
</fo:list-item-label>
<fo:list-item-body start-indent="8pt">
<fo:block>
<!-- I'm not quite sure what you want here -->
<xsl:value-of select="text()"/>
<xsl:apply-templates select="ul" mode="inner"/>
</fo:block>
</fo:list-item-body>
</fo:list-item>
</xsl:for-each>
</fo:list-block>
</xsl:template>
<xsl:template match="ul" mode="inner">
<fo:list-block start-indent="8pt">
<xsl:for-each select="./li">
<fo:list-item>
<fo:list-item-label end-indent="label-end()">
<fo:block font-weight="bold">-</fo:block>
</fo:list-item-label>
<fo:list-item-body start-indent="16pt">
<fo:block>
<xsl:value-of select="."/>
</fo:block>
</fo:list-item-body>
</fo:list-item>
</xsl:for-each>
</fo:list-block>
</xsl:template>
</xsl:stylesheet>
The following stylesheet illustrates a possible solution. Your approach is not far off - however, it fails to account for the behaviour of XSLT processors. This expression:
<xsl:value-of select="."/>
by default returns any text nodes that are children of the context node. But in your case (template match for li elements), all descendant text nodes are output, not only the immediate children of li.
Therefore, the stylesheet below uses
<xsl:value-of select="child::text()"/>
to retrieve the text content of a li element instead and <xsl:apply-templates> to process any li elements that are sub-items of it.
As your title states,
xsl:fo template match not firing on nested list
You diagnosed it correctly. This is because - once inside a template match for li - you do not let the XSLT processor process descendant li elements.
Stylesheet
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/ThisNode">
<fo:root>
<fo:layout-master-set>
<fo:simple-page-master master-name="simple"
page-height="29.7cm"
page-width="21cm"
margin-top="1cm"
margin-bottom="2cm"
margin-left="2.5cm"
margin-right="2.5cm">
<fo:region-body margin-top="3cm"/>
<fo:region-before extent="3cm"/>
<fo:region-after extent="1.5cm"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="simple">
<fo:flow flow-name="xsl-region-body">
<xsl:apply-templates/>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
<xsl:template match="ul[parent::ThisNode]">
<fo:list-block>
<xsl:apply-templates/>
</fo:list-block>
</xsl:template>
<xsl:template match="ul[parent::li]">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="li">
<fo:list-item>
<xsl:choose>
<xsl:when test="parent::ul/parent::ThisNode">
<fo:list-item-label start-indent="1cm">
<fo:block font-weight="bold">•</fo:block>
</fo:list-item-label>
<fo:list-item-body start-indent="1.5cm">
<fo:block><xsl:value-of select="child::text()"/></fo:block>
</fo:list-item-body>
</xsl:when>
<xsl:otherwise>
<fo:list-item-label start-indent="3cm">
<fo:block font-weight="bold">-</fo:block>
</fo:list-item-label>
<fo:list-item-body start-indent="3.5cm">
<fo:block><xsl:value-of select="."/></fo:block>
</fo:list-item-body>
</xsl:otherwise>
</xsl:choose>
</fo:list-item>
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
Output (XSL-FO)
<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="simple" page-height="29.7cm" page-width="21cm" margin-top="1cm"
margin-bottom="2cm"
margin-left="2.5cm"
margin-right="2.5cm">
<fo:region-body margin-top="3cm"/>
<fo:region-before extent="3cm"/>
<fo:region-after extent="1.5cm"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="simple">
<fo:flow flow-name="xsl-region-body">
<fo:list-block>
<fo:list-item>
<fo:list-item-label start-indent="1cm">
<fo:block font-weight="bold">•</fo:block>
</fo:list-item-label>
<fo:list-item-body start-indent="1.5cm">
<fo:block>Item One</fo:block>
</fo:list-item-body>
</fo:list-item>Item One
<fo:list-item>
<fo:list-item-label start-indent="1cm">
<fo:block font-weight="bold">•</fo:block>
</fo:list-item-label>
<fo:list-item-body start-indent="1.5cm">
<fo:block>Item Two</fo:block>
</fo:list-item-body>
</fo:list-item>Item Two
<fo:list-item>
<fo:list-item-label start-indent="1cm">
<fo:block font-weight="bold">•</fo:block>
</fo:list-item-label>
<fo:list-item-body start-indent="1.5cm">
<fo:block>Item Three
</fo:block>
</fo:list-item-body>
</fo:list-item>Item Three
<fo:list-item>
<fo:list-item-label start-indent="3cm">
<fo:block font-weight="bold">-</fo:block>
</fo:list-item-label>
<fo:list-item-body start-indent="3.5cm">
<fo:block>Sub-Item One</fo:block>
</fo:list-item-body>
</fo:list-item>Sub-Item One
<fo:list-item>
<fo:list-item-label start-indent="3cm">
<fo:block font-weight="bold">-</fo:block>
</fo:list-item-label>
<fo:list-item-body start-indent="3.5cm">
<fo:block>Sub-Item Two</fo:block>
</fo:list-item-body>
</fo:list-item>Sub-Item Two
<fo:list-item>
<fo:list-item-label start-indent="1cm">
<fo:block font-weight="bold">•</fo:block>
</fo:list-item-label>
<fo:list-item-body start-indent="1.5cm">
<fo:block>Item Four</fo:block>
</fo:list-item-body>
</fo:list-item>Item Four
<fo:list-item>
<fo:list-item-label start-indent="1cm">
<fo:block font-weight="bold">•</fo:block>
</fo:list-item-label>
<fo:list-item-body start-indent="1.5cm">
<fo:block>Item Five</fo:block>
</fo:list-item-body>
</fo:list-item>Item Five
</fo:list-block>
</fo:flow>
</fo:page-sequence>
</fo:root>
Output (PDF)

using for-each-group technique in xsl-fo

can I use for-each-group if yes then can someone show me an example. I am trying to generate a pdf using xsl-fo
I am trying to output it using a table. Please show me an example which makes use of grouping technique and adding values. However, the output should be displayed in a table.
xml file:
<?xml version="1.0"?>
<Library>
<Book code="123">
<BookName>XML</BookName>
<Category>Programming</Category>
<Quantity>10</Quantity>
<Price>100</Price>
</Book>
<Book code="345">
<BookName>Photoshop</BookName>
<Category>Design</Category>
<Quantity>50</Quantity>
<Price>200</Price>
</Book>
<Book code="123">
<BookName>XML</BookName>
<Category>Programming</Category>
<Quantity>5</Quantity>
<Price>100</Price>
</Book>
<Book code="345">
<BookName>Photoshop</BookName>
<Category>Design</Category>
<Quantity>10</Quantity>
<Price>200</Price>
</Book>
<Book code="456">
<BookName>Illustrator</BookName>
<Category>Design</Category>
<Quantity>100</Quantity>
<Price>300</Price>
</Book>
</Library>
myxsl-fo
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" version="1.0">
<xsl:output encoding="UTF-8" indent="yes" method="xml" standalone="no" omit-xml-declaration="no"/>
<xsl:template match="Library">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="A4-landscape" page-height="300mm" page-width="150mm" margin="1in">
<fo:region-body margin="1in"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="A4-landscape">
<fo:flow flow-name="xsl-region-body">
<fo:table border-top-style="solid" border-top-width="thick">
<fo:table-body font-size="12pt" font-family="times new roman">
<fo:table-row border-bottom-style="solid" border-bottom-color="#000" border-bottom-width="thick">
<fo:table-cell padding-top="1mm" padding-bottom="1mm">
<fo:block font-weight="bold">Category</fo:block>
</fo:table-cell>
<fo:table-cell padding-top="1mm" padding-bottom="1mm">
<fo:block font-weight="bold">Book Code</fo:block>
</fo:table-cell>
<fo:table-cell padding-top="1mm" padding-bottom="1mm">
<fo:block font-weight="bold">Quantity</fo:block>
</fo:table-cell>
<fo:table-cell padding-top="1mm" padding-bottom="1mm">
<fo:block font-weight="bold">Price</fo:block>
</fo:table-cell>
<fo:table-cell padding-top="1mm" padding-bottom="1mm">
<fo:block font-weight="bold">Total</fo:block>
</fo:table-cell>
</fo:table-row>
<xsl:for-each select="Book">
<xsl:sort select="Category"/>
<xsl:sort select="#code"/>
<fo:table-row>
<fo:table-cell padding-top="3mm" padding-bottom="3mm">
<fo:block font-weight="bold">
<xsl:value-of select="Category"/>
</fo:block>
</fo:table-cell>
<fo:table-cell padding-top="3mm" padding-bottom="3mm">
<fo:block font-weight="bold">
<xsl:value-of select="#code"/>
</fo:block>
</fo:table-cell>
<fo:table-cell padding-top="3mm" padding-bottom="3mm">
<fo:block font-weight="bold">
<xsl:value-of select="Quantity"/>
</fo:block>
</fo:table-cell>
<fo:table-cell padding-top="3mm" padding-bottom="3mm">
<fo:block font-weight="bold">
<xsl:value-of select="Price"/>
</fo:block>
</fo:table-cell>
<fo:table-cell padding-top="3mm" padding-bottom="3mm">
<fo:block font-weight="bold">
<xsl:value-of select="number(Quantity)*number(Price)"/>
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:for-each>
</fo:table-body>
</fo:table>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
</xsl:stylesheet>
Instead of xsl:for-each if I try to use xsl:for-each-group then it throws an error saying that the xsl:for-each-group cannot be in that location.
my current output:
however the output I want is shown in the image below:
thanks
As mentioned in the comments, if you are using XSLT 1.0 then the xsl:for-each-group command is not available. In XSLT 1.0, grouping is usually done using a technique called Muenchian Grouping. It is worth reading it, and understanding it, as it is a very useful technique in XSLT 1.0 once you understand it.
In Muenchian Grouping, you start off by defining a key which will be used to look up the elements in your group. In your case, you are grouping by Category and Code together, and so they key would look like this:
<xsl:key name="GroupByCategoryCode" match="Book" use="concat(Category, '|', #code)"/>
Now, in XSLT 2.0, you would probably write this...
<xsl:for-each-group select="Library/Book" group-by="concat(Category, '|', #code)">
However, in XSLT 1.0 you have to write this (I've added lots of indentation to improve readability)
<xsl:for-each
select="Library/Book
[
generate-id() =
generate-id
(
key('GroupByCategoryCode', concat(Category, '|', #code))[1]
)
]">
(Using xsl:apply-templates here would also work).
What this is going is looking at all Book elements, and checking its combination of "Category" and "Code" to see whether it is the first occurrence of that element in the key. Effectively it will pick the distinct occurences of "Category" and "Code".
In XSLT 2.0, you would use "current-group" to then iterate over all elements in the group. In XSLT 1.0, you would just use the key
<xsl:for-each select="key('GroupByCategoryCode', concat(Category, '|', #code))">
Although, in this particular case, you don't need to do a for-each, as you are just summing up Quantity in each group.
<xsl:value-of
select="sum(key('GroupByCategoryCode', concat(Category, '|', #code))/Quantity)"/>
Or, to improve readability abit...
<xsl:variable name="currentGroup" select="key('GroupByCategoryCode', concat(Category, '|', #code))"/>
<xsl:value-of select="sum($currentGroup/Quantity)"/>
Try this XSLT. To keep things simple (and because I don't know xsl-fo) I am outputting HTML to demonstrate the principle. All you need to do is to replace the HTML tags with their xsl-fo equivalents
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="GroupByCategoryCode" match="Book" use="concat(Category, '|', #code)"/>
<xsl:template match="/">
<html>
<body>
<h1>Books Information</h1>
<table border="1">
<tr>
<th>Category</th>
<th>Book Code</th>
<th>Quantity</th>
<th>Unit Price</th>
<th>Price</th>
</tr>
<xsl:apply-templates select="Library/Book[generate-id() = generate-id(key('GroupByCategoryCode', concat(Category, '|', #code))[1])]">
<xsl:sort select="Category"/>
<xsl:sort select="#code"/>
</xsl:apply-templates>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="Book">
<xsl:variable name="currentGroup" select="key('GroupByCategoryCode', concat(Category, '|', #code))"/>
<tr>
<td>
<xsl:value-of select="Category"/>
</td>
<td>
<xsl:value-of select="#code"/>
</td>
<td>
<xsl:value-of select="sum($currentGroup/Quantity)"/>
</td>
<td>
<xsl:value-of select="Price"/>
</td>
<td>
<xsl:value-of select="sum($currentGroup/Quantity) * Price"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
EDIT: To show the word 'Repeated' instead of the Category for repeated categories, you could view this as grouping by category, and only show the category name for the first one in the group.
So, add the following key to the XSLT, to allow you to look up books by category
<xsl:key name="GroupByCategory" match="Book" use="Category"/>
Then, you need to pick the distinct categories (this should surround the existing xsl:apply-templates)
<xsl:for-each select="Library/Book[generate-id() = generate-id(key('GroupByCategory', Category)[1])]">
Then within the template matching Book you could then determine whether to show the category name or "defined" like so
<xsl:choose>
<xsl:when test="position() = 1">
<xsl:value-of select="Category" />
</xsl:when>
<xsl:otherwise>
<xsl:text>Repeated</xsl:text>
</xsl:otherwise>
</xsl:choose>
Try this XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="GroupByCategoryCode" match="Book" use="concat(Category, '|', #code)"/>
<xsl:key name="GroupByCategory" match="Book" use="Category"/>
<xsl:template match="/">
<html>
<body>
<h1>Books Information</h1>
<table border="1">
<tr>
<th>Category</th>
<th>Book Code</th>
<th>Quantity</th>
<th>Unit Price</th>
<th>Price</th>
</tr>
<xsl:for-each select="Library/Book[generate-id() = generate-id(key('GroupByCategory', Category)[1])]">
<xsl:sort select="Category"/>
<xsl:apply-templates select="key('GroupByCategory', Category)[generate-id() = generate-id(key('GroupByCategoryCode', concat(Category, '|', #code))[1])]">
<xsl:sort select="#code"/>
</xsl:apply-templates>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="Book">
<xsl:variable name="Category">
<xsl:choose>
<xsl:when test="position() = 1">
<xsl:value-of select="Category" />
</xsl:when>
<xsl:otherwise>
<xsl:text>Repeated</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="currentGroup" select="key('GroupByCategoryCode', concat(Category, '|', #code))"/>
<tr>
<td>
<xsl:value-of select="$Category"/>
</td>
<td>
<xsl:value-of select="#code"/>
</td>
<td>
<xsl:value-of select="sum($currentGroup/Quantity)"/>
</td>
<td>
<xsl:value-of select="Price"/>
</td>
<td>
<xsl:value-of select="sum($currentGroup/Quantity) * Price"/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>