Tài liệu XML by Example- P4 pdf

50 444 0
Tài liệu XML by Example- P4 pdf

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Paths The syntax for XML paths is similar to file paths. XML paths start from the root of the document and list elements along the way. Elements are separated by the “ / ” character. The root of the document is “ / ”. The root is a node that sits before the top- level element. It represents the document as a whole. The following four paths match respectively the title of the article ( <title>XML Style Sheets</title> ), the keywords of the article, the top- most article element, and all sections in the article. Note that the last path matches several elements in the source tree. /article/title /article/keywords /article /article/section TIP Note that “ / ” points to the immediate children of a node. Therefore, /article/title selects the main title of the article (XML Style Sheets), not all the titles below the arti- cle element. It won’t select the section titles. To select all the descendants from a node, use the “ // ” sequence. /article//title selects all the titles in the article. It selects the main title and the section titles. In the style sheet, most paths don’t start at the root. XSL has the notion of current element. Paths in the match attribute can be relative to the current element. Again, this is similar to the file system. Double-clicking the accessories folder in the c:\program files folder moves to c:\program files\ accessories folder, not to c:\accessories . If the current element is an article, then title matches /article/title but if the current article is a section, title matches one of the /article/ section/title s. To match any element, use the wildcard character “ * ”. The path /article/* matches any direct descendant from article, such as title, keywords, and so on. It is possible to combine paths in a match with the “ | ” character, such as title | p matches title or p elements. 135 Basic XSLT EXAMPLE 07 2429 CH05 2.29.2000 2:21 PM Page 135 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Matching on Attributes Paths can match on attributes, too. The following template applies only to “mailto” URLs: <xsl:template match=”url[@protocol=’mailto’]”> <A> <xsl:attribute name=”HREF”>mailto:<xsl:apply-templates/> </xsl:attribute> <xsl:apply-templates/> </A> </xsl:template> <A href=”mailto:bmarchal@pineapplesoft.com”> ➥bmarchal@pineapplesoft.com</A> It matches <url protocol=”mailto”>bmarchal@pineapplesoft.com</url> that has a protocol attribute with the value “mailto” but it does not match <url>http://www.w3.org/Style</url> . The more generic url path would match the later element. url[@protocol] matches URL elements that have a protocol attribute, no matter what its value is. It would match the <url protocol=”http”>www.w3.org/Style</url> but it would not match <url>http://www.w3.org/Style</url> . Matching Text and Functions Functions restrict paths to specific elements. The following two paths are identical and select the text of the title of the second section in the docu- ment ( Styling ). /article/section[position()=2]/title/text() /article/section[2]/title/text() Most functions can also take a path as an argument. For example, count(//title) returns the number of title elements in the document. Table 5.1 lists some of the most common functions. Table 5.1: Most common XSL functions XSL Function Description position() returns the position of the current node in the node set text() returns the text (the content) of an element 136 Chapter 5: XSL Transformation EXAMPLE OUTPUT OUTPUT 07 2429 CH05 2.29.2000 2:21 PM Page 136 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. last() returns the position of the last node in the current node set count() returns the number of nodes in the current node set not() negates the argument contains() returns true if the first argument contains the second argument starts-with() returns true if the first argument starts with the second argu- ment New functions are declared in JavaScript, Java, C++, and so on, with the xsl:functions element. <xsl:template match=”/”> <xsl:value-of select=”psol:today()”/> </xsl:template> <xsl:functions ns=”psol” type=”text/javascript”> function today() { return Date().toString() } </xsl:functions> CAUTION Be aware that this element was still very much in flux in the draft we used to prepare this chapter. Deeper in the Tree After loading the style sheet, the XSL processor loads the source document. Next, it walks through the source document from root to leaf nodes. At each step it attempts to match the current node against a template. If there is a match, the processor generates the nodes in the resulting tree. When it encounters xsl:apply-templates , it moves to the children of the current node and repeats the process; that is, it attempts to match them against a template. In other words, xsl:apply-templates is a recursive call to the style sheet. A recursive approach is natural to manipulate trees. You might have recog- nized a deep-first search algorithm. Figure 5.6 illustrates how it works. 137 Basic XSLT XSL Function Description EXAMPLE 07 2429 CH05 2.29.2000 2:21 PM Page 137 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Figure 5.6: Walking down the input tree Following the Processor Let’s follow the XSL processor for the first few templates in the style sheet. After loading the style sheet and the source document the processor posi- tions itself at the root of the source document. It looks for a template that matches the root and it immediately finds <xsl:template match=”/”> <HTML> <HEAD> <TITLE>Pineapplesoft Link</TITLE> </HEAD> <BODY> <xsl:apply-templates/> </BODY> </HTML> </xsl:template> Because the root sits before the top-level element, it is ideal to create the top-level element of the resulting tree. For HTML, it is the HTML element with HEAD and BODY elements. When it encounters xsl:appy-templates , the processor moves to the first child of the current node. The first child of the root is the top-level element or the article element. The style sheet defines no templates for the article but can match template against a built-in template. Built-in templates are not defined in the style sheet. They are predefined by the processor. 138 Chapter 5: XSL Transformation 07 2429 CH05 2.29.2000 2:21 PM Page 138 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. <xsl:template match=”* | /”> <xsl:apply-templates/> </xsl:template> NOTE The built-in template does not modify the resulting tree (it does not create elements) but it recursively calls the current element’s children. Without the default template, there would be no rules to trigger the recursive matching process and the processor would stop. It is possible to override the built-in template, for example, to stop processing for ele- ments not explicitly defined elsewhere: <xsl:template match=”* | /”/> The built-in template forces the processor to load the first children of article, that is, the title element. The following template matches <xsl:template match=”article/title”> <P><B><xsl:apply-templates/></B></P> </xsl:template> Note that the processor matches on a relative path because the current node is article. It creates a paragraph in the HTML document. xsl:apply- templates loads title’s children. The first and only child of title is a text node. The style sheet has no rule to match text but there is another built-in template that copies the text in the resulting tree. <xsl:template match=”text()”> <xsl:value-of select=”.”/> </xsl:template> The title’s text has no children so the processor cannot go to the next level. It backtracks to the article element and moves to the next child: the date element. This element matches the last template. <xsl:template match=”abstract | date | keywords | copyright”/> This template generates no output in the resulting tree and stops process- ing for the current element. The processor backtracks again to article and processes its other children: copyright, abstract, keywords, and section. Copyright, abstract, and key- words match the same rule as abstract and generate no output in the resulting tree. 139 Basic XSLT 07 2429 CH05 2.29.2000 2:21 PM Page 139 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. The section element, however, matches the default template and the proces- sor moves to its children, title, and p elements. The processor continues to match rules with nodes until it has exhausted all the nodes in the original document. Creating Nodes in the Resulting Tree Sometimes it is useful to compute the value or the name of new nodes. The following template creates an HTML anchor element that points to the URL. The anchor has two attributes. The first one, TARGET , is specified directly in the template. However, the processor computes the second attribute, HREF , when it applies the rule. <xsl:template match=”url”> <A TARGET=”_blank”> <xsl:attribute name=”HREF”> <xsl:apply-templates/> </xsl:attribute> <xsl:apply-templates/> </A> </xsl:template> <A target=”_blank” href=”http://www.w3.org/Style”> ➥http://www.w3.org/Style</A> Table 5.2 lists other XSL elements that compute nodes in the resulting tree. Table 5.2: XSL elements to create new objects XSL Element Description xsl:element creates element with a computed name xsl:attribute creates attribute with a computed value xsl:attribute-set conveniently combines several xsl:attributes xsl:text creates a text node xsl:processing-instruction creates a processing instruction xsl:comment creates a comment xsl:copy copies the current node xsl:value-of computes text by extracting from the source tree or inserting a variable xsl:if instantiates its content if the expression is true xsl:choose selects elements to instantiate among possible alternatives xsl:number creates formatted number 140 Chapter 5: XSL Transformation EXAMPLE OUTPUT 07 2429 CH05 2.29.2000 2:21 PM Page 140 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. P RIORITY There are rules to prioritize templates. Without going into too many details, templates with more specific paths take precedence over less specific tem- plates. In the following example, the first template has a higher priority than the second template because it matches an element with a specific attribute. <xsl:template match=”url[@protocol=’mailto’]”> <A> <xsl:attribute name=”HREF”>mailto:<xsl:apply-templates/> </xsl:attribute> <xsl:apply-templates/> </A> </xsl:template> <xsl:template match=”url”> <A TARGET=”_blank”> <xsl:attribute name=”HREF”> <xsl:apply-templates/> </xsl:attribute> <xsl:apply-templates/> </A> </xsl:template> If there is a conflict between two templates of equivalent priority, then the XSL processor can either report an error or choose the template that appears last in the style sheet. Supporting a Different Medium Recall that my original problem is to provide both an HTML and a text ver- sion of the document. We have seen how to automatically create an HTML version document, now it’s time to look at the text version. Text Conversion CAUTION Text conversion stretches the concept of XML to XML conversion; therefore, you have to be careful in writing the style sheet. Listing 5.4 is the text style sheet. It is very similar to the previous style sheet except that it inserts only text nodes, no XML elements, in the resulting tree. 141 Supporting a Different Medium EXAMPLE EXAMPLE 07 2429 CH05 2.29.2000 2:21 PM Page 141 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Listing 5.4: A Style Sheet to Produce a Text File <?xml version=”1.0” encoding=”ISO-8859-1”?> <xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform/”> <xsl:output method=”text”/> <xsl:template match=”article/title”> <xsl:text>=== </xsl:text> <xsl:apply-templates/> <xsl:text> ===</xsl:text> </xsl:template> <xsl:template match=”section/title”> <xsl:text>*** </xsl:text> <xsl:apply-templates/> <xsl:text> ***</xsl:text> </xsl:template> <xsl:template match=”url”> <xsl:text>[</xsl:text> <xsl:apply-templates/> <xsl:text>]</xsl:text> </xsl:template> <xsl:template match=”p”> <xsl:text> </xsl:text> <xsl:apply-templates/> </xsl:template> <xsl:template match=”abstract | date | keywords | copyright”/> </xsl:stylesheet> Logically enough, the xsl:stylesheet element does not declare a name- space for the resulting tree. This style sheet also makes heavy use of text nodes. 142 Chapter 5: XSL Transformation 07 2429 CH05 2.29.2000 2:21 PM Page 142 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. <xsl:template match=”section/title”> <xsl:text>*** </xsl:text> <xsl:apply-templates/> <xsl:text> ***</xsl:text> </xsl:template> The following command line creates the text in Listing 5.5. java –classpath ➥c:\lotusxsl\xerces.jar;c:\lotusxs\lotusxsl.jar ➥com.lotus.xsl.Process ➥-in 19990101_xsl.xml ➥-xsl email.xsl -out 19990101_xsl.txt Listing 5.5: The Resulting Text Document === XML Style Sheets === Send comments and suggestions to <bmarchal@pineapplesoft.com>. *** Styling *** Style sheets are inherited from SGML, an XML ancestor. Style sheets originated ➥in publishing and document management applications. XSL is XML’s standard style ➥sheet, see [http://www.w3.org/Style]. *** How XSL Works *** An XSL style sheet is a set of rules where each rule specifies how to format ➥certain elements in the document. To continue the example from the previous ➥section, the style sheets have rules for title, paragraphs and keywords. With XSL, these rules are powerful enough not only to format the document ➥but also to reorganize it, e.g. by moving the title to the front page or ➥extracting the list of keywords. This can lead to exciting applications of XSL ➥outside the realm of traditional publishing. For example, XSL can be used to ➥convert documents between the company-specific markup and a standard one. *** The Added Flexibility of Style Sheets *** Style sheets are separated from documents. Therefore one document can have more ➥than one style sheet and, conversely, one style sheet can be shared amongst ➥several documents. This means that a document can be rendered differently depending on the media or ➥the audience. For example, a “managerial” style sheet may present a summary ➥view of a document that highlights key elements but a “clerical” style sheet ➥may display more detailed information. 143 Supporting a Different Medium OUTPUT 07 2429 CH05 2.29.2000 2:21 PM Page 143 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Customized Views Currently, most people access the Web through a browser on a Windows PC. Some people use Macintoshes, others use UNIX workstations. This will change in the future as more people turn to specialized devices. Already WebTV has achieved some success with a browser in a TV set. Mobile phones and PDAs, such as the popular PalmPilot, will be increas- ingly used for Web browsing. Ever tried surfing on a PalmPilot? It works surprisingly well but, on the small screen, many Web sites are not readable enough. One solution to address the specific needs of smaller devices might be to use XHTML, an XML simplified version of HTML. XHTML is based on HTML but it has an XML syntax (as opposed to an SGML syntax). It is also designed to be modular as it is expected smaller devices will implement only a subset of the recommendation. According to the W3C, these new platforms might account for up to 75% of Web viewing by the year 2002. What can you do about it? Will you have to maintain several versions of your Web site: one for existing Web browsers and one for each new device with its own subset? XSL to the rescue! It will be easy to manage the diversity of browsers and platforms by maintaining the document source in XML and by converting to the appropriate XHTML subset with XSLT. In essence, this is how I manage the e-zine. Figure 5.7 illustrates how this works. 144 Chapter 5: XSL Transformation Figure 5.7: Maintain one XML document and convert it to the appropriate markup language. 07 2429 CH05 2.29.2000 2:21 PM Page 144 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... (%files%) do %xslprocessor% -in %%0 .xml -out %%0.xtr Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 07 2429 CH05 2.29.2000 2:21 PM Page 157 157 Using XSLT to Extract Information -xsl extract.xsl copy opening.tag index .xml for %%0 in (%files%) do copy index .xml /a + %%0.xtr ➥/a index .xml /a copy index .xml + closing.tag index .xml TIP Don’t pass an xml, html, or text argument on... Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 08 2429 CH06 2.29.2000 2:22 PM Page 160 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 08 2429 CH06 2.29.2000 2:22 PM Page 161 6 XSL Formatting Objects and Cascading Style Sheet The previous chapter was a first look at styling XML documents You learned how to use XSLT to convert XML documents to... 5.9: Internet Explorer 5.0 renders XML Changes to the Style Sheet The style sheet has been adapted in two places First, the XSL namespace points to an earlier version of XSL Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 07 2429 CH05 2.29.2000 2:21 PM Page 149 149 Advanced XSLT ... extract information from XML documents A N E W S TA N D A R D The W3C works on a new standard XQL, the XML Query Language, that will offer a better solution to this problem XQL can query multiple documents stored in an XML database XQL will use paths similar or identical to XSLT so it will be familiar Because it works across several documents, XQL is really designed for XML databases XML databases store... Document Prepared for Internet Explorer 5.0 EXAMPLE < ?xml version=”1.0”?> < ?xml- stylesheet href=”simple-ie5.xsl” type=”text/xsl”?> Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark continues 07 2429 CH05 146 2.29.2000 2:21 PM Page 146 Chapter 5: XSL Transformation Listing 5.6: continued XML Style Sheets January 1999 1999,... convert index .xml in HTML Figure 5.12 shows the result in a browser Listing 5.14: Styling the Index EXAMPLE < ?xml version=”1.0” encoding=”ISO-8859-1”?> Pineapplesoft Link: Archives Please purchase PDF Split-Merge... shows the result in Internet Explorer Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 07 2429 CH05 2.29.2000 2:21 PM Page 147 Where to Apply the Style Sheet 147 Listing 5.7: XSLT Style Sheet for Internet Explorer 5.0 < ?xml version=”1.0” encoding=”ISO-8859-1”?> Listing 5.6 is the XML document with the appropriate processing instruction for Internet Explorer 5.0 Listing 5.6: The XML Document Prepared for . Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. <xsl:stylesheet xmlns:xsl=”http://www.w3.org/TR/WD-xsl” xmlns=”http://www.w3.org/TR/REC-html40”. predefined by the processor. 138 Chapter 5: XSL Transformation 07 2429 CH05 2.29.2000 2:21 PM Page 138 Please purchase PDF Split-Merge on www.verypdf.com

Ngày đăng: 14/12/2013, 18:15

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan