|By Frank Neugebauer||
|March 28, 2003 12:00 AM EST||
The Extensible Stylesheet Language (XSL) W3C recommendation was created as a means to display XML data. The recommendation includes a transformation language (XSLT) and formatting object (or output format) language (XSL-FO), which together provide the XSL stylesheet developer with the tools necessary to present XML.
The XSL-FO language is like HTML on steroids, because it allows not only cascading stylesheet (CSS)-type functionality, but also pagination and page layout, which are not available using HTML alone (at least not to as great a degree as with XSL-FO). However, such robustness comes at a cost; the language is very (very) verbose, which makes writing XSL-FO XSL stylesheets very difficult; thus, XSL-FO stylesheets are supposed to be produced as the result of an XSL transformation. But someone has to write the XSL stylesheet that transforms the XML into XSL-FO, and that's where this article can help.
The purpose of this article is to show you, the XSL stylesheet developer, how to make XSL-FO XSL stylesheets that are flexible and reusable. In other words, I'm going to show you how to make modularized XSL-FO XSL stylesheets.
Many of the key concepts presented in this article are not XSL-FO specific and can be leveraged to write any kind of XSL stylesheet. However, I concentrated on XSL-FO XSL stylesheets because their verbosity highlights the need for modularization more than most stylesheets.
I'm assuming a working knowledge of both XSLT and XSL-FO. For an introduction, you can refer to many resources on the Internet, including my article in the January 2002 edition of XML-Journal.
Overview of the Solution
Although this modularization solution is very flexible, it is also very simple and uses the XSL <xsl:import> and <xsl:attribute-set> elements, along with the xsl:use-attribute-sets attribute. When used properly, these elements and this attribute not only allow you to externalize the way an XML document is transformed into an XSL-FO document (<xsl:import>), but also the look-and-feel of the eventual FO document (<xsl:attribute-set>, xsl:use-attribute-sets). Figure 1 depicts all the pieces of these concepts.
From a high level, the XSLT engine reads in an XML document and XSL stylesheet (nothing new there). The main stylesheet, the one that is read by the XSLT engine, then uses <xsl:import> to use two additional stylesheets - one for the layout of the XSL-FO document and another for the "look-and-feel." The layout document then uses the use-attribute-sets attribute to get the "look-and-feel" (i.e., element attribution information) data from the "look-and-feel" XSL stylesheet, which is implemented as a series of <xsl:attribute-set> elements.
With the layout and look-and-feel externalized and separated into different stylesheets, the stylesheet writer can easily exchange both/either the layout and/or look-and-feel of the XSL-FO document information. If the XSL-FO document has such flexibility, it follows that the layout and look-and-feel of the eventual output document, a PDF file for example, has equal flexibility.
A Presentation Example
The example I'm going to present is a good case for the advantages of using a modular design in XSL. In presentation graphics (e.g., Microsoft PowerPoint), there are templates that can change the layout of pages and the color/graphics scheme of your presentation. I am going to replicate, to some degree, that same kind of functionality using XSL. If you use the ideas in this example, you can impress your friends by creating PDF presentations that are cross-platform with data separated from the presentation (which implies that the same data could potentially be used in entirely different formats).
The XML file
The data for the sample is represented in a simple XML file, named presentation.xml, that depicts the idea of a presentation with pages containing concepts (such a format is probably a little too coupled to the presentation, but it'll work). The root element of the document is "Presentation," with descriptive child elements called "Title," "Author," "Date," and "CorporateLogo," and a series of "Page" elements that take the following form:
The "style" attribute is referred to in the layout stylesheet to create the proper text layout in the presentation (e.g, PDF) output. The example is bullet, but you could extend the idea to be graphic, column, bullet-graphic, or any other style you require. You'll see shortly how the value of this attribute is used.
The position element allows you to put the "Page" elements in any order within the XML document and still have them laid out the way you want in the final output. Like the "style" attribute, the "Position" element is used by the layout stylesheet.
The main XSL stylesheet
Ultimately, the XSLT engine requires the name of a stylesheet to be processed. In this example, that file is called main.xsl and is very simple since its main purpose is to point to the correct layout and look-and-feel stylesheet. Listing 1 shows the contents of that stylesheet, minus the XML declaration and <xsl:stylesheet> element.
Within Listing 1, the <xsl:import> elements point to the layout and look-and-feel stylesheet, respectively. To change the template, you simply create a new layout and/or look-and-feel stylesheet and change the value of the import.
The other interesting point of the main.xsl stylesheet is simply that it creates the <fo:root> element and uses the <xsl:apply-templates> to transfer processing to other templates (which are defined in the plainPageLayout.xsl stylesheet).
The lookandfeel.xsl stylesheet contains a number of <xsl:attribute-set> definitions, which can be referenced using the xsl:use-attribute-sets property with an FO element. The contents of each <xsl:attribute-set> are the equivalent of attributes on XSL elements.
Take, for instance, a relatively simple <fo:simple-page-master> element, which defines a template for a page.
You can externalize all the attributes of the <fo:simple-page-master> element into the lookandfeel.xsl file by defining an <xsl:attribute-set> element as follows:
Then, you can refer to the attribute set within the <fo:simple-page-master> by using the "xsl:use-attribute-sets" attribute, such as:
Not only is the syntax easier to read, but the attributes values themselves are now externalized from the layout in what's essentially the FO equivalent of an external CSS.
In XSL-FO, layout is a matter of setting up page definitions and their contents. In my example, I use the page layout XSL document, named plainPageLayout.xsl, to perform the template matching on the XML document contents. I did this because the layout is responsible for understanding the output document, something that requires knowledge of the XML input, since the data from the latter becomes part of the former.
The plainPageLayout.xsl document itself looks like most XSL-FO stylesheets. However, instead of very long attribute listings (for look-and-feel type attributes), there are several xsl:use-attribute-sets attributes.
In terms of XSLT, plainPageLayout. xsl contains three templates: one each to process the "Presentation," "Page," and "Concept" elements, respectively. That presentation template creates the basic layout of the resulting XSL-FO document and then creates the title page, using data from the XML document. Throughout this stylesheet, xsl:use-attribute-sets attributes are used within <fo> elements to retrieve attributes from lookandfeel.xsl.
The Page template actually processes Page elements by style. That is, the
element will catch the "bullet" style pages only. Within the template, the non-title pages are set up with a title and two blank lines, after which the "Concept" elements are selected within a <xsl:apply templates select="Concept"/> statement. Finally, the "Concept" template processes each "Concept," creating an XSL-FO list with bullets.
As a side note, the stylesheet can be made even more modularized if you were to externalize your "Page" and/or "Concept" templates. Doing so would let you use the "bullet" style across different layout XSL documents. I did not do so for the sake of simplicity.
Listing 2 shows the entire contents of plainPageLayout.xsl. Within Listing 2, notice the comment that reads "<!-- Shows how to override attributes. -->." What I'm showing at that point in the stylesheet is that even if an XSL-FO element uses an attribute set, you can still override and/or amend the attribute set within the element itself, much in the same way you can override/add to an HTML element if a CSS is defined - further evidence of the CSS-like nature of attribute sets.
I believe that the proof of any technology is in the pudding. When you run presentation.xml and main.xsl through an FO processor (I used FOP 0.20.3), you will end up with a three-page PDF (if you choose the PDF output option within FOP). Figures 2 and 3 show the first two pages. Page 3 is just like page 2 but contains different concepts (refer to presentation.xml).
Figure 2 shows not only the main "Presentation" element (and the relevant non-page children of "Presentation"), but it also shows some of the XSL-FO design I created. For instance, the logo and yellow background color. The color and font characteristics are externalized within lookandfeel.xsl, of course.
Figure 3 uses the same basic layout characteristics of the title page, but also contains the list of concepts. Every "Page" that uses the "bullet" style will be rendered just as Figure 3 (refer to the third page of presentation.pdf if you download the source code, available at www.sys-con.com/xml/sourcec.cfm).
XSL-FO XSL stylesheets are long and difficult to read and write. Furthermore, it's not easy to change layout and look-and-feel without additional modularization. Using the XSL <xsl:import> and <xsl:attribute-sets> elements along with the xsl:use-attribute-sets attribute can go a very long way toward making such modularization a reality.
Smart Cities are here to stay, but for their promise to be delivered, the data they produce must not be put in new siloes. In his session at @ThingsExpo, Mathias Herberts, Co-founder and CTO of Cityzen Data, discussed the best practices that will ensure a successful smart city journey.
Jan. 23, 2017 05:00 AM EST Reads: 2,141
Web Real-Time Communication APIs have quickly revolutionized what browsers are capable of. In addition to video and audio streams, we can now bi-directionally send arbitrary data over WebRTC's PeerConnection Data Channels. With the advent of Progressive Web Apps and new hardware APIs such as WebBluetooh and WebUSB, we can finally enable users to stitch together the Internet of Things directly from their browsers while communicating privately and securely in a decentralized way.
Jan. 23, 2017 03:00 AM EST Reads: 1,016
"Tintri was started in 2008 with the express purpose of building a storage appliance that is ideal for virtualized environments. We support a lot of different hypervisor platforms from VMware to OpenStack to Hyper-V," explained Dan Florea, Director of Product Management at Tintri, in this SYS-CON.tv interview at 18th Cloud Expo, held June 7-9, 2016, at the Javits Center in New York City, NY.
Jan. 23, 2017 02:30 AM EST Reads: 5,014
Data is an unusual currency; it is not restricted by the same transactional limitations as money or people. In fact, the more that you leverage your data across multiple business use cases, the more valuable it becomes to the organization. And the same can be said about the organization’s analytics. In his session at 19th Cloud Expo, Bill Schmarzo, CTO for the Big Data Practice at Dell EMC, introduced a methodology for capturing, enriching and sharing data (and analytics) across the organization...
Jan. 23, 2017 02:30 AM EST Reads: 3,372
Manufacturers are embracing the Industrial Internet the same way consumers are leveraging Fitbits – to improve overall health and wellness. Both can provide consistent measurement, visibility, and suggest performance improvements customized to help reach goals. Fitbit users can view real-time data and make adjustments to increase their activity. In his session at @ThingsExpo, Mark Bernardo Professional Services Leader, Americas, at GE Digital, discussed how leveraging the Industrial Internet and...
Jan. 23, 2017 02:00 AM EST Reads: 6,648
Data is the fuel that drives the machine learning algorithmic engines and ultimately provides the business value. In his session at 20th Cloud Expo, Ed Featherston, director/senior enterprise architect at Collaborative Consulting, will discuss the key considerations around quality, volume, timeliness, and pedigree that must be dealt with in order to properly fuel that engine.
Jan. 23, 2017 01:45 AM EST Reads: 2,773
Why do your mobile transformations need to happen today? Mobile is the strategy that enterprise transformation centers on to drive customer engagement. In his general session at @ThingsExpo, Roger Woods, Director, Mobile Product & Strategy – Adobe Marketing Cloud, covered key IoT and mobile trends that are forcing mobile transformation, key components of a solid mobile strategy and explored how brands are effectively driving mobile change throughout the enterprise.
Jan. 23, 2017 12:00 AM EST Reads: 5,884
IoT is at the core or many Digital Transformation initiatives with the goal of re-inventing a company's business model. We all agree that collecting relevant IoT data will result in massive amounts of data needing to be stored. However, with the rapid development of IoT devices and ongoing business model transformation, we are not able to predict the volume and growth of IoT data. And with the lack of IoT history, traditional methods of IT and infrastructure planning based on the past do not app...
Jan. 22, 2017 11:00 PM EST Reads: 1,042
With major technology companies and startups seriously embracing IoT strategies, now is the perfect time to attend @ThingsExpo 2016 in New York. Learn what is going on, contribute to the discussions, and ensure that your enterprise is as "IoT-Ready" as it can be! Internet of @ThingsExpo, taking place June 6-8, 2017, at the Javits Center in New York City, New York, is co-located with 20th Cloud Expo and will feature technical sessions from a rock star conference faculty and the leading industry p...
Jan. 22, 2017 07:45 PM EST Reads: 3,874
"LinearHub provides smart video conferencing, which is the Roundee service, and we archive all the video conferences and we also provide the transcript," stated Sunghyuk Kim, CEO of LinearHub, in this SYS-CON.tv interview at @ThingsExpo, held November 1-3, 2016, at the Santa Clara Convention Center in Santa Clara, CA.
Jan. 22, 2017 03:15 PM EST Reads: 1,695
Internet of @ThingsExpo, taking place June 6-8, 2017 at the Javits Center in New York City, New York, is co-located with the 20th International Cloud Expo and will feature technical sessions from a rock star conference faculty and the leading industry players in the world. @ThingsExpo New York Call for Papers is now open.
Jan. 22, 2017 02:30 PM EST Reads: 3,767
"There's a growing demand from users for things to be faster. When you think about all the transactions or interactions users will have with your product and everything that is between those transactions and interactions - what drives us at Catchpoint Systems is the idea to measure that and to analyze it," explained Leo Vasiliou, Director of Web Performance Engineering at Catchpoint Systems, in this SYS-CON.tv interview at 18th Cloud Expo, held June 7-9, 2016, at the Javits Center in New York Ci...
Jan. 22, 2017 02:00 PM EST Reads: 5,836
The 20th International Cloud Expo has announced that its Call for Papers is open. Cloud Expo, to be held June 6-8, 2017, at the Javits Center in New York City, brings together Cloud Computing, Big Data, Internet of Things, DevOps, Containers, Microservices and WebRTC to one location. With cloud computing driving a higher percentage of enterprise IT budgets every year, it becomes increasingly important to plant your flag in this fast-expanding business opportunity. Submit your speaking proposal ...
Jan. 22, 2017 02:00 PM EST Reads: 5,302
WebRTC is the future of browser-to-browser communications, and continues to make inroads into the traditional, difficult, plug-in web communications world. The 6th WebRTC Summit continues our tradition of delivering the latest and greatest presentations within the world of WebRTC. Topics include voice calling, video chat, P2P file sharing, and use cases that have already leveraged the power and convenience of WebRTC.
Jan. 22, 2017 01:45 PM EST Reads: 3,238
20th Cloud Expo, taking place June 6-8, 2017, at the Javits Center in New York City, NY, will feature technical sessions from a rock star conference faculty and the leading industry players in the world. Cloud computing is now being embraced by a majority of enterprises of all sizes. Yesterday's debate about public vs. private has transformed into the reality of hybrid cloud: a recent survey shows that 74% of enterprises have a hybrid cloud strategy.
Jan. 22, 2017 01:00 PM EST Reads: 4,398
Discover top technologies and tools all under one roof at April 24–28, 2017, at the Westin San Diego in San Diego, CA. Explore the Mobile Dev + Test and IoT Dev + Test Expo and enjoy all of these unique opportunities: The latest solutions, technologies, and tools in mobile or IoT software development and testing. Meet one-on-one with representatives from some of today's most innovative organizations
Jan. 22, 2017 12:45 PM EST Reads: 1,698
DevOps is being widely accepted (if not fully adopted) as essential in enterprise IT. But as Enterprise DevOps gains maturity, expands scope, and increases velocity, the need for data-driven decisions across teams becomes more acute. DevOps teams in any modern business must wrangle the ‘digital exhaust’ from the delivery toolchain, "pervasive" and "cognitive" computing, APIs and services, mobile devices and applications, the Internet of Things, and now even blockchain. In this power panel at @...
Jan. 22, 2017 11:45 AM EST Reads: 2,979
Data is the fuel that drives the machine learning algorithmic engines and ultimately provides the business value. In his session at Cloud Expo, Ed Featherston, a director and senior enterprise architect at Collaborative Consulting, discussed the key considerations around quality, volume, timeliness, and pedigree that must be dealt with in order to properly fuel that engine.
Jan. 22, 2017 11:30 AM EST Reads: 3,759
The WebRTC Summit New York, to be held June 6-8, 2017, at the Javits Center in New York City, NY, announces that its Call for Papers is now open. Topics include all aspects of improving IT delivery by eliminating waste through automated business models leveraging cloud technologies. WebRTC Summit is co-located with 20th International Cloud Expo and @ThingsExpo. WebRTC is the future of browser-to-browser communications, and continues to make inroads into the traditional, difficult, plug-in web co...
Jan. 22, 2017 09:30 AM EST Reads: 3,040
"A lot of times people will come to us and have a very diverse set of requirements or very customized need and we'll help them to implement it in a fashion that you can't just buy off of the shelf," explained Nick Rose, CTO of Enzu, in this SYS-CON.tv interview at 18th Cloud Expo, held June 7-9, 2016, at the Javits Center in New York City, NY.
Jan. 22, 2017 09:30 AM EST Reads: 4,782