Real World XML (2nd Edition)

Creating Block-level Content: fo:block

You use blocks in XSL just as we did in CSSto create a rectangular display area set off from other display areas in a document. You use the fo:block formatting object for formatting such items as paragraphs, titles, headlines, figure and table captions, and so on. Here's an example from the beginning of the chapter:

<fo:block font-family="sans-serif" line-height="48pt" font-size="36pt"> Welcome to XSL formatting. </fo:block>

You can use these properties with fo:block :

  • Common accessibility properties: source-document , role

  • Common aural properties: azimuth , cue-after , cue-before , elevation , pause-after , pause-before , pitch , pitch-range , play-during , richness , speak , speak-header , speak-numeral , speak-punctuation , speech-rate , stress , voice-family , volume

  • Common border, padding, and background properties: background-attachment , background- color , background-image , background-repeat , background-position-horizontal , background-position-vertical , border-before-color , border-before-style , border-before-width , border-after-color , border-after-style , border-after-width , border-start-color , border-start-style , border-start-width , border-end-color , border-end-style , border-end-width , border-top-color , border-top-style , border-top-width , border-bottom-color , border-bottom-style , border-bottom-width , border-left-color , border-left-style , border-left-width , border-right-color , border-right-style , border-right-width , padding-before , padding-after , padding-start , padding-end , padding-top , padding-bottom , padding-left , padding-right

  • Common font properties: font-family , font-size , font-stretch , font-size-adjust , font-style , font-variant , font-weight

  • Common hyphenation properties: country , language , script , hyphenate , hyphenation-character , hyphenation-push-character-count , hyphenation- remain -character-count

  • Common margin properties for blocks: margin-top , margin-bottom , margin-left , margin-right , space-before , space-after , start-indent , end-indent

  • break-after

  • break-before

  • color

  • font-height-override-after

  • font-height-override-before

  • hyphenation-keep

  • hyphenation-ladder-count

  • id

  • keep-together

  • keep-with- next

  • keep-with-previous

  • last-line-end-indent

  • linefeed -treatment

  • line-height

  • line-height-shift-adjustment

  • line-stacking-strategy

  • orphans

  • relative-position

  • space-treatment

  • span

  • text-align

  • text-align-last

  • text-indent

  • visibility

  • white-space -collapse

  • widows

  • wrap-option

  • z-index

Note that the data in ch14_01.xml is broken up into various child elements of a <PLANET> element, such as < NAME > , <MASS> , and so on, like this:

<PLANET COLOR="RED"> <NAME>Mercury</NAME> <MASS UNITS="(Earth = 1)">.0553</MASS> <DAY UNITS="days">58.65</DAY> <RADIUS UNITS="miles">1516</RADIUS> <DENSITY UNITS="(Earth = 1)">.983</DENSITY> <DISTANCE UNITS="million miles">43.4</DISTANCE><!--At perihelion--> </PLANET>

In this example, I'll give the data in each of the children of a <PLANET> element its own block in the formatted document. To do that, I add a rule to ch14_02.xsl for each of those children, specifying the font to use for each block:

<?xml version='1.0'?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" version='1.0'> <xsl:template match="PLANETS"> <fo:root> <fo:layout-master-set> <fo:simple-page-master master-name="page" page-height="400mm" page-width="300mm" margin-top="10mm" margin-bottom="10mm" margin-left="20mm" margin-right="20mm"> <fo:region-body margin-top="0mm" margin-bottom="10mm" margin-left="0mm" margin-right="0mm"/> <fo:region-after extent="10mm"/> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference="page"> <fo:flow flow-name="xsl-region-body"> <xsl:apply-templates/> </fo:flow> </fo:page-sequence> </fo:root> </xsl:template> <xsl:template match="PLANET/NAME"> <fo:block font-weight="bold" font-size="36pt" line-height="48pt" font-family="sans-serif"> Name: <xsl:apply-templates/> </fo:block> </xsl:template> <xsl:template match="PLANET/MASS"> <fo:block font-size="36pt" line-height="48pt" font-family="sans-serif"> Mass (Earth = 1): <xsl:apply-templates/> </fo:block> </xsl:template> <xsl:template match="PLANET/DAY"> <fo:block font-size="36pt" line-height="48pt" font-family="sans-serif"> Day (Earth = 1): <xsl:apply-templates/> </fo:block> </xsl:template> <xsl:template match="PLANET/RADIUS"> <fo:block font-size="36pt" line-height="48pt" font-family="sans-serif"> Radius (in miles): <xsl:apply-templates/> </fo:block> </xsl:template> <xsl:template match="PLANET/DENSITY"> <fo:block font-size="36pt" line-height="48pt" font-family="sans-serif"> Density (Earth = 1): <xsl:apply-templates/> </fo:block> </xsl:template> <xsl:template match="PLANET/DISTANCE"> <fo:block font-size="36pt" line-height="48pt" font-family="sans-serif"> Distance (million miles): <xsl:apply-templates/> </fo:block> </xsl:template> </xsl:stylesheet>

Now we've handled all the elements in ch14_01.xml, so that completes ch14_02.xsl. You can see the results in Figure 14-1. Congratulations, you've completed your first transformation to XSL formatting objects!

Категории