Credit: Jon A. Morse (ST Sci/NASA)
Major Structures in XSL
XSL can convert XML to to plain text, to HTML, or to other values of
XML.
Some of the most important structures in XSL are:
- <xsl:apply-templates select="Xpath pattern"/>:
this directs the XSL processor to look for a "template match"
in the XSL code that most specifically matches the above
Xpath pattern. If this pattern is found, the code in the
matching template is executed and the resulting output replaces
the original "xsl:apply-templates" code
- <xsl:template match = "Xpath pattern">:
this contains code that is executed if the "Xpath pattern"
specified here most closely matches the Xpath pattern in
an "xsl:apply-templates" call. Note that the "xsl:template"
statement establishes a "context node"; that is, the node
specified in its "Xpath pattern" serves as a basis, or context, for
all Xpath nodes and expressions within the body of the
"xsl:template" statement. This means that all nodes are
assumed to be attributes or descendents of its "Xpath pattern"
- <xsl:value-of select = "Xpath pattern"/>:
this outputs the text value of the node specified in the Xpath
pattern, concatenated with the text value of all of its sub-nodes,
if any
- <xsl:if test = "Xpath pattern"/>:
the "Xpath pattern" is evaluated -- if the result is true, or
evaluates to a non-zero number, the XSL code in the body of
the statement is executed, otherwise it is skipped. For example:
<xsl:if test = " @ftype = 'pdf' or @ftype = 'tif' " >
<xsl:value-of select = "link" />
</xsl:if>
Assuming that the current context node has an attribute
named "ftype", it tests to see if this value is equal to
either "pdf" or "tif". If so, then the text value of the
"link" node (assuming that it is a sub-node of the current
context node) is output, along with the text value of all
its sub-nodes, if any. Otherwise, if either the attribute
"ftype" does not exist, or if it does not have the specified
values, the "xsl:value-of" statement is ignored.
-
<xsl:choose>...<xsl:when>...<xsl:otherwise>...
</xsl:choose>
: a multi-way conditional that lets you select different
alternatives. For example:
<xsl:choose>
<xsl:when test = "contains(ancestor::PO//Description, 'laptop')">
<xsl:value-of select="ancestor::PO//Description" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="./status" />
</xsl:otherwise>
</xsl:choose>
So if the current context node has a PO ancestor which, one or more
levels down contains a "Description" node that contains the text 'laptop',
then the text value of that Description node is output, otherwise,
the test value of a "status" subnode of the current context node is
output.
- <xsl:for-each select = "Xpath pattern">: an
iteration statement in XSL that lets you cycle through
a series of XML nodes identified by the "Xpath pattern".
For example:
<xsl:for-each select = "ancestor::PO/Attachments/attachment" >
<xsl:value-of select = "link" />
</xsl:for-each>
This cycles through all of the "attachment" elements of the
"Attachments" element and prints out the text value of the "link"
element for each "attachment". Note that the "xsl:for-each"
statement establishes a context node: in this case it is
the "attachment" element.
- <xsl:element name = "name"> and
<xsl:attribute name = "href">:
these XSL statements let you produce complex HTML output
such as INPUT boxes, SELECT boxes, or HyperText links. For example:
- To output an HTML INPUT box such as:
<input type='text' size='15' value='Joe Schmoe'>
where the current entry has an element <Name>Joe Schmoe</Name>
we use this syntax:
<xsl:element name='input'> (1)
<xsl:attribute name="size"> (2)
15 (3)
</xsl:attribute> (4)
<xsl:attribute name="value"> (5)
<xsl:value-of select='.'/> (6)
</xsl:attribute> (7)
</xsl:element> (8)
The 1st line adds: <input type='text'
The 2nd, 3rd, and 4th lines add: size='15'
The 5th, 6th, and 7th lines add: value='Joe Schmoe'
The 8th line adds: >
- Similarly, to add a SELECT box, such as:
<select name='Duration'>
<option> 15 minutes </option>
<option> 25 minutes </option>
<option> 35 minutes </option>
</select>
we use this XSL code:
<xsl:element name='select'>
<xsl:attribute name="name">
Duration
</xsl:attribute>
<xsl:element name='option'>15 Minutes</xsl:element>
<xsl:element name='option'>25 Minutes</xsl:element>
<xsl:element name='option'>35 Minutes</xsl:element>
</xsl:element>
- And to add a link, such as:
<a href="staff.washington.edu/larryg"> <image src="red.gif"> </a>
where the current entry has an element
<Link> staff.washington.edu/larryg </Link>, the XSL code is:
<xsl:element name="A">
<xsl:attribute name="href">
<xsl:value-of select="Link" />
</xsl:attribute>
<image src="red.gif"> </image>
</xsl:element>
-
<xsl:variable name = "name" select = "Xpath pattern" />
: this creates a variable that can be used in other
expressions by prefacing the "name" with a "$". For example:
<xsl:variable name = "yr" select = "substring(Id/date,5,6)" />
. . .
Year: <xsl:value-of select = "$yr" />
The above creates a variable named "yr" which is later used
in the expression "$yr"
- <xsl:copy-of select = "Xpath pattern" />:
this copies the complete subtree specified by the "Xpath pattern"
verbatim to the output. This is normally used in creating XML
rather than HTML. For example:
<xsl:for-each select = "./*" >
<xsl:copy-of select = "." />
</xsl:for-each>
The above cycles through each element of the current subtree and
prints out its tree structure verbatim
-
<xsl:sort select = "Xpath pattern" data-type = "type" order =
"direction"/>:
this sorts the output for tree elements defined by the "Xpath
pattern" based on a data type of either "text" or "number", and
in "ascending" or "descending" direction. For example:
<xsl:for-each select = "//POlist/PO" >
<xsl:sort select = "Id/trackNbr" data-type = "number" order =
"ascending"/>
<xsl:value-of select="Id/status" />
</xsl:for-each>
- <xsl:call-template name = "name" >:
this is used with two other structures:
<xsl:with-param name="name"> and
<xsl:param name = "name" />. Unlike the
"apply-templates" and "templates match" pair above which operates
like an implicit function call, this is an explicit
function call that activates a named section of XSL code and then
replaces itself with the results of that call. For example:
<xsl:call-template name = "fullname" >
<xsl:with-param name="first">
<xsl:value-of select = "./first_name"/>
</xsl:with-param>
<xsl:with-param name="last">
<xsl:value-of select = "./last_name"/>
</xsl:with-param>
</xsl:call-template>
. . .
<xsl:template name = "fullname">
<xsl:param name = "first" />
<xsl:param name = "last" />
First name: <xsl:value-of select = "$first" />
Last name: <xsl:value-of select = "$last" />
</xsl:template>
The above code calls the template named "fullname", replaces
its parameters with the ones declared in the "xsl:with-param"
statements, and then replace the entire "call-template"
block with the values of the first and last name.
|
|
 |
|
 |