|
Hands-on XSL--XSL for fun and diversion广告 Hands-on XSL
Don R. Day This article presents a simple, hands-on exercise that demonstrates the principles of the Extensible Stylesheet Language Transformations (XSLT). It takes about an hour to complete the concept exercises and about 15 minutes at a computer to try out the results with a real XSLT processor. XSL is a style sheet language for documents marked up using XML, the Extensible Markup Language, which can be thought of as "SGML for the Web." XSLT is used to describe how an XML source document is transformed into another XML document that uses the XSL formatting vocabulary. However, XSLT may be used for other general transforms as well. This exercise will deal with one of the most practical tasks for XSLT, transforming an XML document into an HTML document that can be viewed by any Web browser. For your information, this exercise demonstrates section 5.1 of the "XSLT Recommendation, Processing Model" (see Resources). Don't expect to understand this paragraph right now, but come back after completing the exercise to see if this quote from the spec makes more sense. "A list of source nodes is processed to create a result tree
fragment. The result tree is constructed by processing a list containing just
the root node. A list of source nodes is processed by appending the result tree
structure created by processing each of the members of the list in order. A node
is processed by finding all the template rules with patterns that match the
node, and choosing the best amongst them; the chosen rule's template is then
instantiated with the node as the current node and with the list of source nodes
as the current node list. A template typically contains instructions that select
an additional list of source nodes for processing. The process of matching,
instantiation and selection is continued recursively until no new source nodes
are selected for processing." It is recursive. Throughout this exercise, Glossary terms link to their definitions. Sample XML document <chapter
id="cmds"> Note that the first element, <chapter>, surrounds the entire document. Therefore, it is the document element of this document instance. It is also the child of a root node that is defined to always be the top node of any parsed XML document. Concept: Source document viewed as a
tree Preparation for the exercise
Fold and tear one sheet of white typing paper into eighths. (This will be more than enough pieces for the exercise.) On one side of each piece, write the text portion that corresponds to each element or attribute. These pages represent the text or character content of a source document. Here is how you would prepare the text pages for the content of the paragraph node, according to the parse tree view (refer to the Concept: Source document viewed as a tree illustration):
<xsl:stylesheet version="1.0" Label the backside with: </xsl:stylesheet> Concept: Namespaces Define the templates The leaflet itself is a container for the templates that match child elements, as in the <chaptitle> and <para> children of the <chapter> element. This represents the recursive aspect of XSL processing: starting from the root node of a source tree, each node is inspected to see if there is an applicable template rule; if so, that rule is invoked and then the inspection continues on the children of the new node. Whenever you intend to process the child nodes of an element, you are applying the templates that exist for those elements. Hence, this recursive action is called apply-templates. If you want to skip the processing of an element's child nodes or its content, you simply leave out the call to apply-templates. For example, there is not much point to having such a call for the template of an empty element -- it has no child element nodes. Similarly, if an element contains an attribute that indicates it has confidential information, you may filter out that content by simply not referencing it with the apply-templates call. Refer to the Sample XML document as you follow this exercise to create a result tree. As an extra measure of keeping the result tree distinct from the source tree, write all the markup for the result tree in upper case (for example, using <B> instead of <b>). Step 1 Step 2 <HEAD> Because we want to process the rest of the document recursively, again write "apply-templates" across the middle of the inside. On the backside write: </BODY> This completes the second template rule. Step 3 Here are some suggested things to do with the rest of the template rules: id: If you have started a template rule for this item, put it
aside for now. Not everything in a source document needs to be transformed into
something in the resulting document. Attributes usually represent a property
rather than a structure, and for the sake of this lesson, we are dealing only
with one-to-one structural transformations. We are now ready to begin our transformation on the source document. Concept: The XSL processing sequence The picture in Concept: Source document viewed as a tree represents the XML source document that has been parsed into an internal structure of nodes and properties. The XSL processor retrieves, parses, and organizes the template rules in the style sheet. Take the template rules leaflets out of their container and lay them out so you can see their labels; this represents creating the rules base part of the previous diagram. Next, you will model the apply-templates part of the process. To begin applying the templates, locate and pick up the template rule for the root node. Open it and note that inside is the instruction apply-templates. This means you must go to the next node of the tree, chapter, and see if there is a template rule that matches it. Did you find it? Okay, pick up that leaflet and tuck it inside the first leaflet. This represents that we are recursively building a result tree based on the structure of the source tree. For each node in the source tree, keep doing the same thing. Note that there is an apply-templates instruction for the chapter template rule, so go to the next node in sequence, chaptitle. Find the leaflet for that template rule and tuck it inside the previous template, then check its inside. Again, it says that we are to apply-templates, so get the next child of chaptitle, which in this case will be the white text leaf that says "FileCab." Tuck this page inside the chaptitle template. According to the source tree illustration, you should be at the end of that first branch of the source tree. There are no more children to find, and no more rules that apply in this location. Therefore, follow the tree back up to the next sibling element of chaptitle, which is the para element. Find its template, and continue this same process. Processing the para template rule should result in this particular assembly of templates and text, corresponding to the source tree branch for the para node.
Feel free to disassemble your booklet and repeat the steps to assemble the result tree view a few times. These steps are the main point of this tutorial and should become imprinted in your mind. Generate your result HTML Voil? You have just completed an XSL transformation, exactly the way it happens for real. No way, you say? See what's next! Convert your paper template rules into an equivalent XSL style
sheet Separate into a pile the colored, folded pieces of paper that you have labeled. Each of these is a template rule that has a match pattern and a corresponding template for the resulting transformation. Start writing your XSL style sheet by copying down the label you put on your container for the templates: <xsl:stylesheet version="1.0" Now you will do the following for each colored leaflet in front of you. Start with the template rule for the root node, and repeat the process for the rest of the leaflets. Pick up a leaflet. Use the following text as the template for
what you write on the style sheet. Words that are bracketed by underscore
characters represent things on your template rule that you will copy into this
new format. <xsl:template match="__label__"> Copy the leaflet's label into the
designated __label__ field of the match attribute. (Hint for the root template:
instead of the name root, XSL actually uses the shorthand "/" (just as in a
filepath) to indicate the root.) Do the same for each of the rest of the colored leaflets before
you. Remember that the white pages were part of the source document, not part of
the transformation rules. Only transcribe the information you put on the
colored, folded leaflets. </xsl:stylesheet> Congratulations on having written a conforming, functional XSL style sheet! Try it out Copy the sample source document into a file called test.xml.
Save it in an obviously-named directory (for example, c:\testxml). After a moment, the process should finish and the directory
should contain the expected result file, myresult.htm. Resources
Glossary attribute.A property of an element. Attributes are often used to attach such information as style, security level, intent or role, or an indication of data type. child.A node within the scope of another node. current node.The node that has been matched by a particular template rule. The context around the current node includes itself, its ancestors (parent, grandparent, and so on), siblings, and children. document element.The outermost element of an XML document or fragment. element.The structural part of an XML document. Elements have a start tag, content, and an end tag. The start tag of an element may have additional properties, called attributes, that apply to the entire element. generated text.Data that is inserted into a transformed node of a document. In books with chapters, the word Chapter is usually generated as part of the printed chapter title. match pattern.The name of an element, an attribute, or other valid XPath target in a source document. node.An element, attribute, or text item in a document. These represent parts of the structure that have nested relationships with each other, and this nesting may be represented graphically as nodes in a tree view of the structure. parent.The node that contains the current node. parsing.The processing that separates the markup of a document (including tag names and the syntax, or "<" and ">" characters) from its content (usually text and images). recursion.In a document tree structure, applying a process to each node of the tree by repeatedly calling the same process until all children and grandchildren of the node have been processed. The alternative, and the way most people think about documents, is sequential processing. result tree.The representation of a document after it has been transformed from its source structure. The syntax for the desired result format is applied when the result tree is written to its file format by the XSL processing application. root element.The implicit highest-level node of a parsed XML document. You may not always be able to predict which element will be the document element of a parsed instance, but it will always have a root node that you can count on being able to use for preliminary or setup processing. root node.The starting point of all parsed documents. After the root node comes the document element, which may be different between any two documents. Because all parsed documents have a root node, this is a reliable node to use in a transform. sibling.One of several nodes within the scope of another node. source tree.The
representation of a document after it has been parsed. Any XML syntax (the
"<" and ">" delimiters) has been removed. The tree structure of a document
is often diagrammed like this: stream.Data that is in the process of being transmitted from one operation to the next. An example of streaming media is an audio file that is played on a Web browser even as the data is coming from a server. template.The replacement markup or content to be copied into the result tree. template rule.A style sheet is composed of a set of template rules. Each template rule has a match pattern that associates it to an element or node in the source document. The template rule contains the template of either markup or generated text that is to be associated with the result instance of the rule. A template may optionally include either a recursive call for the processing of child nodes of this node (<xsl:apply-templates/>), a call to a different, named template rule, or a query for specific values of other nodes in the source tree. walking a tree.To travel downward along the left side of all nodes until you reach the end of a branch, then come up to the next new branch, work it to the end, back to the next branch, and so on. Walking this tree will result in this sequence of nodes visited: root: well-formed.An XML document
(sometimes called an instance) that meets certain criteria. For example,
the document always has start- and end-tag markup for each document element (as
in About the author 如果您希望与本文章的作者或其所在机构,进一步交流,请联系:畅享网 姜小姐 jill.jiang@amteam.org | 021-51096826-112 | 在线联系 |
节能与优化IT 企业CIO过冬良策当前金融危机的影响还在继续漫延,很多企业都在苦寻过冬的良策,在这种情况下,节能与优化技术与产品无疑成为CIO们关注的首要对象,本次选题就是针对节能与优化IT来为CIO们提供过冬的良…… 观08软件并购风潮 议09巨头何处生花2008,似乎注定是不平静的一年。有人说2008是并购年。业内人士表示,在全球软件行业,并购一直是大企业谋求做大做强的捷径之一,包括甲骨文、SAP,微软等全球软件巨头都为了扩大自己…… |
|
|