web analytics

What is the difference between XSL and XSLT?

Options
@2016-01-22 08:13:42

All XSLT stylesheets are themselves XML documents, so all the rules of XML documents apply to all stylesheets you write.

An XML document must be contained in a single element

The first element in your XML document must contain the entire document. That first element is called the document element or the root element.

All elements must be nested

If you start one element inside another, you have to end it there, too.

All attributes must be quoted

You can quote the attributes with either single or double quotes.These two XML tags are equivalent:

<a href="http://www.google.com">

<a href='http://www.google.com'>

Also XML doesn’t allow attributes without values.

XML tags are case-sensitive

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.

All end tags are required

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.

Empty tags can contain the end marker

In other words, these two XML fragments are identical:

<lily age="13"></lily>

<lily age="13"/>

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.

XML declarations

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.

@2016-01-25 14:22:44

Recursive Processing Model and Pattern Matching

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.

  • <xsl:apply-templates select="node-set expression" mode="..." />: process all children on select nodes, mode="..." allows an element to be processed multiple times in different ways.
  • <xsl:call-template name="..." />: invoke template by name.
@2016-01-25 14:27:26

Built-in Template Rules

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.

Built-in template rule for element and document nodes

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:

<xsl:template match="*|/">
      <xsl:apply-templates/>
</xsl:template>

As an example, given this document:

<?xml version="1.0"?>
<x>
     <y>
          <z/>
     </y>
</x>

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.

Built-in template rule for modes

This template ensures that element and document nodes are processed, regardless of any mode that might be in effect.

<xsl:template match="*|/" mode="x">
      <xsl:apply-templates mode="x"/>
</xsl:template>

Built-in template rule for text and attribute nodes

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:

<xsl:template match="text()|@*">
      <xsl:value-of select="."/>
</xsl:template>

Built-in template rule for comment and processing instruction nodes

This template does nothing:

<xsl:template match="comment()|processing-instruction()"/>

Built-in template rule for namespace nodes

This template also does nothing:

<xsl:template match="namespace()"/>

@2016-03-04 16:00:47

Regardless of what your stylesheet contains, XSLT processing always begins with a virtual call to:

<xsl:apply-templates select="/"/>

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.

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com