| XSL FO(Formatting Objects) |
|
| Tables |
|
| The fundamental table element in XSL is fo:table-and-caption. This is a block-level object that contains a fo:table and a fo:caption. If your table doesn’t need a caption, you can just use a raw fo:table instead. The XSL-FO table model is quite close toHTML's table model. Table below shows the mapping between HTML 4.0 table elements and XSL formatting objects: |
|
| HTML Tables vs. XSL Formatting Object Tables |
|
| HTML Element | XSL FO Element |
| TABLE | fo:table-and-caption |
| no equivalent | fo:table |
| CAPTION | fo:table-caption |
| COL | fo:table-column |
| COLGROUP | no equivalent |
| THEAD | fo:table-header |
| TBODY | fo:table-body |
| TFOOT | fo:table-footer |
| TD | fo:table-cell |
| TR | fo:table-row |
|
|
| Each fo:table-and-caption contains an optional fo:table-caption element and one fo:table element. The caption can contain any block-level elements you care to place in the caption. By default captions are placed before the table, but this can be adjusted by setting the caption-side property of the table-and-caption element to one of these eight values: |
|
• before
• after
• start
• end
• top
• bottom
• left
• right |
|
| For example, here's a table with a caption on the bottom: |
|
<fo:table-and-caption caption-side="bottom">
<fo:table-caption>
<fo:block font-weight="bold" font-family="Helvetica, Arial, sans" font-size="12pt"> |
|
Table 19-1: HTML Tables vs. XSL Formatting Object Tables
</fo:block>
</fo:table-caption>
<fo:table> <!-- table contents go here --> </fo:table>
</fo:table-and-caption> |
|
| The fo:table element contains fo:table-column elements, an optional fo:table-header, an optional fo:table-footer, and one or more fo:table-body elements. The fo:table-body is divided into fo:table-row elements. |
|
| Each fo:table-row is divided into fo:table-cell elements. The fo:table-header and fo:table-footer can either be divided into fo:table-cell or fo:table-row elements. For example, here's a simple table that includes the first three rows of Table above. |
|
<fo:table>
<fo:table-header> <fo:table-cell>
<fo:block font-family="Helvetica, Arial, sans" font-size="11pt" font-weight="bold"> |
|
HTML Element
</fo:block> </fo:table-cell>
<fo:table-cell> <fo:block font-family="Helvetica, Arial, sans" font-size="11pt" font-weight="bold"> |
|
XSL FO Element
</fo:block>
</fo:table-cell>
</fo:table-header>
<fo:table-body> <fo:table-row>
<fo:table-cell>
<fo:block font-family="Courier, monospace"> |
|
TABLE
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block font-family="Courier, monospace"> fo:table-and-caption </fo:block> </fo:table-cell>
|
|
</fo:table-row>
<fo:table-row>
<fo:table-cell>
<fo:block>no equivalent</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block font-family="Courier, monospace">
|
|
fo:table
</fo:block>
</fo:table-cell>
</fo:table-row>
</fo:table-body>
</fo:table>
|
|
| You can make table cells span multiple rows and columns by setting the number-columns-spanned and/or number-rows-spanned attributes to an integer giving the number of rows or columns to span. The optional column-number attribute can change which column the spanning begins in. The default is the current column. |
|
| Borders can be drawn around table parts using the normal border properties. The empty-cells attribute has the value show or hide; show if borders are to be drawn around cells with no content, hide if not. The default is show. |
|
| When a long table extends across multiple pages, sometimes the header and footer are repeated on each page. You can alter this behavior with the table-omit-header-at-break and table-omit-footer-at-break attributes of the fo:table element. The value false indicates that the header or footer is to be repeated from page to page. The value true indicates that it is not. The default is false. |
|
| The optional fo:table-column element is an empty element that specifies properties for all cells in a particular column. The cells it applies to are identified by the column-number attribute or by the position of the fo:table-column element itself. fo:table-column does not actually contain any cells. |
|
| A fo:table-column can apply properties to more than one consecutive column by setting the number-columns-spanned property to an integer greater than one. The most common property to set in a fo:table-column is column-width (a signed length) but the standard border, padding, and background properties (discussed below and mostly the same as in CSS) can also be set. |
|
| Caution |
|
| FOP 0.20.4 has limited table support. In particular, it does not support fo:table-caption or fo:table-and-caption. Furthermore, FOP requires you to explicitly specify the column widths using a fo:table-column element. You can't let it choose suitable widths as you might let a Web browser do. |
|
| For example, Listing below lays out all the properties of the elements in a table. Figure shows the first page of output produced by this style sheet. |
|
| An XSL style sheet that formats the elements as a table |
|
<?xml version="1.0"?> <xsl:stylesheet 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 xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="A4" page-width="297mm" page-height="210mm" margin-top="0.5in" margin-bottom="0.5in" margin-left="0.5in" margin-right="0.5in">
<fo:region-body/>
</fo:simple-page-master> |
|
</fo:layout-master-set>
<fo:page-sequence master-reference="A4">
<fo:flow flow-name="xsl-region-body">
<fo:table> <fo:table-column column-width="30mm"/>
<fo:table-column column-width="12mm"/>
<fo:table-column column-width="12mm"/>
<fo:table-column column-width="25mm"/>
<fo:table-column column-width="27mm"/>
<fo:table-column column-width="18mm"/>
<fo:table-column column-width="49mm"/>
<fo:table-column column-width="16mm"/>
<fo:table-column column-width="16mm"/>
<fo:table-column column-width="16mm"/>
<fo:table-column column-width="21mm"/>
<fo:table-column column-width="21mm"/>
<fo:table-column column-width="21mm"/>
<fo:table-body>
|
|
<xsl:apply-templates select="//ATOM">
<xsl:sort data-type="number"
select="ATOMIC_NUMBER"/>
</xsl:apply-templates>
</fo:table-body>
</fo:table> </fo:flow>
</fo:page-sequence> </fo:root>
</xsl:template> <xsl:template match="ATOM">
<fo:table-row> <fo:table-cell>
<fo:block><xsl:value-of select="NAME"/>
</fo:block>
</fo:table-cell> |
|
<fo:table-cell>
<fo:block><xsl:value-of select="SYMBOL"/></fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block>
<xsl:value-of select="ATOMIC_NUMBER"/>
</fo:block>
</fo:table-cell>
<fo:table-cell> <fo:block>
<xsl:value-of select="ATOMIC_WEIGHT"/>
</fo:block> </fo:table-cell>
<fo:table-cell>
<fo:block>
<xsl:value-of select="OXIDATION_STATES"/>
</fo:block> </fo:table-cell>
<fo:table-cell>
<fo:block><xsl:value-of select="DENSITY"/>
</fo:block> </fo:table-cell>
|
|
<fo:table-cell>
<fo:block>
<xsl:value-of select="ELECTRON_CONFIGURATION"/>
</fo:block>
</fo:table-cell> <fo:table-cell>
<fo:block>
<xsl:value-of select="ELECTRONEGATIVITY"/>
</fo:block> </fo:table-cell>
<fo:table-cell>
<fo:block> <xsl:value-of select="ATOMIC_RADIUS"/>
</fo:block> </fo:table-cell>
<fo:table-cell> <fo:block>
<xsl:value-of select="ATOMIC_VOLUME"/>
</fo:block>
|
|
</fo:table-cell>
<fo:table-cell>
<fo:block>
<xsl:value-of select="SPECIFIC_HEAT_CAPACITY"/>
</fo:block>
</fo:table-cell>
<fo:table-cell>
<fo:block> <xsl:value-of select="SPECIFIC_HEAT_CAPACITY"/>
</fo:block>
</fo:table-cell>
<fo:table-cell> <fo:block>
<xsl:value-of select="THERMAL_CONDUCTIVITY"/>
</fo:block> </fo:table-cell>
</fo:table-row>
</xsl:template>
</xsl:stylesheet> |
|
 |
