Using Leaders and Markers - Exercise

Contact Us or call 1-877-932-8228
Using Leaders and Markers - Exercise

Using Leaders and Markers

Duration: 15 to 20 minutes.

In this exercise, you will practice creating a nicely formatted table of contents for the song lyrics document we have been working on. You will also use markers so that each page in the document has the appropriate song title in the header. The output is shown in LeadersMarkers/Exercises/Lyrics.pdf.

  1. Open LeadersMarkers/Exercises/Lyrics.xsl for editing.
  2. Add code to transform Lyrics.xml to create a FO that will render a PDF like LeadersMarkers/Exercises/Lyrics.pdf.
    • The table of contents should be nicely formatted.
    • Each page in the main document (e.g, after the table of contents), should show the name of the current story in the header.
  3. To test your solution, transform LeadersMarkers/Exercises/Lyrics.xml against LeadersMarkers/Exercises/Lyrics.xsl.

Solution:

LeadersMarkers/Solutions/Lyrics.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="/">
		<fo:root>
---- C O D E   O M I T T E D ----
<fo:page-sequence master-reference="TOC" format="i">
---- C O D E   O M I T T E D ----
<fo:flow flow-name="xsl-region-body">
					<fo:block font-family="Helvetica" font-size="22pt" 
						font-weight="bold" text-align="center" space-after=".2in" id="TOC">
						Table of Contents
					</fo:block>
					<xsl:for-each select="Songs/Song">
						<fo:block font-family="Helvetica" font-size="12pt" font-weight="bold" space-after=".1in" text-align-last="justify">
							<fo:basic-link internal-destination="{generate-id(Title)}">
								<xsl:value-of select="Title"/>
								<fo:leader leader-pattern="dots"/>
								<fo:page-number-citation ref-id="{generate-id(Title)}"/>
							</fo:basic-link>
						</fo:block>
					</xsl:for-each>
					<fo:block id="LastPageToc" break-after="odd-page"/>
				</fo:flow>
				
			</fo:page-sequence>
			
			
			<fo:page-sequence master-reference="Song" initial-page-number="1">
				
				<fo:static-content flow-name="top-right-and-left">
					<fo:block font-family="Helvetica" font-size="10pt" text-align="outside" 
						padding-top=".05in" border-top-width=".05in" border-top-color="#0000ff" 
						border-top-style="solid" font-weight="bold">
						<fo:retrieve-marker retrieve-class-name="mrkSongTitle" retrieve-position="first-including-carryover"/>
					</fo:block>
				</fo:static-content>
---- C O D E   O M I T T E D ----
<fo:flow flow-name="xsl-region-body">
				
					<xsl:for-each select="Songs/Song">
						<fo:block font-weight="bold" font-size="14pt" font-family="Times" 
							id="{generate-id(Title)}" break-before="page">
							<fo:marker marker-class-name="mrkSongTitle">
							<xsl:number format="1. "/>
							<xsl:value-of select="Title"/>
							</fo:marker>
							<xsl:number format="1. "/>
							<xsl:value-of select="Title"/>
						</fo:block>
						<fo:block font-size="12pt" font-style="italic" font-family="Times" 
							margin-bottom=".1in" margin-left=".2in">
								- <xsl:value-of select="@Artist"/>
						</fo:block>
						<fo:block linefeed-treatment="preserve" white-space-collapse="false" wrap-option="wrap" white-space-treatment="preserve" font-size="10pt">
							<xsl:value-of select="Lyrics"/>
						</fo:block>
					</xsl:for-each>
					
					<fo:block id="LastPage"/>
					
				</fo:flow>
			</fo:page-sequence>
				
			<!--END PAGE SEQUENCES-->	
			
		</fo:root>
	</xsl:template>
	
</xsl:stylesheet>
Next