Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 50 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
50
Dung lượng
532,22 KB
Nội dung
Figure 6.10: XMetaL, a WYSIWYG editor XSLFO CSS is a simple and efficient styling mechanism. However, it is limited to styling a document, it cannot reorganize or otherwise process them. CSS cannot build a table of contents or extract an index as XSLT. XSLT and CSS Nothing prevents you from combining XSLT with CSS. Listing 6.5 shows how an XSLT style sheet can attach a CSS style sheet to a document and create a table of contents in XML. Figure 6.11 shows the result in a browser. Listing 6.5: XSLT Style Sheet <?xml version=”1.0” encoding=”ISO-8859-1”?> <xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform/”> <xsl:template match=”/”> <xsl:processing-instruction name=”xml-stylesheet”> href=”article.css” type=”text/css” </xsl:processing-instruction> <xsl:apply-templates/> </xsl:template> 185 XSLFO EXAMPLE continues 08 2429 CH06 2.29.2000 2:22 PM Page 185 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Listing 6.5: continued <xsl:template match=”keywords”> <keywords><xsl:apply-templates/></keywords> <section> <title>Table of Contents</title> <xsl:for-each select=”/article/section/title”> <p><xsl:value-of select=”.”/></p> </xsl:for-each> </section> </xsl:template> <xsl:template match=”*”> <xsl:copy><xsl:apply-templates/></xsl:copy> </xsl:template> </xsl:stylesheet> 186 Chapter 6: XSL Formatting Objects and Cascading Style Sheet OUTPUT Figure 6.11: The result in a browser Figure 6.12 shows how it works. First, you apply an XSLT style sheet to the document. 08 2429 CH06 2.29.2000 2:22 PM Page 186 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. ✔ Refer to Chapter 5, “XSL Transformation,” for instructions on how to apply XSLT style sheets with LotusXSL. This XSLT style sheet creates an XML document, not an HTML document. It reorganizes the document by creating a table of contents. The XSLT style sheet also inserts a processing instruction that links the XML document to a CSS style sheet. The browser loads the XML document and the CSS style sheet to format it. The major advantage of this solution, when compared to using XSLT to cre- ate an HTML document, is that the final document is in XML. Therefore, the final document still contains structure-rich markup. 187 XSLFO Figure 6.12: Combining XSLT and CSS XSLFO If using CSS in combination with XSLT makes sense, why not offer CSS features in XSLT? This is the reasoning behind XSLFO. XSLFO essentially ports the CSS properties to XSL. Listing 6.6 is a simple XSLFO style sheet. Figure 6.13 shows the result in InDelv, currently the only browser on the market to support XSLFO. Listing 6.6: A Simple XSLFO Style Sheet <?xml version=”1.0”?> <xsl:stylesheet xmlns:xsl=”http://www.w3.org/TR/WD-xsl” xmlns:fo=”http://www.w3.org/TR/WD-xsl/FO”> <xsl:template match=”/”> <fo:display-sequence start-indent=”5pt” end-indent=”5pt” font-size=”10pt” font-family=”serif”> EXAMPLE continues 08 2429 CH06 2.29.2000 2:22 PM Page 187 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Listing 6.5: continued <xsl:apply-templates/> </fo:display-sequence> </xsl:template> <xsl:template match=”p”> <fo:block> <xsl:apply-templates/> </fo:block> </xsl:template> <xsl:template match=”title”> <fo:block font-size=”13pt” font-weight=”bold”> <xsl:apply-templates/> </fo:block> </xsl:template> <xsl:template match=”url”> <fo:inline-link destination=”text()” color=”blue”> <xsl:apply-templates/> </fo:inline-link> </xsl:template> <xsl:template match=”date”/> <xsl:template match=”keywords”/> <xsl:template match=”abstract”/> <xsl:template match=”copyright”/> </xsl:stylesheet> 188 Chapter 6: XSL Formatting Objects and Cascading Style Sheet 08 2429 CH06 2.29.2000 2:22 PM Page 188 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Figure 6.13: An XSLFO style sheet in a browser An XSLFO style sheet is a list of XSL templates. The templates create for- matting objects in the resulting tree. These formatting objects are equiva- lent to CSS’ flow objects. In Listing 6.6, you will recognize formatting objects for block boxes (for example, fo:block ) and inline boxes (for example, fo:inline-link ). The object properties are word for word taken from the CSS specification. XLSFO also includes formatting objects specifically designed for XML; for example, fo:inline-link creates a hyperlink. It has no equivalent in CSS. This section is a very brief look at XSLFO because, at the time of this writing, XSLFO has not achieved significant market acceptance. The concepts, however, are very close to CSS. What’s Next Now that you know how to create and view XML documents, the next three chapters will take you one step further and teach you how to manipulate and create XML documents from a scripting or programming language. 189 What's Next OUTPUT 08 2429 CH06 2.29.2000 2:22 PM Page 189 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 09 2429 CH07 2.29.2000 2:22 PM Page 190 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 7 The Parser and DOM The previous chapters showed how to view and transform XML documents. Style sheet is a powerful technology but it is limited to viewing and trans- forming. When you have more specific needs, you need to turn to program- ming. This chapter introduces how to read XML documents from JavaScript or Java. In this chapter, you learn • what an XML parser is • how to interface a parser with an application • what DOM, the Document Object Model, is • how to write JavaScript applications that use DOM • how to write Java applications that use DOM • which other applications use DOM What Is a Parser? A parser is the most basic yet most important XML tool. Every XML appli- cation is based on a parser. A parser is a software component that sits between the application and the XML files. Its goal is to shield the developer from the intricacies of the XML syntax. Parsers are confusing because they have received a lot of publicity: There are dozens of parsers freely available on the Internet. When Microsoft shipped Internet Explorer 4.0 as the first browser with XML support, they bundled two XML parsers with it. Yet, if you ask for a demo of a parser, you won’t see much. The parser is a low-level tool that is almost invisible to everybody but programmers. The confusion arises because the tool that has so much visibility in the market- place turns out to be a very low-level device. 09 2429 CH07 2.29.2000 2:22 PM Page 191 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Parsers Why do you need parsers? Imagine you are given an XML file with product descriptions, including prices. Your job is to write an application to convert the dollar prices to Euros. It looks like a simple assignment: Loop through the price list and multiply each price by the exchange rate. Half a day’s work, including tests. Remember the prices are in an XML file. To loop through the prices means to read and interpret the XML syntax. It doesn’t look difficult—basically elements are in angle brackets. Let’s say the half-day assignment is now a one-day assignment. Do you remember entities? The XML syntax is not just about angle brack- ets. There might be entities in the price list. The application must read and interpret the DTD to be able to resolve entities. While it’s reading the DTD, it might as well read element definitions and validate the document. ✔ For more information on how the DTD influences the document, see the section “Standalone Documents” in Chapter 3 (page 79). What about other XML features: character encodings, namespaces, param- eter entities? And did you consider errors? How does your software recover from a missing closing tag? The XML syntax is simple. Yet, it’s an extensible syntax so XML applica- tions have to be ready to cope with many options. As it turns out, writing a software library to read XML files is a one-month assignment. If you were to write such a library, you would be writing your own parser. Is it productive to spend one month writing a parser library when you need only half a day’s work to process the data? Of course not. That’s why developers download a parser from the Internet or use the one that ships with the development tool. This is the common definition of a parser: off-the-shelf components that isolate programmers from the specifics of the XML syntax. If you are not convinced yet and if you’d rather write your own XML parser, consider this: No programmer in his/her right mind (except those working for Oracle, Sybase, Informix, and the like) would write low-level database drivers. It makes more sense to use the drivers that ship with the database. Likewise, no programmer should spend time decoding XML files—it makes more sense to turn to existing parsers. 192 Chapter 7: The Parser and DOM 09 2429 CH07 2.29.2000 2:22 PM Page 192 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. NOTE The word parser comes from compilers. In a compiler, a parser is the module that reads and interprets the programming language. In a compiler, the parser creates a parse tree, which is an in-memory representation of the source code. The second half of the compiler, known as the backend, uses parse trees to generate object files (compiled modules). Validating and Nonvalidating Parsers XML documents can be either well-formed or valid. Well-formed documents respect the syntactic rules. Valid documents not only respect the syntactic rules but also conform to a structure as described in a DTD. Likewise, there are validating and nonvalidating parsers. Both parsers enforce syntactic rules but only validating parsers know how to validate documents against their DTDs. Lest there be any confusion, there is no direct mapping between well- formed and nonvalidating parsers. Nonvalidating parsers can read valid documents but won’t validate them. To a nonvalidating parser, every docu- ment is a well-formed document. Similarly, a validating parser accepts well-formed documents. Of course, when working on well-formed documents, it behaves as a nonvalidating parser. As a programmer, you will like the combination of validating parsers and valid documents. The parser catches most of the structural errors for you. And you don’t have to write a single line of code to benefit from the service: The parser figures it out by reading the DTD. In short, it means less work for you. The Parser and the Application This section shows you how to integrate the parser in your applications. It discusses the various interfaces available to the programmer. The Architecture of an XML Program Figure 7.1 illustrates the architecture of XML programs. As you can see, it is divided into two parts: • The parser deals with the XML file. • The application consumes the content of the file through the parser. 193 The Parser and the Application 09 2429 CH07 2.29.2000 2:22 PM Page 193 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Figure 7.1: Architecture of an XML program Note that the application can be very simple (such as printing information on the screen), or quite complex (such as a browser or an editor). This chapter and the next one concentrate on the dotted line between the two elements. This is the interface, or the communication path, between the parser and the application. The parser and the application must share a common model for XML data. In practice, the common model is always some variation on a tree in mem- ory that matches the tree in the XML document. The parser reads the XML document and populates the tree in memory. This tree built by the parser is an exact match of the tree in the XML docu- ment. The application manipulates it as if it were the XML document. In fact, for the application, it is the XML document. Object-Based Interface There are two basic ways to interface a parser with an application: using object-based interfaces and using event-based interfaces. In practice, the two approaches are more complementary than competitive. Using an object-based interface, the parser explicitly builds a tree of objects that contains all the elements in the XML document. This is probably the most natural interface for the application because it is handed a tree in memory that exactly matches the file on disk. Obviously, it’s more convenient for the application to work with the tree in memory, if only because it doesn’t have to worry about the XML syntax. Furthermore, if using a validating parser, the tree may have been validated against the DTD. Listing 7.1 is a list of products, with their prices in U.S. dollars, presented in an XML document. The structure for this document is shown in Figure 7.2. 194 Chapter 7: The Parser and DOM EXAMPLE 09 2429 CH07 2.29.2000 2:22 PM Page 194 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... convert(form,xmldocument) { var fname = form.fname.value, output = form.output, rate = form.rate.value; output.value = “”; var document = parse(fname,xmldocument), topLevel = document.documentElement; searchPrice(topLevel,output,rate); } function parse(uri,xmldocument) { xmldocument.async = false; xmldocument.load(uri); if(xmldocument.parseError.errorCode != 0) alert(xmldocument.parseError.reason); return xmldocument;... convert(form,xmldocument) { var fname = form.fname.value, output = form.output, rate = form.rate.value; output.value = “”; var document = parse(fname,xmldocument), topLevel = document.documentElement; walkNode(topLevel,output,rate) } function parse(uri,xmldocument) { xmldocument.async = false; xmldocument.load(uri); if(xmldocument.parseError.errorCode != 0) alert(xmldocument.parseError.reason); return xmldocument;... loads the price list in the XML island and returns its Document object Most of the code in this function is Internet Explorerspecific because the DOM specification starts only at the Document object EXAMPLE function parse(uri,xmldocument) { xmldocument.async = false; xmldocument.load(uri); if(xmldocument.parseError.errorCode != 0) alert(xmldocument.parseError.reason); return xmldocument; } The function... convert(form,xmldocument) { var pricesfname = form.prices.value, ratesfname = form.rates.value; output = form.output, rates = new Array(3); output.value = “”; var document = parse(ratesfname,xmldocument), topLevel = document.documentElement; searchRate(topLevel,rates); document = parse(pricesfname,xmldocument); topLevel = document.documentElement; walkNode(topLevel,output,rates); } function parse(uri,xmldocument)... walkNode(topLevel,output,rates); } function parse(uri,xmldocument) { xmldocument.async = false; xmldocument.load(uri); Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark continues 09 2429 CH07 214 2.29.2000 2:23 PM Page 214 Chapter 7: The Parser and DOM Listing 7.8: continued if(xmldocument.parseError.errorCode != 0) alert(xmldocument.parseError.reason); return xmldocument; } function searchRate(node,rates)... NAME=”fname” VALUE=”prices .xml > Rate: It also defines a read-only text area that serves as output: Finally, it defines an XML island XML islands are mechanisms used to insert XML in HTML documents In this case, XML islands are used to access Internet Explorer’s XML parser The price... a DOM tree is Document Document inherits from Node so it can be inserted in a tree Document inherits most properties from Node and adds only two new properties: Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 09 2429 CH07 204 2.29.2000 2:23 PM Page 204 Chapter 7: The Parser and DOM • documentElement is the topmost element in the document • doctype is the Document Type DOM... Chapter 9, “Writing XML, ” looks at how to create XML documents Document Object Model Originally, the W3C developed DOM for browsers DOM grew out of an attempt to unify the object models of Netscape Navigator 3 and Internet Explorer 3 The DOM recommendation supports both XML and HTML documents The current recommendation is DOM level 1 Level 1 means that it fully specifies well-formed documents DOM level... applications that manipulate XML documents such as browsers, editors, XSL processors, and so on Event-based interfaces are geared toward applications that maintain their own data structure in a non -XML format For example, event-based interfaces are well adapted to applications that import XML documents in databases The format of the application is the database schema, not the XML schema These applications... interfaces is DOM, Document Object Model, published by the W3C (www.w3.org/TR/REC-DOM-Level-1) The standard for event-based interface is SAX, Simple API, developed collaboratively by the members of the XML- DEV mailing list and edited by David Megginson (www.megginson.com/SAX) The two standards are not really in opposition because they serve different needs Many parsers, such as IBM’s XML for Java and . the Document object. function parse(uri,xmldocument) { xmldocument.async = false; xmldocument.load(uri); if(xmldocument.parseError.errorCode != 0) alert(xmldocument.parseError.reason);. document.documentElement; searchPrice(topLevel,output,rate); } function parse(uri,xmldocument) { xmldocument.async = false; xmldocument.load(uri); if(xmldocument.parseError.errorCode