XSL includes some thirty-file elements. They can be divided into three categories"
- Root elements
- Top-level elements
- Instruction elements
Root elements specify that the document is an XSL document. The most common form is xsl:stylesheet.
Top-level elements may be used immediately under the root. Examples include xsl:import and xsl:template.
Instruction elements appear within the contents of other xsl elements. Examples include xsl:element and xsl:attribute.
The discussion below covers the more frequently used elements. For a complete list/set of descriptions, see the W3C specification of XSLT or a published source, such as Harold & Means, XML in a Nutshell.
Root Elements
xsl:stylesheetExample:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> </xsl:stylesheet>Comment:
You can think of this as boilerplate -- it marks the bounding "container" for the stylesheet, and appears directly following the xml tag.
An equivalent element that is not use as frequently as xsl:stylesheet is xsl:transform.
Top-level Elements
xsl:includeExample:
<xsl:include href="XSLButtons.xsl"/> <xsl:include href="XSLForms.xsl"/> <xsl:include href="XSLPages.xsl"/>Comment:
Inserts files of XSL statements into the current file at this location. Used to divide XSL constructs into logical groups. Included elements have the same status as those in the original file.
xsl:importExample:
<xsl:import href="XSLButtons.xsl"/> <xsl:import href="XSLForms.xsl"/> <xsl:import href="XSLPages.xsl"/>Comment:
Imports files of XSL statements into the current file. Used to divide XSL constructs into logical groups. Similar to xsl:include except that included elements have priority over those in the original file. Thus, if an element with the same name appears in the original file and in an imported file, the imported version will be used (actually determined by a system of priorities; see other sources for details.
xsl:templateExamples:
<xsl:template match="item"> </xsl:template> <xsl:template name="item_template"> </xsl:template>Comment:
The basic building block on which all of XSL is built. The template element is the basic "rule" that is searched for or explicitly called to process nodes in the (constructed) XML document tree.
The match form of template is called by an xsl:apply-template element from the current context. A match will be found if the template successfully matches a portion of the document tree as specified from the current position.
The name form is called by an xsl:call-template element and behaves like a function call. It can be used, for example, to insert a standard element, such as a button, into the document at the current position.
xsl:variableExamples:
<xsl:variable name="varname" select="value"/>Comment:
The name of the variable is set through the name attribute, and the value of the variable is set by the select attribute. xsl:variable is really a constant, since its value cannot be changed. They may be defined both as a top-level element and as a contained element, with obvious implications regarding scope.
The variable and its value are referenced through the name, $varname.
Instruction Elements
xsl:apply-templatesExamples:
<xsl:apply-templates/><xsl:apply-templates select="item"/>Comment:
The main activating element for invoking all applicable templates or a designated template. Doing so shifts the "current node" to the node being processed.
xsl:call-templateExamples:
<xsl:call-template name="item"/>Comment:
An activating element that is an alternative to apply-templates for invoking a specific template. Doing so does not shift the "current node" to the node being processed.
xsl:value-ofExamples:
<xsl:value-of select="address"/> <xsl:value-of select="."/>Comment:
xsl:value-of inserts in the current location the data from the XML document identified by the select attribute.
In the first example, it would select the contents of the <address> tag appearing in the current context, e.g., within an <item> tag.
In the second example, the value would be the contents of the tag that was matched by the template. See the discussion of xsl:element, below, for an example.
xsl:elementExamples:
<xsl:template match="current_table"> <xsl:element name="input"> <xsl:attribute name="type">hidden</xsl:attribute> <xsl:attribute name="name">currentTable</xsl:attribute> <xsl:attribute name="value"><xsl:value-of select="."/></xsl:attribute> </xsl:element> </xsl:template>Comment:
Element, next to xsl:template, is probably the most important construct in XSL. Basically, it allows you to construct an element dynamically.
In the example of above, the value of the attribute, value, is set to the contents of the current tag, in this case <value>.
The output produced by the above would be:
<input type="hidden" name="currentTable" value="SomeTableName">xsl:attributeExamples:
<xsl:attribute name="name">currentTable</xsl:attribute> <xsl:attribute name="value"><xsl:value-of select="."/></xsl:attribute>Comment:
xsl:attribute is used in conjunction with xsl:element to dynamically construct elements, for example, an element within a <form> HTML tag. The name of the attribute is provided the xsl:attribute name attribute and the value of that attribute is provided through either an in-line literal, as in the case of currentTable value of name, above, or thorugh an xsl:value-of element, as in the case of the dynamically set value of the HTML value attribute.
xsl:ifExample:
<xsl:if test="./table = 'File' "> <form method="post" action="http://localhost:8888/pnps/servlet/DBFormServlet" ENCTYPE="multipart/form-data"> </form> </xsl:if>Comment:
xsl:if allows conditional processing of a block of statements. It is not matched by a corresponding else, so it's if or nothing.
xsl:choose xsl:when xsl:otherwiseExample:
<xsl:choose> <xsl:when test="./table = 'File' "> <form method="post" action="http://localhost:8888/pnps/servlet/DBFormServlet" ENCTYPE="multipart/form-data"> <xsl:apply-templates select = "save_state"/> <table > <xsl:call-template name="table_name"/> <xsl:apply-templates select="table_entries"/> <xsl:call-template name="buttons_navigate_group"/> <xsl:call-template name="buttons_edit_group"/> <xsl:call-template name="build_upload_file_group"/> <xsl:call-template name="build_message_display"/> <xsl:call-template name="buttons_database_group"/> </table> </form> </xsl:when> <xsl:otherwise> <form method="post" action="http://localhost:8888/pnps/servlet/DBFormServlet"> <xsl:apply-templates select = "save_state"/> <table > <xsl:call-template name="table_name"/> <xsl:apply-templates select="table_entries"/> <xsl:call-template name="buttons_navigate_group"/> <xsl:call-template name="buttons_edit_group"/> <xsl:call-template name="build_message_display"/> <xsl:call-template name="buttons_database_group"/> </table> </form> </xsl:otherwise> </xsl:choose>Comment:
The xsl:choose, xsl:when, and xsl:otherwise elements may be used together to create a structure similar to a case structure or a sequence of if, else if, . . . , else statements.
The xsl:choose creates the conditional block.
Like the xsl:if statement, the xsl:when element includes a boolelan test. If it evaluates to true, the element that foillows is intersted -- in this case, a form element. If the test evaluates to false, the next xsl:when statement is evaluated.
If all of the xsl:when elements evaluate to false, the final (optional) xsl:otherwise element is inserted -- in this case, a different form.
Useful hint: note the ENCTYPE="multipart/form-data" attribute and value included with the File form.
xsl:foreachExamples:
<xsl:for-each select="return_report"> <xsl:apply-templates select="item"/> </xsl:for-each>Comment:
xsl:foreach provides a construct for iterating over a sequence of similar items. For example, if a report included a number of items (enclosed within <item> tags), the code shown above could be used to successively apply the item template to each of those items.
The order in which the items are output is the order in which they appear in the original document. However, if you wish to output them in some type of sorted order, the xsl:sort element can be used. See, below.
xsl:sortExamples:
xsl:template match="entries"> <xsl:apply-templates > <xsl:sort select="./name_first"/> </xsl:apply-templates> </xsl:template>Comment:
xsl:sort is used to specify one or more sort fields to be used to order the items being identified by a bounding xsl:apply-templates construct.
In the above example, the <item> nodes will be ordered in the set of all such contiguous nodes by the value of the <name_last> field in each.
Elements Not Diccussed
Root Elements
xsl:transform
Top-level Elements
xsl:apply-imports
xsl:attribute-set
xsl:decimal-format
xsl:key
xsl:namesspace-alias
xsl:output
xsl:param
xsl:perserve-space
xsl:strip-spaceInstruction elements
xsl:apply-imports
xsl:comment
xsl:copy
xsl:copy-of
xsl:fallback
xsl:message
xsl:number
xsl:processing-instruction
xsl:text
Examples
The following document will serve as the source document for several different xsl stylesheets.
XML Document
<?xml version="1.0"?> <return_report> <item> <name_first>John </name_first> <name_middle>B. </name_middle> <name_last>Smith </name_last> <address>100 Evans Ct.</address> <city>Carrboro</city> <state>NC</state> <zip>27510</zip> </item> <item> <name_first>F. </name_first> <name_middle>Don </name_middle> <name_last>Smith </name_last> <address>Department of Computer Science</address> <city>Chapel Hill</city> <state>NC</state> <zip>27599-3175</zip> </item> </return_report>XSL Document 1
This is a minimal stylesheet, with references to explicit, expected fields (e.g., name_first);
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <HTML> <HEAD> <TITLE>addresss entry xsl stylesheet</TITLE> </HEAD> <BODY> <xsl:apply-templates/> </BODY> </HTML> </xsl:template> <xsl:template match="item"> <HR/> <xsl:value-of select="name_first"/> <xsl:value-of select="name_middle"/> <xsl:value-of select="name_last"/> <br/><br/> <xsl:value-of select="address"/><br/> <xsl:value-of select="city"/>, <xsl:value-of select="state"/>, <xsl:value-of select="zip"/> </xsl:template> </xsl:stylesheet>See the results
XSL Document 2
This stylesheet illustrates the use of the for-each xsl construct. It has minimal effect here, but it is a general construct that can be used in many different contexts.
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <HTML> <HEAD> <TITLE>addresss entry xsl stylesheet</TITLE> </HEAD> <BODY> <xsl:apply-templates/> </BODY> </HTML> </xsl:template> <!-- ************************* --> <xsl:template match="return_report"> <H3>Names and Addresses</H3> <xsl:for-each select="item"> <hr/> <h4>Item</h4> <xsl:call-template name="item"/> </xsl:for-each> </xsl:template> <!-- ************************* --> <xsl:template match="item" name="item"> <HR/> <xsl:value-of select="name_first"/> <xsl:value-of select="name_middle"/> <xsl:value-of select="name_last"/> <br/><br/> <xsl:value-of select="address"/><br/> <xsl:value-of select="city"/>, <xsl:value-of select="state"/>, <xsl:value-of select="zip"/> </xsl:template> </xsl:stylesheet>See the results
XSL Document 3
This stylesheet illustrates the use of the sort function as well as additional use of xsl:apply-templates.
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <HTML> <HEAD> <TITLE>addresss entry xsl stylesheet</TITLE> </HEAD> <BODY> <xsl:apply-templates/> </BODY> </HTML> </xsl:template> <!-- ************************* --> <xsl:template match="return_report"> <H2>Report</H2> <xsl:apply-templates/> </xsl:template> <!-- ************************* --> <xsl:template match="report_person"> <H3>Names and Addresses</H3> <xsl:apply-templates /> </xsl:template> <!-- ************************* --> <xsl:template match="entries"> <xsl:apply-templates > <xsl:sort select="./name_first"/> </xsl:apply-templates> </xsl:template> <!-- ************************* --> <xsl:template match="item" name="item"> <HR/> <xsl:value-of select="name_first"/> <xsl:value-of select="name_middle"/> <xsl:value-of select="name_last"/> <br/><br/> <xsl:value-of select="address"/><br/> <xsl:value-of select="city"/>, <xsl:value-of select="state"/>, <xsl:value-of select="zip"/> </xsl:template> </xsl:stylesheet>
See the results
XML Document: Form
This xml document will serve as the source document for the xsl stylesheet 4, discussed below.
<?xml version="1.0"?> <return_form> <form_person> <form_item> <name_first>F. </name_first> <name_middle>Don </name_middle> <name_last>Smith </name_last> <address>Department of Computer Science</address> <city>Chapel Hill</city> <state>NC</state> <zip>27599-3175</zip> </form_item> </form_person> </return_form>
XSL Document 4: Form
This stylesheet is an example of a form created from xml data. It also illustrates use of tables and the xsl element construct.
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <HTML> <HEAD> <TITLE>addresss entry xsl stylesheet</TITLE> </HEAD> <BODY> <xsl:apply-templates/> </BODY> </HTML> </xsl:template> <!-- ************************* --> <!-- ************************* --> <xsl:template match="return_form"> <H2>Form</H2> <xsl:apply-templates/> </xsl:template> <!-- ************************* --> <!-- ************************* --> <xsl:template match="form_person"> <H3>Names and Addresses</H3> <xsl:call-template name="form_item"/> </xsl:template> <!-- ************************* --> <!-- ************************* --> <xsl:template match="form_item" name="form_item"> <HR/> <form method="post" action="http://wwwj.cs.unc.edu:8888/jbs/servlet/jbsGetPostEcho"> <table> <tr> <td width="20%" align="right"> <b>First Name: </b> </td> <td width="60%" align="left"> <xsl:element name="input"> <xsl:attribute name="type">text</xsl:attribute> <xsl:attribute name="size">25</xsl:attribute> <xsl:attribute name="name">name_first</xsl:attribute> <xsl:attribute name="value"><xsl:value-of select="name_first"/> </xsl:attribute> </xsl:element> </td> </tr> <tr> <td width="20%" align="right"> <b>Middle Name: </b> </td> <td width="60%" align="left"> <xsl:element name="input"> <xsl:attribute name="type">text</xsl:attribute> <xsl:attribute name="size">25</xsl:attribute> <xsl:attribute name="name">name_middle</xsl:attribute> <xsl:attribute name="value"><xsl:value-of select="name_middle"/> </xsl:attribute> </xsl:element> </td> </tr> <tr> <td width="20%" align="right"> <b>Last Name: </b> </td> <td width="60%" align="left"> <xsl:element name="input"> <xsl:attribute name="type">text</xsl:attribute> <xsl:attribute name="size">25</xsl:attribute> <xsl:attribute name="name">name_last</xsl:attribute> <xsl:attribute name="value"><xsl:value-of select="name_last"/> </xsl:attribute> </xsl:element> </td> </tr> <tr> <td width="20%" align="right"> <b>Address: </b> </td> <td width="60%" align="left"> <xsl:element name="input"> <xsl:attribute name="type">text</xsl:attribute> <xsl:attribute name="size">25</xsl:attribute> <xsl:attribute name="name">address</xsl:attribute> <xsl:attribute name="value"><xsl:value-of select="address"/> </xsl:attribute> </xsl:element> </td> </tr> <tr> <td width="20%" align="right"> <b>City: </b> </td> <td width="60%" align="left"> <xsl:element name="input"> <xsl:attribute name="type">text</xsl:attribute> <xsl:attribute name="size">25</xsl:attribute> <xsl:attribute name="name">city</xsl:attribute> <xsl:attribute name="value"><xsl:value-of select="city"/> </xsl:attribute> </xsl:element> </td> </tr> <tr> <td width="20%" align="right"> <b>State: </b> </td> <td width="60%" align="left"> <xsl:element name="input"> <xsl:attribute name="type">text</xsl:attribute> <xsl:attribute name="size">25</xsl:attribute> <xsl:attribute name="name">state</xsl:attribute> <xsl:attribute name="value"><xsl:value-of select="state"/> </xsl:attribute> </xsl:element> </td> </tr> <tr> <td width="20%" align="right"> <b>Zip: </b> </td> <td width="60%" align="left"> <xsl:element name="input"> <xsl:attribute name="type">text</xsl:attribute> <xsl:attribute name="size">25</xsl:attribute> <xsl:attribute name="name">zip</xsl:attribute> <xsl:attribute name="value"><xsl:value-of select="zip"/> </xsl:attribute> </xsl:element> </td> </tr> <xsl:call-template name="buttons_person_form"/> </table> </form> </xsl:template> <!-- ************************* --> <!-- ************************* --> <xsl:template name="buttons_person_form"> <tr> <td width="20%" align="right"> <!--<b><xsl:value-of select="label"/> </b>--> </td> <td width="60%" align="left"> <xsl:call-template name="button_submit"/> <xsl:call-template name="button_reset"/> <xsl:call-template name="button_clear"/> </td> </tr> </xsl:template> <!-- ************************* --> <xsl:template name="button_submit"> <input type="submit" value="Submit" name="userAction" style="background-color: rgb(224, 250, 226)"/> </xsl:template> <!-- ************************* --> <xsl:template name="button_clear"> <xsl:element name="input"> <xsl:attribute name="type">submit</xsl:attribute> <xsl:attribute name="value">Clear</xsl:attribute> <xsl:attribute name="name">userAction</xsl:attribute> <xsl:attribute name="style">background-color: rgb(224, 250, 226)</xsl:attribute> </xsl:element> </xsl:template> <!-- ************************* --> <xsl:template name="button_reset"> <xsl:element name="input"> <xsl:attribute name="type">reset</xsl:attribute> <xsl:attribute name="value">Reset</xsl:attribute> <xsl:attribute name="name">userAction</xsl:attribute> <xsl:attribute name="style">background-color: rgb(224, 250, 226)</xsl:attribute> </xsl:element> </xsl:template> </xsl:stylesheet>See the results