Unformatted text preview:

Chapter 7 - Transformation - XSLT Learning XML by Erik T. RayXSLTXSLT ConceptsXSLT Concepts (cont.)XSLT OutputNamespacesTemplatesMatching NodesRule ConflictsRules (cont.)apply-templates instructionfor-each instructionOutputNamed TemplatesProcessing InstructionsSortingExamplesCombining StylesheetsCombining Stylesheets (cont.)ModesAugust 2006 1Chapter 7 - Transformation - XSLT Learning XMLbyErik T. RaySlides were developed by Jack DavisCollege of Information Scienceand TechnologyRadford UniversityAugust 2006 2XSLT•Transformation is one of the most important and useful techniques for working with XML•Transforming an XML document can be used in many ways -- changing a non-presentational document into a form that can be displayed- changing one tag set into another- extracting specific pieces of information and formatting them in a different way- changing an XML document into text, i.e. - transforming an XML data file into a comma delimited format for Excel- reformatting or generating new content- many, many, moreAugust 2006 3XSLT Concepts•An XSLT processor takes two inputs- an XSLT stylesheet to govern the transformation- an input document (source tree)•XSLT processor generates one output- the result tree (usually a document)•The XSLT stylesheet controls the transformation process. XSLT is really a script or program, not just a stylesheet•XSLT processor is a state engine, at any point in time it has a state. There are rules to drive processing forward based on the state. - The state consists of a set of nodes and the process is recursive -- meaning that for each node processed there may be children that also need processing. (The current node may be set aside until child processing is complete.)August 2006 4XSLT Concepts (cont.)•An XSLT engine begins by reading the XSLT stylesheet and caching it as a lookup table. For each node in the source it will look in the table for the best matching rule. The rule specifies what to put in the output tree and also how to continue. •Starting from the root node, the rules are found, nodes processed and results put in the output tree. This process continues until all input nodes are processed. •Consider the following XML document excerpt- example 7-01•Suppose we want to transform the XML document to an html document- XSL stylesheetA template is a mixture of markup, text content, and XSLT instructions. The instructions may be conditional statements, content formatting functions, or instructions to redirect processing to other nodes.August 2006 5XSLT Output•The output of the XSLT transformation process- html document•The elements in the source tree have been mapped to different elements in the result tree. •If the transformation will be done by the web server or client, you must include a reference to the stylesheet in the document as a processing instruction, similar to the one used to associate documents with CSS stylesheets.<?xml-stylesheet type="text/xml" href="mytrans.xsl"?>•the type attribute for IE 6 must read as follows: type="text/xsl"August 2006 6Namespaces•XSLT can be extended by the implementer to perform special functions not contained in the specification. For example, you can add a feature to redirect output to multiple files. These extensions are identified by a separate namespace that you must declare if you want to use them. And, just to make things clear for the XSLT engine, you should set the attribute extension-element-prefixes to contain the namespace prefixes of extensions.the element below declares namespaces for XSLT control elements (prefix XSL) and implementation-specific elements (prefix ext). Finally, it specifies the version 1.0 of XSLT in the last attribute.<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ext="http://www.myxslt.org/extentions" extension-element-prefixes="ext" version="1.0" >August 2006 7Templates•XSLT stylesheets can be a collection of templates. Each template associates a condition (an element in the source tree with a particular attribute) with a mixture of output data and instructions. Templates are matched to nodes based on priority, typically the most specific match will have a higher priority than a more general match. For example, one template may match all elements with the XPath expression *. Another may match a specific element, while a third matches that element and further requires an attribute. Templates can also have a priority specified using a priority attribute.Templates are compact pieces of code that are easy to read and manage. The match and priority attributes show exactly when each template is to be used.August 2006 8Matching Nodes•XSLT patterns used inside the match attributes of template elements are a subset of XPath expressions. The first restriction on XSLT patterns is that only descending axes may be used: child and attribute. Paths are actually evaluated right to left, not the other direction as is usual with XPath. As the processor moves through the source tree, it keeps a running list of nodes to process next, called the context node set. Each node in this set is processed in turn. The processor looks at the set of rules in the stylesheet, finds a few that apply to the node to be processed, and out of this set selects the best matching rule. The right-to-left processing helps the XSLT engine prioritize eligible templates.August 2006 9Rule Conflicts•More than one rule may match a node, in this case, the XSLT processor must select one rule from the possible rules. The basic assumption is that rules that are more specific in their application take precedence over rules that are more general. The rules for selecting the best match are:- if the pattern contains multiple alternatives specified with the or ( | ) bar, each is treated with equal importance.- a pattern that contains specific hierarchical information has higher priority than a pattern that contains general information.- a wildcard is more general that a specific element or attribute name and therefore has lower priority. - a pattern with a successful test expression in square brackets ([]) overrides a pattern with no test expression.- other information such as position in the stylesheet may be considered if these rules don't establish a priorityAugust 2006 10Rules (cont.)•The xsl:template element has an optional priority attribute that can be set to give it precedence over other rules and


View Full Document

Radford ITEC 325 - Lecture Note

Documents in this Course
Load more
Download Lecture Note
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Note and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Note 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?