eXtensible Stylesheet Language Transforms (XSLT)
An Introduction |
Prof. David Bernstein
|
Computer Science Department |
bernstdh@jmu.edu |
xsl
namespaceWhen the conceptual (as opposed to actual) root element is encountered, we want to build the skeleton of the HTML document.
xsltexamples/ilovejmu/simple.xsl (Fragment: 0)
When a text
(i.e., actual root) element is
encountered, we want to insert a P
element into the
HTML skeleton, and insert the contents of the text
element in the P
.
xsl:stylesheet
Element<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl" result-ns="html">
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html" indent="yes"/>
xsl:template
Element
match
|
The pattern that determines which nodes will be processed by this template. |
xsl:apply-templates
Element
select
|
The pattern that determines which nodes will be processed. If this attribute is omitted, all nodes will be processed. |
xsl:value-of
Element
select
|
The pattern that determines which nodes will be processed. If this attribute is omitted, the current node will be returned. If more than one node is matched, only the first will be returned. |
/
|
The child operator. It selects elements that are
children of the specified node. For example,
reference/year refers to the year
element that is a child of the reference element.
|
.
|
The current node operator. For example, ./year
refers to the year element that is a child of the
current element.
|
@
|
The attribute operator. For example, train/@number
refers to the number attribute of the train
element.
|
*
|
The wildcard operator. For example, timetable/*
refers to all children of the timetable
element.
|
[]
|
The index operator (1-based). |
position()
returns the index of the current node
(1-based)last()
returns the index of the last node
=
|
The equal value operator. For example, time[@status = 'Ar']
refers to a time element with a status
attribute equal to "Ar".
|
>
|
The greater than value operator. |
<
|
The less than value operator. |
>=
|
The greater than or equal to value operator. |
<=
|
The less than or equal to value operator. |
!=
|
The not equal to value operator. |
|
xsl:if
Element
test
|
The pattern that determines the value of the test condition. |
xsl:if
Element (cont.)
xsl:choose
Elementxsl:when
elementsxsl:otherwise
elementxsl:if
xsl:choose
Element (cont.)
<xsl:template match="*|/"> <xsl:apply-templates/> </xsl:template>
<xsl:template match="text()|@*"> <xsl:value-of select="."/> </xsl:template>