Inside Xslt

Inline-Level Formatting Objects

In addition to the block objects in XSL-FO, you can also create inline objects. An inline object represents part of a larger formatting region, such as a block; for example, it might represent a word or two in a block. Inline objects are usually used to format part of the text as that text follows the normal flow in the page. For example, you can make the first character in a paragraph larger, make the whole first line blue, insert page numbers , add images, and so on.

One reason you might create inline objects is to format parts of a blocks text; for example, in Chapter 11, you saw the following use of the text-decoration property of <fo:inline> to underline text:

<xsl:template match="PLANET/MASS"> <fo:block font-size="24pt" line-height="32pt" font-family="Times"> <fo:inline text-decoration="underline"> Mass </fo:inline>: <xsl:apply-templates/> [Earth = 1] </fo:block> </xsl:template>

The following list includes the inline formatting elements:

Ill take a look at a few of the more common of these inline elements now, starting with <fo:inline> itself.

Creating Inline Regions: <fo:inline>

As you already saw in Chapter 11, you can use the <fo:inline> element to format a part of your text with a background, underline it, or enclose it with a border. This element lets you format an inline area, such as a few words in a block of text, almost as though it were a block itself.

You can use the following properties with <fo:inline> :

For example, youve already seen how to add an underline to a single word inside other text using the text-decoration property:

<xsl:template match="PLANET/MASS"> <fo:block font-size="24pt" line-height="32pt" font-family="Times"> <fo:inline text-decoration="underline"> Mass </fo:inline>: <xsl:apply-templates/> [Earth = 1] </fo:block> </xsl:template>

Youll see more on how to use <fo:inline> when working with footnotes later in this chapter.

Handling Characters Individually: <fo:character>

As you can guess from its name , the <fo:character> object enables you to handle the characters in a document individually. One place to use <fo:character> is when you want to replace certain characters with other characters. In the following example, I match an element named <PASSWORD> and replace the characters in it with the character *:

<xsl:template match="PASSWORD"> <fo:character character="*"> <xsl:value-of select="."/> </fo:character> </xsl:template>

You can use the <fo:character> element to format individual characters, as in this case, which formats the characters in Hello using different colors:

<fo:character character="H" font-size="24pt" color="red"/> <fo:character character="E" font-size="24pt" color="yellow"/> <fo:character character="L" font-size="24pt" color="green/> <fo:character character="L" font-size="24pt" color="blue"/> <fo:character character="O" font-size="24pt" color="orange"/>

You can use the following properties with <fo:character> :

Creating Page Numbers: <fo:page-number>

Another useful inline formatting object is <fo:page-number> . This element creates an inline area displaying the current page number. Heres an example:

<fo:block> You are now reading page <fo:page-number/>. </fo:block>

You can use the following properties with <fo:page-number> :

Youll see an example using page numbers during the discussion of page sequences at the end of this chapter.

Inserting Graphics: <fo:external-graphic>

A popular element in XSL-FO formatting is <fo:external-graphic> , which you use to embed an image in a document.

You can use the following properties with <fo:external-graphic> :

As in HTML, you can set the size of the image in the document; in XSL-FO, you use the content-height , content-width , and scaling properties, and if you dont set these properties, the image is displayed in its original size. The following example, graphics.fo, displays an image, xslfo.jpg, and a caption:

Listing 12.4 graphics.fo

<?xml version="1.0" encoding="UTF-8"?> <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> <fo:layout-master-set> <fo:simple-page-master margin-right="20mm margin-left="20mm" margin-bottom="10mm" margin-top="10mm" page-width="300mm page-height="400mm" master-name="page"> <fo:region-body margin-right="0mm" margin-left="0mm" margin-bottom="10mm" margin-top="0mm"/> <fo:region-after extent="10mm"/> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-name="page"> <fo:flow flow-name="xsl-region-body"> <fo:block space-after="12pt" font-weight="bold" font-size="36pt" text-align="center"> Using Graphics </fo:block> <fo:block text-align="center"> <fo:external-graphic src="file:xslfo.jpg"/> </fo:block> <fo:block space-before="10pt" text-align="center" font-size="24pt"> An image embedded in a document. </fo:block> </fo:flow> </fo:page-sequence> </fo:root>

You can see the PDF document created from graphics.fo in Figure 12.3.

Figure 12.3. Displaying an image using formatting objects.

Actually inserting an image into the result document is easy if your software supports it. The fop processor now supports images (it didnt just a few versions ago), so you can use the <fo:external-graphic> element to insert the image this way:

<fo:block text-align="center"> <fo:external-graphic src="file:xslfo.jpg"/> </fo:block>

Formatting First Lines: <fo:initial-property-set>

You can use the <fo:initial-property-set> element to specify properties for and format the first line of a block. In the following example, Im formatting the first line of a block in small caps using the font-variant property (which, incidentally, fop doesnt support):

<fo:block> <fo:initial-property-set font-variant="small-caps"/> This text will be displayed in the result document. The first line will be displayed using small caps. </fo:block>

You can use the following properties with <fo:initial-property-set> :

That completes this overview of the inline formatting objects; the next sections look at an out-of-line formatting object: footnotes.

Категории