|
|
|
|
| XSL FO(Formatting Objects) |
|
| Inlines |
|
| The fo:inline element has no particular effect on the layout of the page. Rather it's an element on which you can hang formatting attributes such as font-style or color for application to the inline's contents. |
|
| The fo:inline formatting object is a container that groups inline objects together. It cannot contain block-level elements. For example, you can use fo:inline elements to add style to various parts of the footer, like this: |
|
|
<fo:static-content flow-name="xsl-region-after">
<fo:block font-weight="bold" font-size="10pt" font-family="Arial, Helvetica, sans">
<fo:inline font-style="italic" text-align="start"> The XML Bible |
|
</fo:inline> <fo:inline text-align="centered"> Page <fo:page-number/>
</fo:inline>
<fo:inline text-align="right"> Chapter 19: XSL Formatting Objects </fo:inline>
</fo:block> </fo:static-content> |
|
|
|
|
| XSL FO(Formatting Objects) |
|
| Footnotes |
|
| The fo:footnote element creates a footnote. The author places the fo:footnoteelement in the flow exactly where the footnote reference such as 1 or * will occur. |
|
| The fo:footnote element contains both the reference text and a fo:footnote-body block-level element containing the text of the footnote. However, only the footnote reference is inserted inline. The formatter places the note text in the after region (generally the footer) of the page. |
|
| For example, this footnote uses an asterisk as a footnote marker and refers to "JavaBeans, Elliotte Rusty Harold (IDG Books, Foster City, 1998), p. 147". Standard properties such as font-size and vertical-align are used to format both the note marker and the text in the customary fashion. |
|
<fo:footnote>
<fo:inline font-size="smaller" vertical-align="super">* </fo:inline>
<fo:footnote-body font-size="smaller">
<fo:inline font-size="smaller" vertical-align="super"> * </fo:inline>
<fo:inline font-style="italic">JavaBeans</fo:inline>
, Elliotte Rusty Harold (IDG Books, Foster City, 1998), p. 147 </fo:footnote-body>
</fo:footnote>
|
|
| Tip |
|
| XSL-FO doesn't provide any means of automatically numbering and citing footnotes, but this can be done by judicious use of xsl:number in the transformation style sheet. XSLTransformations make end notes easy as well |
|
|
|
|
| XSL FO(Formatting Objects) |
|
| Floats |
|
| A fo:float produces a floating box anchored to the top of the region where it occurs. Afo:float is most commonly used for graphics, charts, tables or other out-of-line content that needs to appear somewhere on the page, although precisely where it appears is not particularly important. For example, this fo:block includes a floating graphic with a caption: |
|
<fo:block>
Although PDF files are themselves ASCII text,
this isn't a book about PostScript, so there's
nothing to be gained by showing you the exact
output of the above command. If you're curious,
open the PDF file in any text editor.
Instead, Figure
<fo:float float="before">
<fo:external-graphic src="4760-7fg1901.jpg"
height="485px" width="623px" />
<fo:block font-family="Helvetica, sans">
<fo:inline font-weight="bold">
Figure 19-1:
</fo:inline>
The PDF file displayed in Netscape Navigator
</fo:block>
</fo:float>
shows the rendered file displayed in
Netscape Navigator using the Acrobat plug-in.
</fo:block>
|
|
| The formatter tries to place the graphic somewhere on the same page where the content surrounding the fo:float appears. However, it may not always be able to find room on that page. If it can’t, it moves the object to a subsequent page. Within those limits, it's free to place it anywhere on the page. |
|
| The value of the float attribute indicates on which side of the page the fo:float floats. It can be set to before, start, end, left, right, none, or inherit. |
|
| The clear attribute can be set on elements near the floating object to indicate whether they'll flow around the side of the float or whether they'll move below the float. |
|
| It can have the values start (the start edge of the object must not be adjacent to a floating object), end (the end edge of the object must not be adjacent to a floating object), left (the left edge of the object must not be adjacent to a floating object), right (the right edge of the object must not be adjacent to a floating object), both (neither the left nor the right edge of the object may be adjacent to a floating object), none, or inherit. |
|
|
|
|
| XSL FO(Formatting Objects) |
|
| Formatting Properties |
|
| By themselves, formatting objects say relatively little about how content is formatted. They merely put content in abstract boxes, which are placed in particular parts of a page. Attributes on the various formatting objects determine how the content in those boxes is styled. |
|
| As already mentioned, there are more than 200 different formatting properties. Not all properties can be attached to all elements. For instance, there isn’t much point to specifying the font-style of a fo:external-graphic. Most properties, however, can be applied to more than one kind of formatting object element. |
|
| (The few that can’t, such as src and provisional-label-separation, were discussed above with the formatting objects they apply to.) When a property is common to multiple formatting objects, it shares the same syntax and meaning across the objects.For example, you use identical code to format a fo:title in 14-point Times bold as you do to format a fo:block in 14-point Times bold. |
|
| Many of the XSL-FO properties are similar to CSS properties. The value of a CSS font-family property is the same as the value of an XSL-FO font-family attribute. If you’ve read about CSS in Chapters 14 through 16, you’re already more than half finished learning XSL-FO properties. |
|
| The id property |
|
| The id property can be applied to any element. This is an XML ID-type attribute. The value of this property must, therefore, be an XML name that’s unique within the style sheet and within the output formatting object document. The last requirement is a little tricky because it's possible that one template rule in the style sheet may generate several hundred elements in the output document. The generate-id() function of XSLTcan be useful here. |
|
| The language property |
|
| The language property specifies the language of the content contained in either a fo:block or a fo:character element. Generally, the value of this property is an ISO 639language code such as en (English) or la (Latin). It may also be the keyword none or use-document. The latter means to simply use the language of the input as specified by the xml:lang attribute. For example, consider the first verse of Caesar’s Gallic Wars: |
|
<fo:block id="verse1.1.1" language="la">
Gallia est omnis divisa in partes tres, quarum unam incolunt Belgae, aliam Aquitani, tertiam qui ipsorum lingua Celtae, nostra Galli appellantur
</fo:block> |
|
| Although the language property has no direct effect on formatting, it may have an indirect effect if the formatter selects layout algorithms depending on the language. For instance, the formatter should use different default writing modes for Arabic and English text. This carries over into determination of the start and end regions and the inline and block progression directions. |
|
| Paragraph properties |
|
| Paragraph properties are styles that normally are thought of as applying to an entire block of text in a traditional word processor, although perhaps block-level text properties is a more appropriate name here. For example, indentation is a paragraph property, because you can indent a paragraph, but you can’t indent a single word. |
|
| Break properties |
|
| The break properties specify where page breaks are and are not allowed. There are seven loosely related break properties: |
|
• keep-with-next
• keep-with-previous
• keep-together
• break-before
• break-after |
|
| The keep-with-next property determines how much effort the formatter will expend to keep this formatting object on the same page as the following formatting object. The keep-with-previous property determines how much effort the formatter will expend to keep this formatting object on the same page as the preceding formatting object. |
|
| And the keep-together property determines how much effort the formatter will expend to keep the contents of this formatting object on one page. These are not hard and fast rules because it's always possible that a formatting object is just too big for one page. |
|
| Each of these properties can be set to an integer giving the strength of the effort to keep the objects on the same page (larger integers are stronger) or to the keywords always or auto. always means maximum effort; auto means let the breaks fall where they may. |
|
| By contrast, the break-before property and break-after properties mandate some kind of break. What exactly is broken is determined by the value of the property. This can be one of these five values: |
|
• column: Break the current column and move to the next column.
• page: Break the current page and move to the next page.
• even-page: Break the current page and move to the next even-numbered page, inserting a blank page if the current page is itself an even-numbered page.
• odd-page: Break the current page and move to the next odd-numbered page, inserting a blank page if the current page is itself an odd-numbered page.
• auto: Let the formatter decide where to break; the default. |
|
| For example, this template rule ensures that each ATOM of sufficiently small size is printed on a page of its own: |
|
<xsl:template match="ATOM">
<fo:block break-before="page" break-after="page">
<xsl:apply-templates/>
</fo:block>
</xsl:template> |
|
| Finally, the inhibit-line-breaks property is a boolean that can be set to true to indicate that not even a line break is allowed, much less a page break. |
|
| XSL-FO also defines three shorthand page-break properties: page-break-after, page-break-before, and page-break-inside. These are not absolutely necessary because their effects can be achieved by appropriate combinations of the keep and break properties. |
|
| For example, to specify a page break after an element, you'd set break-before to page and keep-with-previous to auto. |
|
| Hyphenation properties |
|
| The hyphenation properties determine where hyphenation is allowed and how it should be used. These properties apply only to soft or “optional†hyphens such as the ones sometimes used to break long words at the end of a line. |
|
| They do not apply to hard hyphens such as the ones in the word mother-in-law, although hard hyphens may affect where soft hyphens are allowed. There are six hyphenation properties. They are: |
|
| • hyphenate: Automatic hyphenation is allowed only if this property has the value true. |
|
| • hyphenation-character: The Unicode character used to hyphenate words, such as - in English. |
|
| • hyphenation-keep: One of the four keywords (column, none, page, inherit) that specify where and whether hyphenation is allowed. The default is not to hyphenate. |
|
| • hyphenation-ladder-count: A nonnegative integer that specifies the maximum number of hyphenated lines that may appear in a row. |
|
| • hyphenation-push-character-count: A nonnegative integer that specifies the minimum number of characters that must follow an automatically inserted hyphen. (Short syllables look bad in isolation.) |
|
| • hyphenation-remain-character-count: A nonnegative integer specifying the minimum number of characters that must precede an automatically inserted hyphen. |
|
| For example: |
|
<fo:block hyphenate="true"
hyphenation-character="-"
hyphenation-keep="none"
hyphenation-ladder-count="2"
hyphenation-push-character-count="4"
hyphenation-remain-character-count="4" >
some content...
</fo:block>
|
|
| XSL-FO does not specify a word-breaking algorithm to determine where a soft hyphen may be applied. Even when these properties allow hyphenation, it’s still completely up to the formatter to figure out how to hyphenate individual words. Indeed, basic formatters may not attempt to hyphenate words at all. |
|
| Indent properties |
|
| The indent properties specify how far lines are indented from the edge of the text. There are four of these: |
|
• start-indent
• end-indent
• text-indent
• last-line-end-indent |
|
| The start-indent property offsets all lines from the start edge (left edge in English). The end-indent property offsets all lines from the end edge (right edge in English). The text-indent property offsets only the first line from the start edge. |
|
| The last-line-end-indent property offsets only the last line from the start edge. Values are given as a signed length. For example, a standard paragraph with a half-inch, first-line indent might be formatted this way: |
|
<fo:block text-indent="0.5in">
The first line of this paragraph is indented
</fo:block> |
|
| A block quote with a one-inch indent on all lines on both sides is formatted like this: |
|
<fo:block start-indent="1.0in" end-indent="1.0in">
This text is offset one inch from both edges.
</fo:block> |
|
| Because the text-indent is added to the start-indent to get the total indentation of the first line, using a positive value for start-indent and a negative value for text-indent creates hanging indents. For example, all lines except the first in this paragraph are indented by one inch. The first line is only indented half an inch: |
|
<fo:block text-indent="-0.5in" start-indent="1.0in">
This paragraph uses a hanging indent.
</fo:block> |
|
|
|
|
| Input Whitespace & Output Serialization |
|
| <xsl:preserve-space> |
|
| The xsl:preserve-space element is used to keep white-space only nodes in the output. Note that the default is to leave white-space only nodes. Therefore, it is only necessary to use the xsl:preserve-space element when you use the xsl:strip-space element and wish to insure that certain white-space nodes are not removed. By white-space, we refer to carriage returns, line feeds, spaces and tabs. No text or numbers appear in the node. |
|
| The related xsl:strip-space element is used to remove white-space only nodes so that they do not appear in the output. This is a self-closing element and it cannot contain any child elements or any content. |
|
| elements="names" |
|
| The mandatory elements attribute is a white-space delimited list of the names of the elements in which all of the white-space nodes must not be removed. You can also use generic names with wild cards. |
|
| We use the eBIZ_com_Staff XML file for our example with the following header: <?xml-stylesheet type="text/xsl" href="xslt_example_preservespace.xsl"?>and we name it: eBIZ_com_Staff.xml |
|
| Code for xslt_example_preservespace1.xsl: |
|
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- DWXMLSource="sample23.xml" --><!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp " ">
<!ENTITY copy "©"
>
<!ENTITY reg "®"
>
<!ENTITY trade "™">
<!ENTITY mdash "—"
>
<!ENTITY ldquo "“">
<!ENTITY rdquo "”">
<!ENTITY pound "£">
<!ENTITY yen "¥">
<!ENTITY euro "€">
]>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:preserve-space elements="fname" />
<xsl:output method="html" encoding="iso-8859-1"
doctype-public="-//W3C//DTD XHTML 1.0
Transitional//EN" doctype-system=
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
|
|
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script>
var i=0;
function mm() {
i+=1;
document.data.srno.innerHTML=""+i;
}
</script>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1"/>
<title>Sample XSL Document</title>
<style type="text/css">
<xsl:comment>
.style3 {font-family: Geneva, Arial,
Helvetica, sans-serif; color: #0033FF; }
</xsl:comment>
</style>
</head>
<body>
|
|
<table width="59%" border="1" cellspacing="0" cellpadding="1" id="data">
<tr>
<td width="9%" nowrap="nowrap"
bgcolor="#6699FF"><strong>Sr no </strong></td>
<td width="22%" bgcolor="#6699FF">
<strong>Employee ID </strong></td>
<td width="30%" bgcolor="#6699FF">
<strong>Name</strong></td>
<td width="20%" bgcolor="#6699FF">
<strong>Department</strong></td>
<td width="19%" bgcolor="#6699FF">
<strong>Designation</strong></td>
<td width="19%" nowrap="nowrap" b
gcolor="#6699FF"><strong>Phone No </strong></td>
<td width="19%" nowrap="nowrap
"
bgcolor="#6699FF"><strong>Address </strong></td>
</tr>
<xsl:for-each select="ebiz/employee_details">
<tr>
<td><span class="style3">.
<script>i=i+1;document.write(i);</script>
</span></td>
|
|
<td nowrap="nowrap"><span class="style3">
<xsl:value-of select="emp_id" /></span></td>
<td nowrap="nowrap"><span class="style3"
><xsl:value-of select="fname"> </xsl:value-of>
<xsl:value-of select="lname"></xsl:value-of></span></td>
<td nowrap="nowrap"><xsl:value-of select="department" /></td>
<td nowrap="nowrap"><xsl:value-of select="designation" /></td>
<td nowrap="nowrap"><xsl:value-of select="phone" /></td>
<td nowrap="nowrap"><xsl:value-of select="address"/></td
>
</tr></xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
|
|
| Click here to view the file online |
|
| Source code for eBIZ_com_Staff.xml: |
|
<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl"
href="xslt_example_preservespace1.xsl"?>
<ebiz>
<employee_details>
<emp_id>eBIZTECH001</emp_id>
<fname>Mr Aman Kumar</fname>
<lname>Singh</lname>
<department>TECHICAL[Java]</department>
<designation>Sr. Developer</designation>
<phone> 32942</phone>
<address>Sector 44, Noida</address>
</employee_details>
<employee_details>
<emp_id>eBIZCSS001</emp_id>
<fname>Mrs Sunita</fname>
<lname>Singhania</lname>
<department>Customer Support[Web Assist
</department>
<designation>CCE</designation>
<phone> 000000</phone>
<address>Sector 66, Gurgaon</address>
|
|
</employee_details>
<employee_details>
<emp_id>eBIZAC001</emp_id>
<fname>Miss Amisha</fname>
<lname>Mishra</lname>
<department>Accounts</department>
<designation>Jr. Accountant</designation>
<phone> 32942</phone>
<address>Sector 66, Rohni</address>
</employee_details>
<employee_details>
<emp_id>eBIZAC001</emp_id>
<fname>ANITA</fname>
<lname>Mishra</lname>
<department>Accounts</department>
<designation>Jr. Accountant</designation>
<phone> 09999 </phone>
<address>Sector 66, Noida</address>
</employee_details>
|
|
<employee_details>
<emp_id>eBIZAC001</emp_id
>
<fname>ANKITA</fname>
<lname>DUBEY</lname>
<department>Accounts</department>
<designation>Jr. Accountant</designation>
<phone>032942 </phone>
<address>Sector 99, Noida</address>
</employee_details>
<employee_details>
<emp_id>eBIZAC001</emp_id>
<fname>AKSHITA</fname>
<lname>Jaiswal </lname>
<department>TECH</department>
<designation>Jr. DEVELOPER</designation>
<phone> (+91)99990</phone>
<address>Sector 66, Noida</address>
</employee_details>
<employee_details>
<emp_id>eBIZAC001</emp_id>
<fname>Shweta</fname>
<lname>Agrwal</lname>
<department>Accounts</department>
<designation>Jr. Accountant</designation>
<phone> 32942</phone>
<address>Sector 66, Noida</address>
</employee_details>
</ebiz>
|
|
Click the hyperlinks to view the file without XSL formatting
and with XSL formatting [IE Only] |
|
 |
|
|
|
|
| Input Whitespace & Output Serialization |
|
| <xsl:strip-space> |
|
| The xsl:strip-space element is used to remove white-space only nodes so that they do not appear in the output. By white-space, we refer to carriage returns, line feeds, spaces and tabs. No text or numbers appear in the node. |
|
| The related xsl:preserve-space element is used to keep white-space only nodes in the output. Note that the default is to leave white-space only nodes. Therefore, it is only necessary to use the xsl:preserve-space element when you use the xsl:strip-spaceelement and wish to insure that certain white-space nodes are not removed. |
|
| This is a self-closing element and it cannot contain any child elements or any content. |
|
| elements="names" |
|
| The mandatory elements attribute is a white-space delimited list of the names of the elements in which all of the white-space nodes can be removed. You can also use generic names with wild cards. For example, elements="*" removes all white-space nodes in all elements. |
|
| We use the eBIZ_com_Staff XML file for our example with the following header: |
|
| <?xml-stylesheet type="text/xsl" href="xslt_example_stripspace.xsl"?> |
|
| and we name it: eBIZ_com_Staff.xml |
|
| Source code for eBIZ_com_Staff.xml: |
|
<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl"
href="xslt_example_stripspace.xsl"?>
<ebiz>
<employee_details>
<emp_id>eBIZTECH001</emp_id>
<fname>Mr Aman Kumar</fname>
<lname>Singh</lname>
<department>TECHICAL[Java]</department>
<designation>Sr. Developer</designation>
<phone> 32942</phone>
<address>Sector 44, Noida</address>
</employee_details>
<employee_details>
<emp_id>eBIZCSS001</emp_id>
<fname>Mrs Sunita</fname>
<lname>Singhania</lname>
<department>Customer Support[
Web Assist]</department>
<designation>CCE</designation>
<phone> 000000</phone>
<address>Sector 66, Gurgaon</address>
</employee_details>
<employee_details>
<emp_id>eBIZAC001</emp_id>
|
|
<fname>Miss Amisha</fname>
<lname>Mishra</lname>
<department>Accounts</department>
<designation>Jr. Accountant</designation>
<phone> 32942</phone>
<address>Sector 66, Rohni</address>
</employee_details>
<employee_details>
<emp_id>eBIZAC001</emp_id>
<fname>ANITA</fname>
<lname>Mishra</lname>
<department>Accounts</department>
<designation>Jr. Accountant</designation>
<phone> 09999 </phone>
<address>Sector 66, Noida</address>
</employee_details>
|
|
<employee_details>
<emp_id>eBIZAC001</emp_id>
<fname>ANKITA</fname>
<lname>DUBEY</lname>
<department>Accounts</department>
<designation>Jr. Accountant</designation>
<phone>032942 </phone>
<address>Sector 99, Noida</address>
</employee_details>
<employee_details>
<emp_id>eBIZAC001</emp_id>
<fname>AKSHITA</fname>
<lname>Jaiswal </lname>
<department>TECH</department>
<designation>Jr. DEVELOPER</designation>
<phone> (+91)99990</phone>
|
|
<address>Sector 66, Noida</address>
</employee_details>
<employee_details>
<emp_id>eBIZAC001</emp_id>
<fname>Shweta</fname>
<lname>Agrwal</lname>
<department>Accounts</department>
<designation>Jr. Accountant</designation>
<phone> 32942</phone>
<address>Sector 66, Noida</address>
</employee_details>
</ebiz>
|
|
| Click the hyperlinks to view the file without XSL . |
|
| Source code for xslt_example_stripspace.xsl : |
|
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp " ">
<!ENTITY copy "©">
<!ENTITY reg "®">
<!ENTITY trade "™">
<!ENTITY mdash "—">
<!ENTITY ldquo "“">
<!ENTITY rdquo "”">
<!ENTITY pound "£">
<!ENTITY yen "¥">
<!ENTITY euro "€">
]>
<xsl:stylesheet version="1.0" xmlns:xsl=
"http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="phone address" />
<xsl:preserve-space elements="fname" />
<xsl:output method="html" encoding="iso-8859-1"
doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
doctype-system="http://www.w3.org/TR/
xhtml1/DTD/xhtml1-transitional.dtd"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script>
var i=0;
function mm() {
i+=1;
document.data.srno.innerHTML=""+i;
}
</script>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>Sample XSL Document</title>
<style type="text/css">
<xsl:comment>
.style3 {font-family: Geneva, Arial, Helvetica, sans-serif; color: #0033FF; }
</xsl:comment>
</style>
</head>
<body>
<table width="59%" border="1" cellspacing="0" cellpadding="1" id="data">
<tr>
|
|
<td width="9%" nowrap="nowrap" bgcolor="#6699FF">
<strong>Sr no </strong></td>
<td width="22%" bgcolor="#6699FF">
<strong>Employee ID </strong></td>
<td width="30%" bgcolor="#6699FF">
<strong>Name</strong></td>
<td width="20%" bgcolor="#6699FF">
<strong>Department</strong></td>
<td width="19%" bgcolor="#6699FF">
<strong>Designation</strong></td>
<td width="19%" nowrap="nowrap" bgcolor="#6699FF"
><strong>Phone No </strong></td>
<td width="19%" nowrap="nowrap" bgcolor="#6699FF"
><strong>Address </strong></td>
</tr>
<xsl:for-each select="ebiz/employee_details">
<tr>
|
|
<td><xsl:number value="position()" format="A. " /></td>
<td nowrap="nowrap"><span class="style3">
<xsl:value-of select="emp_id" /></span></td>
<td nowrap="nowrap"><span class="style3">
<xsl:value-of select="fname"> </xsl:value-of>
<xsl:value-of select="lname"></xsl:value-of></span></td>
<td nowrap="nowrap"><xsl:value-of select="department" /></td>
<td nowrap="nowrap"><xsl:value-of select="designation" /></td>
<td nowrap="nowrap"><xsl:value-of select="phone" /></td>
<td nowrap="nowrap"><xsl:value-of select="address"/></td>
</tr></xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
|
|
| Click on hyperlink to view the XSL file or XML file with formatting.[IE only] |
|
|
|
|
| Input Whitespace & Output Serialization |
|
| <xsl:output> |
|
| The xsl:output element is used to define the format of the output created by the stylesheet. This is accomplished by setting one or more of ten optional attributes. The most important of these ten attributes is the method attribute which dictates if the type of output is HTML, text, or XML. The type of output, in turn, dictates which of the other nine attributes can be applied to the output. |
|
| The following table defines which attributes can optionally be set for each of the three types of output. A dash signifies that the attribute cannot effect the output. |
|
<xsl:output
cdata-section-elements="namelist"
doctype-public="string"
doctype-system="string"
encoding="string"
indent="yes" | "no"
media-type="mimetype"
method="html" | "name" | "text" | "xml"
omit-xml-declaration="yes" | "no"
standalone="yes" | "no"
version="version_number"
/>
|
|
| Attribute | HTML | Text | XML |
| cdata-section-elements | - | - | YES |
| doctype-public | YES | - | YES |
| doctype-system | YES | - | YES |
| encoding | YES | YES | YES |
| indent | YES | - | YES |
| media-type | YES | YES | YES |
| omit-xml-declaration | - | - | YES |
| standalone | - | - | YES |
| version | YES | - | YES |
|
|
| You can have zero or more xsl:output elements: |
|
| • If there is more than one xsl:output element, the XSLT processor essentially combines the information. |
|
| • If more than one xsl:output element sets the same attribute, the element with the highest import precedence will have its attribute selected. |
|
| • If more than one xsl:output element repeats an attribute and has the same highest import precedence, either the last will be chosen or an error will be declared |
|
| • If there is more than one cdata-section-elements attribute, all of the values in the name lists will effectively be merged into one list. |
|
The xsl:output element can only be a child of the xsl:stylesheet or the xsl:transform elements.
This is a self-closing element and it cannot contain any child elements or any content. |
|
| cdata-section-elements="namelist" |
|
| The optional cdata-section-elements attribute is set to a white-space delimited list of qnames (element names) whose content is to be output in CDATA sections. CDATA sections permit the use of sequences of characters that contain markup elements without violating the XML requirement to be well-formed (i.e., all tags and elements are closed). |
|
| doctype-public="string" |
|
| The optional doctype-public attribute specifies the public identifiers that go in the document type declaration (DTD). If the doctype-system attribute is not set, then the doctype-public attribute is ignored. |
|
| doctype-system="string" |
|
| The optional doctype-system attribute specifies the system identifiers that go in the document type declaration (DTD). The DTD should go immediately after the XML declaration. |
|
| encoding="string" |
|
| The optional encoding attribute specifies the preferred character encoding which is used to encode sequences of characters as sequences of bytes. |
|
| indent="yes" | "no" |
|
| The optional indent attribute specifies whether or not to indent. If set to yes, the XML and HTML outputs are step-indented to make them more readable. |
|
| media-type="mimetype" |
|
| The optional media-type attribute sets the MIME type. The default is: media-type="text/xml" |
|
| method="html" | "qname" | "text" | "xml" |
|
| The optional method attribute dictates the type of output. The three permitted values are HTML, text and XML. (Some XSLT processors recognize a qname as being an acceptable value for this attribute, but it is not part of the W3C standard.) The default is XML. However, if the first child element of the root node is the HTML <html> tag and there are no preceding text nodes, then the default output type is set to HTML. |
|
| omit-xml-declaration="yes" | "no" |
|
| The optional omit-xml-declaration attribute dictates whether the XSLT processor should output an XML declaration. The default is no and an XML declaration is output. If yes, there is no output. |
|
| standalone="yes" | "no" |
|
| The optional standalone attribute dictates whether the XSLT processor should output a standalone declaration. Yes signifies that it will. No, the default, signifies that it will not. |
|
| version="version_number" |
|
The optional version attribute provides the W3C version number for the output format. If the output is XML, the default version is 1.0 (currently, the only X3C version). Or if the output type is HTML, the default version is 4.0.
We use the eBIZ.com Staff XML file for our example with the following header: |
|
| <?xml-stylesheet type="text/xsl" href="xslt_example_output.xsl"?> |
|
| and we name it: xslt_example_output.xml |
|
| In this example, we declare our output to be in HTML. (By default, the occurrence of the <html> tag in the code signifies to the Microsoft XSLT processor we are using that the output is to be in HTML.) |
|
| Code for xslt_example_output.xsl: |
|
<?xml version="1.0" encoding="iso-8859-1"?><!DOCTYPE xsl:stylesheet [
<!ENTITY nbsp " ">
<!ENTITY copy "©">
<!ENTITY reg "®">
<!ENTITY trade "™">
<!ENTITY mdash "—">
<!ENTITY ldquo "“">
<!ENTITY rdquo "”">
<!ENTITY pound "£">
<!ENTITY yen "¥">
<!ENTITY euro "€">
]>
<xsl:stylesheet xmlns:xsl=
"http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" version="4.0" />
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script>
var i=0;
function mm() {
i+=1;
document.data.srno.innerHTML=""+i;
}
</script>
|
|
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1"/>
<title>Sample XSL Document</title>
<style type="text/css">
<xsl:comment>
.style3 {font-family: Geneva, Arial,
Helvetica, sans-serif; color: #0033FF; }
</xsl:comment>
</style>
</head>
<body>
<table width="59%" border="1"
cellspacing="0" cellpadding="1" id="data">
<tr>
<td width="9%" nowrap="nowrap"
bgcolor="#6699FF"><strong>Sr no </strong></td>
<td width="22%" bgcolor="#6699FF">
<strong>Employee ID </strong></td>
<td width="30%" bgcolor="#6699FF"
><strong>Name</strong></td>
<td width="20%" bgcolor="#6699FF">
<strong>Department</strong></td>
<td width="19%" bgcolor="#6699FF">
<strong>Designation</strong></td>
<td width="19%" nowrap="nowrap"
bgcolor="#6699FF"><strong>Phone No </strong></td>
<td width="19%" nowrap="nowrap
" bgcolor="#6699FF"><strong>Address </strong></td>
</tr>
<xsl:for-each select="ebiz/employee_details">
<tr>
|
|
<td><xsl:number value="position()" format="A. " /></td>
<td nowrap="nowrap"><span class="style3">
<xsl:value-of select="emp_id" /></span></td>
<td nowrap="nowrap"><span class="style3"
><xsl:value-of select="fname"> </xsl:value-of>
<xsl:value-of select="lname"></xsl:value-of></span></td>
<td nowrap="nowrap"><xsl:value-of select="department" /></td>
<td nowrap="nowrap"><xsl:value-of select="designation" /></td>
<td nowrap="nowrap"><xsl:value-of select="phone" /></td>
<td nowrap="nowrap"><xsl:value-of select="address"/></td>
</tr></xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
|
|
| Click here to view the resulting XML file or the XSL file. |
|
|
|
|
| Input Whitespace & Output Serialization |
|
| <xsl:namespace-alias> |
|
<xsl:namespace-alias
stylesheet-prefix="prefix"
result-prefix="prefix"
/>
|
|
| The xsl:namespace-alias element is used to replace a namespace (prefix) in a stylesheet with a different namespace (prefix) for use in the output. There must be onexsl:namespace-alias element for each namespace you wish to change. |
|
| In general, the primary use for this element is to convert non-XSLT elements (literal result elements) into XSLT elements for display in the output. The literal result elements are said to be mapped to the XSLT namespace. |
|
| A xsl:namespace-alias element can only be a child of the xsl:stylesheet or the xsl:transform elements. |
|
| This is a self-closing element and it cannot contain any child elements or any content. |
|
| stylesheet-prefix |
|
| The mandatory stylesheet-prefix attribute is the namespace (prefix) that you wish to change. If there is more than one xsl:namespace-alias element that have the same value for the stylesheet-prefix attribute, the one with the highest import precedence is selected. (If there is a tie on import precedence, an error occurs.) |
|
| result-prefix |
|
| The mandatory result-prefix attribute is the new namespace (prefix) that will appear in the output. For example, if you are mapping literal result elements into the XSLT namespace, then this value will be: result-prefix="xsl" |
|
| We use the eBIZ.com Staff List XML file for our example with the following header: <?xml-stylesheet type="text/xsl" href="xslt_example_namespacealias.xsl"?>and we name it: xslt_namespacealias.xml |
|
| This stylesheet generates a very simple stylesheet where the guru prefix is converted to the xsl prefix. |
|
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0" xmlns:ebiz="ebiz.xsl">
<xsl:param name="var">name</xsl:param>
<xsl:param name="blank"></xsl:param>
<xsl:namespace-alias stylesheet-prefix="ebiz" result-prefix="xsl" />
<xsl:template match="/">
<ebiz:stylesheet version="1.0">
<ebiz:variable name="{$var}">
<ebiz:value-of select="{$blank}" />
</ebiz:variable>
</ebiz:stylesheet>
</xsl:template>
</xsl:stylesheet>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
0 comments: