This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
NOTE: All the elements in angle braces (<>, should be xsl prefixed, means - "xsl colon elementName", I had to remove "xsl colon" part as I was not able to post this message, becuase this part gets converted to some image. So please insert this fragment befoe each element name that is in angle brackets. XSLT Transformation is a process of taking an XML document and turning it into a document in another form The process of applying rules to the source document is called transformation XSLT is a declarative language Programmer has control over the general course of transformation but not on the step-by-step processing of the XSLT processor XSLT stylesheet consists of a series of templates A template consists of an expression describing the form of some XML fragment and one or more elements specifying what output to generate when the form is matched in the source document. Each stylesheet contains a template that matches the root of a document – root template. The XSLT processor begins from this template XSLT works its way through the structures in the source document and the context is the node of the parse tree on which the processor if focussing An XSLT processor will start from the document element (root node in the tree) but the context changes throughout the course of processing Node tests: comment(), text(), node(), processing-instruction() XSLT has 37 elements and attributes Commonly used elements are categorized as 1. Building stylesheets 2. Controlling transformation output 3. Generating simple output 4. Generating XML 5. Iteration 6. Conditional tests 7. Programming constructs 8. Modularity Building stylesheets: 1. <stylesheet> or <transform> - This is the top-most element. This element must have two attributes - namespace and version 2. <template> - This element has four optional attributes – match, mode, priority, name. 3. <apply-templates> - Two child elements of this element are - <sort> and <with-param>. If this element is used with no attributes, it tells the processor to match the templates in the stylesheet against the current context node. Controlling transformation output 1. <preserve-space> attribute is – “elements” 2. <strip-space> attribute is – “elements” 3. <output> This element has 10 optional attributes – method, version, encoding, standalone, indent, omit-xml-declaration, doctype-public, doctype-system, media-type, cdata-section-elements <output> is an empty element and is the child of the parent element - <stylesheet> Generating simple output- 1. <value-of> element. This element has an attribute – “select” (required attribute). Value of the “select” attribute is an XPath expression. If the expression returns a collection of nodes, only first node is returned to the output. 2. Making copies: <copy> and <copy-of> elements. <copy> element copies the current context node to the output. It does not bring along any attributes or child nodes. <copy-of> element copies current context node and all the nodes under it. Generating XML constructs directly: Five elements for creating major constructs of XML are: <element> <attribute> <comment> <text> <processing-instruction> Iteration: <for-each> element. This element operates on a collection of nodes designated by the value of its ‘select’ attribute. While the element is executed, the nodes in the collection become the current context node. Conditional tests: <if> element. This element does not have any ‘else’ part. This element takes one attribute – ‘test’ which is an XPAth expression <choose> element. This element has two child elements: <when> and <otherwise> <choose> has no attributes. <otherwise> is the default part of the choose statement. Programming constructs: 1. Declaring Variables: <variable> element This element has one required attribute – “name” It has one optional attribute – “select” (value of this attribute is an XPath expression) An XSLT variable cannot have its value changed (XSLT is free of sede-effects) Variables can be local or global 2. Calling templates: A named template uses <param> element to declare parameters and optionally give them the default values It is not an error to call a template without the required parameters, although processing may be affected Parameters can be local or global Templates can be called from <call-template> or <apply-templates> elements Modularity: <include> and <import> elements These elements have one required attribute: ‘href’ Both are child elements of <stylesheet> Both are empty elements Stylesheet included by <include> has same priority as native template Templates imported by <import> have lower priority To apply both native and imported templates, use <apply-imports/> statement Some things that CSS can’t do: 1. CSS can’t do computations 2. CSS can’t change the order of elements 3. CSS can’t combine multiple documents XSLT 1. XSLT stylesheet should be an XML document 2. XSLT is based on pattern-matching 3. XSLT is free of side effects 4. XSLT uses iteration and recursion instead of loops Scenarios where XSLT is preferable: 1. Information is to be rendered on different devices (web, handheld-devices etc) 2. To exchange data between different partners who use different database systems 3. When redesign of the site (say) is done very frequently 4. To combine documents that are in different formats XSLT stylesheets use namespace prefixes to process the XML elements they contain. Anything that doesn’t use the xsl namespace prefix is not processed, its written to the result tree. DOM, XSLT and XPath all use tree structures to represent data from an XML document. Stylesheet is itself an XML document The nodes to be processed are represented by context. Initially the context is the root of the XML document but it changes throughout the stylesheet Process of transformation is: 1. XSLT stylesheet is parsed and converted into a tree structure 2. XML document is parsed and converted into a tree structure 3. XSLT processor is now at the root of the XML document 4. <template> matches the document root 5. Process begins inside xsl:template where the element is matched with the value of ‘match’ attribute Built-in templates have lower priority than any other templates. Following are built-in templates: 1. for element and root nodes - <template match=”*|/”> 2. for modes - <template match=”*|/” mode=”x”> 3. for text and attribute nodes - <template match=”text()|@*”> 4. for comment and processing-instruction nodes – <template match=”comment()|processing-instruction()”/> This doesn’t do anything 5. for namespace nodes - <template match=”namespace()”/> This doesn’t do anything. Top level elements – elements whose parent is <stylesheet> element 1. <include> and <import> 2. <preserve-space> and <strip-space> 3. <output> 4. <param> 5. <variable> 6. <Key> 7. Other elements are: <decimal-format>, <namespace-alias> and <attribute-set> <import> can appear only at the beginning of a stylesheet, while <include> can appear anywhere XSLT uses XPath expression in the ‘match’ and ‘select’ attributes XPath works with parsed version of the XML document, means all the entities are resolved first. XSLT processor handles all <sort> elements before it does anything and then it processes <for-each> element Attributes of <sort> element are: 1. Select 2. Data-type : It can be text, number or QName 3. Order : It can be ascending, descending 4. Case-order : It can be upper-first, lower-first. This attribute is used only when data-type=”text” 5. lang <sort> element can be used within <for-each> or <apply-templates> elements.