Creating the Page Layout with XSLT - Exercise

Contact Us or call 1-877-932-8228
Creating the Page Layout with XSLT - Exercise

Creating the Page Layout with XSLT

Duration: 20 to 25 minutes.

In this exercise, you will create an XSLT (virtually from scratch) that transforms PageLayout/Exercises/Stories.xml into a FO page identical to PageLayout/Demos/OddEvenBreak.fo.

  1. Open and examine PageLayout/Exercises/Stories.xml.
  2. Open PageLayout/Exercises/Stories.xsl for editing.
  3. Add code to transform Stories.xml to create a FO that will render a PDF like PageLayout/Demos/OddEvenBreak.pdf.

Solution:

PageLayout/Solutions/Stories.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 xmlns:fo="http://www.w3.org/1999/XSL/Format">
		<fo:layout-master-set>
		
		<fo:simple-page-master master-name="StoriesOdd" 
			page-height="11in" page-width="8.5in" margin=".5in">
			<fo:region-body margin=".5in" />
			<fo:region-before region-name="oddHead" extent=".5in" />
			<fo:region-after region-name="oddFoot" extent=".5in" />
		</fo:simple-page-master>
		
		<fo:simple-page-master master-name="StoriesEven" 
			page-height="11in" page-width="8.5in" margin=".5in">
			<fo:region-body margin=".5in" />
			<fo:region-before region-name="evenHead" extent=".5in" />
			<fo:region-after region-name="evenFoot" extent=".5in" />
		</fo:simple-page-master>
		
		<fo:page-sequence-master master-name="Stories">
			<fo:repeatable-page-master-alternatives>
				<fo:conditional-page-master-reference 
					master-reference="StoriesOdd" odd-or-even="odd" />
				<fo:conditional-page-master-reference 
					master-reference="StoriesEven" odd-or-even="even" />
			</fo:repeatable-page-master-alternatives>
		</fo:page-sequence-master>
			
		</fo:layout-master-set>
		
		<fo:page-sequence master-reference="Stories">
			<fo:static-content flow-name="oddHead">
				<fo:block border-bottom-width="thin" 
					border-bottom-style="solid" border-bottom-color="green"
					font-weight="bold" text-align="outside">
					Story Title
				</fo:block>
			</fo:static-content>
			<fo:static-content flow-name="evenHead">
				<fo:block border-bottom-width="thin" 
					border-bottom-style="solid" border-bottom-color="green"
					font-weight="bold" text-align="outside">
					Book of Stories
				</fo:block>
			</fo:static-content>
			<fo:static-content flow-name="oddFoot">
				<fo:block border-bottom-width="thin" 
					border-bottom-style="solid" border-bottom-color="green"
					font-weight="bold" text-align="outside">
					<fo:page-number/> |
				</fo:block>
			</fo:static-content>
			<fo:static-content flow-name="evenFoot">
				<fo:block border-bottom-width="thin" 
					border-bottom-style="solid" border-bottom-color="green"
					font-weight="bold" text-align="outside">
					| <fo:page-number/>
				</fo:block>
			</fo:static-content>
			<fo:flow flow-name="xsl-region-body">
				<xsl:apply-templates/>
			</fo:flow>
		</fo:page-sequence>
	</fo:root>
</xsl:template>
<xsl:template match="body/div/h2">
	<fo:block font-weight="bold" font-size="larger" break-before="odd-page">
		<xsl:value-of select="."/>
	</fo:block>
</xsl:template>
<xsl:template match="body/div/p">
	<fo:block margin-top="12pt">
		<xsl:apply-templates/>
	</fo:block>
</xsl:template>
<xsl:template match="head | h1 | a"/>
</xsl:stylesheet>
Next