Programming and Coding
What is the difference between XSL and XSLT?
All XSLT stylesheets are themselves XML documents, so all the rules of XML documents apply to all stylesheets you write.
The first element in your XML document must contain the entire document. That first element is called the document element or the root element.
If you start one element inside another, you have to end it there, too.
You can quote the attributes with either single or double quotes.These two XML tags are equivalent:
Also XML doesn’t allow attributes without values.
In HTML, <h1> and <H1> are the same. In XML, they’re not. If you try to end an <h1> element with </H1>, the parser will throw an exception.
This is another area where most HTML documents break. Your browser doesn’t care whether you don’t have a </p> or </br> tag, but your XML parser does.
In other words, these two XML fragments are identical:
Notice that there is nothing, not even whitespace, between the start tag and the end tag in the first example; that’s what makes this an empty tag.
Some XML documents begin with an XML declaration, which is a line similar to this:
<?xml version="1.0" encoding="ISO-8859-1"?>
If no encoding is specified, the XML parser assumes you’re using UTF-8 or UTF-16.
Most of our stylesheets consist of rules (called templates in XSLT) used to transform a document. Each rule says, "When you see part of a document that looks like this, here’s how you convert it into something else."
When the XSLT processor begin transforming the XML document, firstly, it sets some properties based on your stylesheet, for example it would set its output method to HTML, then it begins processing as follows:
1. Do I have any nodes to process? The nodes to process are represented by the context. Initially, the context is the root of the XML document, but it changes throughout the stylesheet. While any nodes are in the context, get the next node from the context.
2. Do I have any <xsl:template>s that match it? IF there is a template that matches this node—it’s the one that begins <xsl:template match="/">. In case when there are two or more <xsl:template>s match, pick the right one and process it. The right one is the most specific template. For example, <xsl:template match="/html/body/h1/p"> is more specific than <xsl:template match="p">. If no <xsl:template>s match, the XSLT processor uses some built-in rules.
3. When the XSLT processor processes the current node by finding the right xsl:template for it, That xsl:template may in turn invoke other
xsl:templates, which invoke xsl:templates as well, this is how the recursive processing model works.
Although most XSLT stylesheets explicitly define how various XML elements should be transformed, XSLT defines several built-in template rules that apply in the absence of any specific rules. These rules have a lower priority than any other templates, so they’re always overridden when you define your own templates.
This template processes the document node and any of its children. This processing ensures that recursive processing will continue, even if no template is declared for a given element:
As an example, given this document:
The built-in template rule for element and document nodes means that we could write a stylesheet containing only a template with match="z" and the <z> element will still be processed, even if there are no template rules for the <x> and <y> elements.
This template ensures that element and document nodes are processed, regardless of any mode that might be in effect.
<xsl:template match="*|/" mode="x">
This template copies the text of all text and attribute nodes to the output tree. Be aware that you have to actually select the text and attribute nodes for this rule to be invoked:
This template does nothing:
This template also does nothing:
Regardless of what your stylesheet contains, XSLT processing always begins with a virtual call to:
This sets the current node list to a list containing only one node—the root node of the source tree. It then invokes the template rule that matches the root node. This virtual call constructs the entire result tree, which, after all, is the point of executing a stylesheet. Your job as an XSLT stylesheet author is to define—using template rules—what happens when the XSLT processor executes this instruction.
Many stylesheets include an explicit template rule that matches the root node: <xsl:template match="/">.... This allows you to take control of processing right off the bat; however, it isn't required. Instead, you could rely on the built-in template rule for root nodes and elements to recursively apply templates until they reach a node for which you have defined an explicit template rule.
© 2020 Digcode.com. All rights reserved.