Tuesday, May 22, 2007

xslt notes

XSLT is a powerful language for manipulating the data in XML documents.Creating and using templates is the very heart of XSLT.

XSLT can function like a database language such as SQL (Structured Query Language, the famous database-access language), because it enables you to extract the data you want from XML documents, much like applying an SQL statement to a database. Some people even think of XSLT as the SQL of the Web.

How do you connect this stylesheet to the XML document planets.xml?one way to do that is with an <?xml-stylesheet?> XML processing instruction. This processing instruction uses two attributes. The first attribute is type, which you set to "text/xml" to indicate that you're using an XSLT stylesheet. (To use the other type of stylesheets, cascading stylesheets [CSS]―which are usually used with HTML―you'd use "text/css".) The second attribute is href, which you set to the URI (recall that XML uses Uniform Resource Identifiers, URIs, rather than URLs) of the stylesheet.

You can use XSLT in three ways to transform XML documents:

  • With standalone programs called XSLT Processors. There are several programs, usually based on Java, that will perform XSLT transformations; we'll see a number of them in this chapter.

  • In the client. A client program, such as a browser, can perform the transformation, reading in the stylesheet you specify with the <?xml-stylesheet?> processing instruction. The Internet Explorer can handle transformations this way to some extent.

  • In the server. A server program, such as a Java servlet, can use a stylesheet to transform a document automatically and send it to the client.

When you're working with XSLT, you no longer think in terms of documents, but rather in terms of trees.

From the XSLT point of view, then, documents are trees built of nodes; XSLT recognizes seven types of nodes:

  • The root node. This is the very start of the document. This node represents the entire document to the XSLT processor. Important: Don't get the root node mixed up with the root element, which is also called the document element (more on this later in this chapter).

  • Attribute node. Holds the value of an attribute after entity references have been expanded and surrounding whitespace has been trimmed.

  • Comment node. Holds the text of a comment, not including <!-- and -->.

  • Element node. Consists of the part of the document bounded by a start and matching end tag, or a single empty element tag, such as <br/>.

  • Namespace node. Represents a namespace declaration―and note that it is added to each element to which it applies.

  • Processing instruction node. Holds the text of the processing instruction, which does not include <? and ?>. The XML declaration, <?xml version="1.0"?>, by the way, is not a processing instruction, even though it looks like one. XSLT processors strip it out automatically.

  • Text node. Text nodes hold sequences of characters―that is, PCDATA text. Text nodes are normalized by default in XSLT, which means that adjacent text nodes are merged.

==========

XSL defines a number of top-level elements that can be direct child elements of <xsl:stylesheet>:

  • <xsl:attribute-set>

  • <xsl:decimal-format>

  • <xsl:import>

  • <xsl:include>

  • <xsl:key>

  • <xsl:namespace-alias>

  • <xsl:output>

  • <xsl:param>

  • <xsl:preserve-space>

  • <xsl:strip-space>

  • <xsl:template>

  • <xsl:variable>

The XSLT 1.1 working draft adds one more top-level element:

  • <xsl:script>

==========

XSLT Instructions

A number of XSLT elements, called instructions, may appear in a template body:

  • <xsl:apply-imports>

  • <xsl:apply-templates>

  • <xsl:attribute>

  • <xsl:call-template>

  • <xsl:choose>

  • <xsl:comment>

  • <xsl:copy>

  • <xsl:copy-of>

  • <xsl:element>

  • <xsl:fallback>

  • <xsl:for-each>

  • <xsl:if>

  • <xsl:message>

  • <xsl:number>

  • <xsl:processing-instruction>

  • <xsl:text>

  • <xsl:value-of>

  • <xsl:variable>

==========
If an element in a template body is not an XSL instruction or an extension element, then the XSLT processor must treat it as a literal result element. This means that an element must be treated literally and copied to the result tree (that is, copied to the output node tree created by the XSLT processor).

==========
To indicate what node or nodes you want to work on in a template, XSLT supports various ways of
matching or selecting nodes.

set the match attribute of <xsl:template> to a
pattern that matches the name of the node or nodes you want to work with which is on templates, shows you how to create patterns. For example, the pattern "/" stands for the root node. The pattern "*" matches any element node. The pattern "PLANET" matches all <PLANET> element nodes, and so on.

You can access the value of a node with the <xsl:value-of> element. This element has two possible attributes:

  • select (mandatory). The value that will be output. Set to an expression.To refer to an attribute value using XPath, you preface the attribute name with @ like this: "@src", "@height", "@width", and so on.

  • disable-output-escaping (optional). Indicates that characters such as ">" should be sent to the output as is, without being changed to "&gt". Set to "yes" or "no".

The precedence of an imported stylesheet or stylesheet fragment is lower than the precedence of the stylesheet that's importing it.

No comments: