This is my home, welcome to my blog. There is no limit to the goodness of life. The more you experience and appreciate the goodness of life.

11/19/2010

What is XSL-FO? How is it related to XSLT? Give examples to help explain your answer

XSL is the Extensible Stylesheet Language, a W3C recommended standard. And it was designed to be a language for transforming and formatting XML documents, just as CSS is a style sheet language of HTML. XSL contains three parts, XSLT, XPath and XSL-FO. The full name of the XSL-FO is Extensible Stylesheet Language Formatting Objects. Because the “T” in “XSLT” stands for the transformation part, XSL-FO means the formatting part, and yet people often referred to as XSL. So XSLT is a normative part of XSL-FO, and language features of XSL-FO are similar to XSLT and Xpath.

XSL-FO files are XML documents with output information, and XSL-FO files are stored in documents with a .fo or a .fob file extension. XSL-FO files have a structure like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">

<fo:layout-master-set>
  <fo:simple-page-master master-name="test">
<!-- Page template goes here -->
 <fo: region-body/>
  </fo:simple-page-master>
</fo:layout-master-set>

<fo:page-sequence master-reference="test">
  <!--  A sequence of pages, page content goes here -->
<fo: flow>
  <fo: block></fo: block>
</fo: flow>
</fo:page-sequence>
</fo:root>

Since a formatting object document is an XML document, the first line of all XSL-FO documents must be the XML declaration. Each XSL-FO file has only one, root element, typically called <fo: root>.

Now we have an outline of the structure of a typical XSL-FO file, in order to clearly understand the relationship between XSL-FO and XSLT, we make use of XSLT to transform an XML file to include XSL-FO elements for display.
This is XML document:

<? Xml version=’1.0’?>
<BookList>
  <title>BOOK INFORMATION</title>
   <Book BookID=”001”>
        <Name>HTTP Developer’s Handbook</Name>
<Author>Chris Shiflett </Author>
<ISBN>0-672-32454-7</ISBN>
</Book>
<Book BookID="002">
                  <Name> Digital Image Processing</Name>
                  <Author>Rafael C. Gonzalez </Author>
                  <ISBN>978-7-121-10207-3</ISBN>
</Book>
</BookList>

The XSLT stylesheet declares two namespaces in its xsl:stylesheet start-tag: one to indentify the XSLT instructions to the XSLT processor and one to indentify the XSL elements to the rendering program. The first thing output is the <fo: root> element by XSLT template.
The XSL document corresponding to this XML file shows as follows:

<?xml version=”1.0”?>
<xsl: styleheet version= ”1.0” xmlns:xsl =”http://www.w3.org/1999/XSL/Transform” xmlns:fo =” http://www.w3.org/1999/XSL/Format” >
<xsl: template match=”/”>
  <fo: root>

   <fo: layout-master-set>
      <fo: simple-page-master master-name=”test”
page-height = “20 cm”
  page-width = “12 cm”
margin-top = “1 cm”
margin-bottom= “3 cm”
margin-left =”3 cm”
margin-right =”3 cm”>
<fo: region-body margin-top= “3 cm”/>
<fo: region-before extent= “3 cm”/>
<fo: region-after extent= “1.5 cm”/>
</fo: simple-page-master>
</fo: layout-master-set>

<fo: page-sequence master-name=”test”>
<fo: static-content flow-name=”xsl-region-before”>
     <fo: block text-align=”end”
        font-size=”10pt” font-family=”serif” line-height=”13pt” >
      <xsl: value-of select=”/BookList/title”/> -p. <fo: page-number/>
     </fo: block>
 </fo: static-content>

 <fo: flow flow-name=”xsl-region-body”>
   <fo:  block font-size=”20pt”
  font-family=”sans-serif”
line-height=”26pt”
background-color = “yellow”
color = “black”
text-align = “center”
padding-top = “3pt”>
     <xsl: apply-templates select=” BookList/title”/>
</fo: block>

<fo:  block font-size=”18pt”
font-family=”sans-serif”
line-height=”24pt”>
     <xsl: apply-templates select=” BookList /Book”/>
</fo: block>
</fo: flow>

</fo: page-sequence>
</fo: root>
</fo: template>

<xsl: template match=”BookList/title”>
   <xsl: value-of select=”.”/>
</xsl:template>

<xsl: template match=” BookList /Book”>
  <fo: block space-before=”8pt” space-after=”8pt”>
    <xsl: apply-templates select=”Name”/>
<xsl: apply-templates select =”Author”/>
<xsl: apply-templates select =”ISBN”/>
  </fo:block>
</xsl:template>

<xsl: template match=”Name”>
  <fo: block>
   <xsl: value-of select=”.”/>
  </fo: block>
</xsl:template>

<xsl: template match=”Author”>
  <fo: block>
   <xsl: value-of select=”.”/>
  </fo: block>
</xsl:template>

<xsl: template match=”ISBN”>
  <fo: block>
   <xsl: value-of select=”.”/>
  </fo: block>
</xsl:template>
</xsl: stylesheet>

The above five XSLT templates output the content of the various elements of our XML source document. They are applied in <fo: flow> element or its descendants. And the <fo: flow> element contains the main page content, other than the header and footer. In the xsl file, <fo: static-content> element utilizes xsl-region-before describing the header but we do not define the footer, these can be based on specific example.

0 评论:

发表评论