Introduction to XSLT

advertisement

Introduction to XSLT

What is XML?

Design Goals of XML

XML Format

XML Declaration

Elements

Attributes

Design Goals of XML

XML shall be straightforwardly usable over the Internet.

XML shall support a wide variety of applications.

XML shall be compatible with SGML.

It shall be easy to write programs which process XML documents.

The number of optional features in XML is to be kept to the absolute minimum, ideally zero.

XML documents should be human-legible and reasonably clear.

The XML design should be prepared quickly.

The design of XML shall be formal and concise.

XML documents shall be easy to create.

Terseness in XML markup is of minimal importance.

XML Format

XML contains elements, attributes, and data. An example XML entry would look like:

<book isbn=” 102-0497449-4818519”>

<title>Tom Sawyer</title>

<author>Mark Twain</author>

</book>

XML Declaration

The XML Declaration should appear at the top of each XML file and should specify the XML version and the encoding used within the XML. An example XML declaration would look like:

<?xml version="1.0" encoding="ISO-8859-

1"?>

Elements

XML Elements are contained within <> symbols and must nest properly. Each node that opens must have a corresponding close node. Nodes can contain attributes, data, and “child” nodes.

An example XML element (also called a node) with a single piece of data would look like:

<book>Tom Sawyer</book>

Attributes

An XML Attribute is a single piece of data assigned to an XML Element. If data is assigned as an attribute, the data can not be split further into sub-elements. XML attributes should only be used if the data will never need additional elements created below. An example

XML element with an attribute and a single piece of data would look like:

<book isbn=” 102-0497449-4818519”>Tom

Sawyer</book>

XSLT Basics

What is XSLT?

XML Declaration and XSLT Declaration

Connecting to an XML Data file

What is XSLT?

An XSLT stylesheet utilizes XPath in order to transform the initial XML document into another document. The original XML document must be well formed. The resultant document should also be well formed, though in certain instances, this can be overridden as necessary.

Declarations

<?xml version="1.0" encoding="ISO-8859-1"?>

In addition to the XML declaration, an XSLT stylesheet must specify a style sheet declaration. The style sheet declaration is what enables the XSLT to utilize the binding features.

An example XML Style Sheet Declaration would look like:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transfor m">

Connecting to XML Data

In order to client-side bind the XML Data with the XSLT Stylesheet, a single line must be added within the XML Data. An example binding line would look like:

<?xml-stylesheet type="text/xsl" href="PageDesign.xsl"?>

This line should appear directly beneath the XML Declaration in the XML Data file.

XSLT Tags

<xsl:template>

<xsl:template match=””>

<xsl:apply-templates>

<xsl:value-of>

<xsl:for-each >

<xsl:sort>

<xsl:if>

<xsl:choose> position function mod

<xsl:call-template>

<xsl:copy>

<xsl:copy-of>

<xsl:template>

The <xsl:template> element is used to create individual templates within a single stylesheet. These templates can be accessed by simple matching or called directly via the name attribute.

<xsl:template match=””>

When an XSLT style sheet comes across an

<xsl:template match=”/”> tag, the style sheet will look for matching nodes. The matching node is determined by the value located within the match attribute. In this case a forward slash (/).

In XSLT, a forward slash represents the root node of the XML data. In this case, the template should be matched only a single time as only a single root node can exist within an XML document.

<xsl:apply-templates>

When an XSLT style sheet comes across an <xsl:apply templates> tag, the style sheet will compare current nodes with available <xsl:template match=””> tags.

For each match it comes across (in the current node set), the template will be applied. The select statement can be added to the tag in order to specify the order in which the nodes are processed.

<xsl:value-of>

The <xsl:value-of> tag is used to take the value (or attribute value) from an XML element within the XML Data file and use it within the output stream of the stylesheet.

The standard use is to write output to the final output.

<xsl:for-each >

The <xsl:for-each> tag allows the XSLT to loop through a set of nodes at which point it can use data within each node for output. The <xsl:for-each> is often used to create lists or tables of repeating data.

XPath is used to determine which selection will be looped and can include select statements to filter the looped items.

<xsl:sort>

The <xsl:sort> tag can be used within

<xsl:for-each> lops in order to sort the nodes before the final output has been created. This is often useful for alphabetizing items within a repeating list.

<xsl:if>

The <xsl:if > statement allows the XSLT to perform a test on the current node set to see if it meets conditions for continued processing.

The <xsl:if> statement must contain the entire section it will affect. <xsl:if> statements are most often used within

<xsl:for-each> loops.

<xsl:choose>

The <xsl:choose> statement is used to select a single instance of multiple possibilities. The first condition that is met will be used, if no condition are met, the

<xsl:choose> statement allows the use of

<xsl:otherwise> which will be the catch-all condition.

position function

The position function is used to determine the current position in the node set. This tag will often be used within <xsl:for-each> loops in order to number the output.

mod

Mod has many applications, but the most common is alternating row colors within a table. Because of the nature of mod, the developer can determine how often a pattern repeats (every other row, every third row, etc.).

<xsl:call-template>

The <xsl:call-template> tag works very much like <xsl:apply-templates> with the exception that the developer is able to call a chosen template regardless of the current node in use. This adds the ability to call template that have nested calls whether or not the original template should have been called in the first place.

<xsl:copy> and <xsl:copy-of>

The <xsl:copy> tag allows the XSLT to create a copy of the currently selected node.

The <xsl:copy-of> tag is very similar to the

<xsl:copy” tag but copies the children nodes and data as well.

Old vs New

<img><xsl:attribute name=“src”><xsl:value-of select=“./source”

/></xsl:attribute><xsl:attribute name=“alt”><xsl:value-of select=“./altTag”

/></xsl:attribute></img>

<img src=“{./source}” alt=“{./altTag}” />

Questions?

Download