Creating a List - Exercise

Contact Us or call 1-877-932-8228
Creating a List - Exercise

Creating a List

Duration: 10 to 15 minutes.

In this exercise, you will practice transforming an XML document into a list for output as PDF.

  1. Open BlocksAndInlines/Exercises/Beatles.xml and examine the code.
  2. Open BlocksAndInlines/Exercises/Beatles.xsl for editing.
  3. Notice that the list block has already been started:
    <fo:list-block start-indent="20pt">
    	<xsl:apply-templates/>
    </fo:list-block>
  4. At the bottom of the document is a template matching beatle.
  5. Fill in this template so that a list item is created for each Beatle.
  6. To test your solution, transform BlocksAndInlines/Exercises/Beatles.xml against BlocksAndInlines/Exercises/Beatles.xsl.

Code Sample:

BlocksAndInlines/Exercises/Beatles.xsl
<?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:output method="xml" indent="yes"/>
	<xsl:template match="/beatles">
		<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
			<fo:layout-master-set>
				<fo:simple-page-master master-name="list" page-height="11in" 
					page-width="8.5in" margin-top=".5in" margin-bottom=".5in" 
					margin-left=".5in" margin-right=".5in">
					<fo:region-body margin-top="1in" margin-bottom="1in"/>
					<fo:region-before extent=".5in"/>
					<fo:region-after extent=".5in"/>
				</fo:simple-page-master>
			</fo:layout-master-set>
			<fo:page-sequence master-reference="list">
				<fo:static-content flow-name="xsl-region-before" text-align="center">
					<fo:block font-size="20pt" font-weight="bold">
						The Beatles
					</fo:block>
				</fo:static-content>
				<fo:flow flow-name="xsl-region-body">
					<fo:block space-after="24pt" font-weight="bold">
						The Beatles
					</fo:block>
					<fo:list-block start-indent="20pt">
						<xsl:apply-templates/>
					</fo:list-block>
				</fo:flow>
			</fo:page-sequence>
		</fo:root>
	</xsl:template>
	<xsl:template match="beatle">
	<!--
		Fill in this template so that a list item is created for each Beatle
	-->
	</xsl:template>
</xsl:stylesheet>

Solution:

BlocksAndInlines/Solutions/Beatles.xsl
<?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:output method="xml" indent="yes"/>
---- C O D E   O M I T T E D ----
<xsl:template match="beatle">
		<fo:list-item>
			<fo:list-item-label end-indent="label-end()">
				<fo:block font-weight="bold">
					<fo:character character="&#x2022;"/>
				</fo:block>
			</fo:list-item-label>
			<fo:list-item-body start-indent="body-start()">
				<fo:block>
					<xsl:value-of select="name/firstname"/>
						<xsl:text> </xsl:text>
					<xsl:value-of select="name/lastname"/>
				</fo:block>
			</fo:list-item-body>
		</fo:list-item>
	</xsl:template>
</xsl:stylesheet>
Next