Creating Internal Links - Exercise

Contact Us or call 1-877-932-8228
Creating Internal Links - Exercise

Creating Internal Links

Duration: 15 to 20 minutes.

In this exercise, you will practice creating internal links by creating a PDF that looks like LinksAndGraphics/Solutions/Lyrics.pdf.

  1. Open and examine LinksAndGraphics/Exercises/Lyrics.xml. This document contains 50 classic rock songs. The DTD for Lyrics.xml is shown at the end of these instructions. The basic structure is as follows:
    • Root Element: Songs
      • Attributes: Source, Image
      • Elements: Song (repeatable)
        • Attributes: Artist
        • Elements: Title (contains text), Lyrics (contains text)
  2. Open LinksAndGraphics/Exercises/Lyrics.xsl for editing.
  3. Most of the document, including all the page masters, is created for you. You will do the following:
    • In the body of the page sequence that references the "TitlePage" master, add the ClassicRock.gif image from the Images folder.
    • In the body of the page sequences that reference the master pages called "Song1", "Song2", etc, list the song titles, artists and lyrics so that each song is displayed as shown below:The trick to getting the lyrics to break at line breaks is to set the linefeed-treatment attribute of the containing block to "preserve".
    • In the body of the page sequence that references the "TOC" master, list all the song titles from Lyrics.xml and make them links to the song lyrics, which you added in the previous step.
  4. To test your solution, transform LinksAndGraphics/Exercises/Lyrics.xml against LinksAndGraphics/Exercises/Lyrics.xsl.

Code Sample:

LinksAndGraphics/Exercises/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 ----
<!--BEGIN PAGE SEQUENCES-->
			<fo:page-sequence master-reference="TitlePage" 
				force-page-count="end-on-even">
				<fo:flow flow-name="xsl-region-body">
					<fo:block text-align="center" font-size="18pt" id="TitlePage">
						<fo:block font-weight="bold" margin-bottom="1cm">
								Top <xsl:value-of select="count(//Song)"/> 
								Classic Rock Songs of All Time
						</fo:block>
						<!--Add the ClassicRock.gif image from the Images folder-->
					</fo:block>
				</fo:flow>
			</fo:page-sequence>
			
			<fo:page-sequence master-reference="TOC">
---- 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>
					<!--
						List all the song titles from Lyrics.xml.  
						Make them Links to the song lyrics.
					-->
					<fo:block id="LastPageToc" break-after="odd-page"/>
				</fo:flow>
				
			</fo:page-sequence>
			
			<fo:page-sequence master-reference="Song" initial-page-number="1">
---- C O D E   O M I T T E D ----
<fo:flow flow-name="xsl-region-body">
					<!--
						List the song titles, artists and lyrics in the following format:

						Song TItle
							- Artist

						Lyrics
					-->
					<fo:block id="LastPage"/>
				</fo:flow>
			</fo:page-sequence>
			<!--END PAGE SEQUENCES-->
			
		</fo:root>
	</xsl:template>
	
</xsl:stylesheet>

Solution:

LinksAndGraphics/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 ----
<!--BEGIN PAGE SEQUENCES-->
			<fo:page-sequence master-reference="TitlePage" 
				force-page-count="end-on-even">
				<fo:flow flow-name="xsl-region-body">
					<fo:block text-align="center" font-size="18pt" id="TitlePage">
						<fo:block font-weight="bold" margin-bottom="1cm">
								Top <xsl:value-of select="count(//Song)"/>
								Classic Rock Songs of All Time
						</fo:block>
						<fo:block text-align="center">
							<fo:external-graphic src="url('{/Songs/@Image}')"/>
						</fo:block>
					</fo:block>
				</fo:flow>
			</fo:page-sequence>
---- 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">
							<fo:basic-link internal-destination="{generate-id(Title)}">
								<xsl:value-of select="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">
---- 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">
							<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