XSLT elements

Last updated

XSLT (Extensible Stylesheet Language Transformations) defines many elements to describe the transformations that should be applied to a document. This article lists some of these elements. For an introduction to XSLT, see the main article.

Contents

XSLT logic elements

NodeDescription/ attributesContainer/ childrenExample
xsl:apply-templatesSpecifies that other matches may exist within that node; if this is not specified any matches will be ignored. If select is specified, only the templates that specify a “match” that fits the selected node or attribute type will be applied. I.e. the matching elements by select attribute in xsl:apply-templates correspond to the template that match the same elements. If mode is specified, only the templates that have the same “mode” and have an appropriate “match” will be applied.Any parent. Can contain any number of xsl:sort and xsl:with-param children.
<xsl:apply-templates/>
xsl:chooseMultiple choices. No attributes.Any parent. Contains xsl:when blocks and up to one xsl:otherwise block.
<xsl:choose></xsl:choose>
xsl:for-eachCreates a loop which repeats for every match. select designates the match criteria.Any parent. Can contain any XML.
<xsl:for-eachselect="input[@name=$name]"></xsl:for-each>
xsl:ifYes or out No conditions. test specifies criteria for entering the if.Any parent. Can contain any XML.
<xsl:iftest="$type='text' or $type='password'"></xsl:if>
xsl:otherwiseThe default choice if none of the xsl:when criteria are met.xsl:choose. Can contain any XML.
<xsl:otherwise></xsl:otherwise>
xsl:stylesheetTop-level element. Occurs only once in a stylesheet document. version specifies which XSLT version is being used. xmlns:xsl specifies the URL of that standard.Top-level element. Contains all XML.
<xsl:stylesheet></xsl:stylesheet>
xsl:templateSpecifies processing templates. match is when the template should be used. name gives the template a name which xsl:call-template can use to call this template.xsl:stylesheet. Can contain any XML.
<xsl:templatematch="//input"></xsl:template>
xsl:variableAllows a variable to be declared. name is the variable name. It can be referred to later with $name. select is the value of the variable.Any parent. no children.
<xsl:variablename="type"select="@type"/>
xsl:whenYes or No conditions. test specifies criteria for entering the if.xsl:choose. Can contain any XML.
<xsl:whentest="$type='radio'"></xsl:when>

Example XSLT stylesheet using logic elements

<xsl:stylesheet><xsl:templatematch="//input"><xsl:variablename="type"select="@type"/><xsl:variablename="name"select="@name"/><xsl:iftest="$type='text' or $type='password' or $type='radio' or $type='checkbox'"><xsl:choose><xsl:whentest="$type='radio'"><xsl:iftest="not(preceding-sibling::input[@type='radio'])"><selectname="{@name}"><xsl:for-eachselect="../input[@name=$name]"><optionvalue="{@value}"><xsl:apply-templates/></option></xsl:for-each></select></xsl:if></xsl:when><xsl:whentest="$type='text'"><inputname="{@name}"type="{@type}"><xsl:apply-templates/></input></xsl:when><xsl:whentest="$type='password'"><inputname="{@name}"type="{@type}"><xsl:apply-templates/></input></xsl:when></xsl:choose></xsl:if></xsl:template></xsl:stylesheet>

XSLT file I/O elements

NodeDescription/ attributesContainer/ childrenExample
xsl:call-templateCalls the template whose name is specified. name matches the name in xsl:template.Any parent. children are xsl:with-param.
<xsl:call-templatename="lib:make-elem">
xsl:importRetrieves another XSLT file. href is the URI of the file.xsl:stylesheet. No children.
<xsl:importhref="..\Library\FuncLib.xslt"/>
xsl:outputDescribes how data will be returned. method designates what kind of data is returned. omit-xml-declaration indicates if the initial xml tag should be included. encoding designates how the data is returned.xsl:stylesheet. No children.
<xsl:outputmethod="xml"omit-xml-declaration="yes"encoding="UTF-8"/>
xsl:paramDesignates a parameter which may be passed into the template with xsl:call-template.xsl:template. No children.
<xsl:paramname="elem-name"/>
xsl:textOutputs the tag content.Any parent. No children.
<xsl:text>ClaimNumber ClaimSeqNumber</xsl:text>
xsl:value-ofOutputs a variable. select specifies the variable.Any parent. No children.
<xsl:value-ofselect="$s"/>
xsl:with-paramDesignates a parameter passed to xsl:call-template. It must be matched by an xsl:param in the template. name specifies the parameter name. select specifies the parameter value.xsl:call-template. No children.
<xsl:with-paramname="elem-name"select="'hma:ClaimNumber'"/>

Client-side XSLT can be implemented in a browser by adding a line like the following to the source XML file, right after the root XML tag.

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

This is described on the page http://www.xml.com/pub/a/2000/10/25/msie/index.html

Other XSLT semantics

symbolmeaningwhere used
|union operator. used to group nodesets in XPath expressions.select attributes of elements such as xsl:when, xsl:if, xsl:for-each.
&andany conditional or match criterion, for example xsl:if.test, xsl:when.test, xsl:template.select and xsl:for-each.select.
<!--begin comment.anywhere not in a tag.
-->end comment.anywhere not in a tag.
$start of a variable name.anywhere in a tag, for example xsl:value-of.select and xsl:variable.name.
name()the name of the tag being processed. Useful if the matching criteria contains |s (pipe symbols).any conditional or match criterion, for example xsl:if.test, xsl:when.test, xsl:template.select and xsl:for-each.select.
@an attribute within the XML.any conditional or match criterion, for example xsl:if.test, xsl:when.test, xsl:template.select and xsl:for-each.select.

[1]

Functions defined by XSLT

The following functions can occur in many XSLT attributes, such as xsl:value-of.select and xsl:for-each.select.

FunctionDescription/ SyntaxExample
ceilingThe ceiling function returns the smallest integer that is equal to or is larger than the numeric value of the number argument.
ceiling(3.57)
concatConcatenates two or more strings.
concat($fname,' ',$lname)
contains [2] Returns true if the first string contains the second string, otherwise returns false.
contains('string to search','find')
countThe count function counts and returns the number of nodes in a node-set.
count(elements)
floorThe floor function returns the largest integer that is equal to or is smaller than the numeric value of the number argument.
floor(3.57)
normalize-spaceRemoves white-space from the beginning and end of the string
normalize-space($fname)
positionThe position function returns the one-based [3] index of the current node being processed by an xsl:for-each or xsl:apply-templates element node-list. There are no arguments.
position()
roundThe round function rounds a number to its closest integer.
round(3.57)
stringThe string function converts the value argument to a string.
string()
string-lengthThe string-length function returns the number of characters in a string. The string argument is optional. If omitted, the default is to use the string value of the context node.
string-length('hello')
substringA segment within a variable value. Substring takes three parameters: the input variable, the first character to be selected, and the length of the resulting string
substring($dob,4,2)
substring-afterThe substring-after function returns a portion out of the string specified in the string argument that occurs after the substring specified in the substring argument.
substring-after('In 1814 we took a little trip','we')
substring-beforeThe substring-before function returns a portion out of the string specified in the string argument that occurs before the substring specified in the substring argument.
substring-before('In 1814 we took a little trip','we')
sumThe sum function adds and returns the total value of a set of numeric values in a node-set or list of values.
sum(1,3,7,12)
translateTakes the string in the value argument, replaces all occurrences of characters in the string1 argument with substitute characters at the same location in the string2 argument and returns the modified string.
translate('band','abcd','ABCD')='BAnD'

Related Research Articles

In computing, the term Extensible Stylesheet Language (XSL) is used to refer to a family of languages used to transform and render XML documents.

XSLT is a language for transforming XML documents into other XML documents, or other formats such as HTML for web pages, plain text or XSL Formatting Objects, which may subsequently be converted to other formats, such as PDF, PostScript and PNG. XSLT 1.0 is widely supported in modern web browsers.

In computing, the Java API for XML Processing, or JAXP, one of the Java XML Application programming interfaces, provides the capability of validating and parsing XML documents. It has three basic parsing interfaces:

DocBook is a semantic markup language for technical documentation. It was originally intended for writing technical documents related to computer hardware and software, but it can be used for any other sort of documentation.

In software, an XML pipeline is formed when XML processes, especially XML transformations and XML validations, are connected.

XSL-FO is a markup language for XML document formatting that is most often used to generate PDF files. XSL-FO is part of XSL, a set of W3C technologies designed for the transformation and formatting of XML data. The other parts of XSL are XSLT and XPath. Version 1.1 of XSL-FO was published in 2006.

An XML editor is a markup language editor with added functionality to facilitate the editing of XML. This can be done using a plain text editor, with all the code visible, but XML editors have added facilities like tag completion and menus and buttons for tasks that are common in XML editing, based on data supplied with document type definition (DTD) or the XML tree.

XPath 2.0 is a version of the XPath language defined by the World Wide Web Consortium, W3C. It became a recommendation on 23 January 2007. As a W3C Recommendation it was superseded by XPath 3.0 on 10 April 2014.

The following tables compare XML compatibility and support for a number of browser engines.

The identity transform is a data transformation that copies the source data into the destination data without change.

In computing, the two primary stylesheet languages are Cascading Style Sheets (CSS) and the Extensible Stylesheet Language (XSL). While they are both called stylesheet languages, they have very different purposes and ways of going about their tasks.

Oxygen XML Editor

The Oxygen XML Editor is a multi-platform XML editor, XSLT/XQuery debugger and profiler with Unicode support. It is a Java application, so it can run in Windows, Mac OS X, and Linux. It also has a version that can run as an Eclipse plugin.

The DocBook XSL stylesheets are a set of XSLT stylesheets for the XML-based DocBook language.

XSLT/Muenchian grouping Algorithm for grouping of data used in XSL Transformations

Muenchian grouping is an algorithm for grouping of data used in XSL Transformations v1 that identifies keys in the results and then queries all nodes with that key. This improves the traditional alternative for grouping, whereby each node is checked against previous nodes to determine if the key is unique . In both cases the key can take the form of an attribute, element, or computed value.

XMLStarlet is a set of command line utilities (toolkit) to query, transform, validate, and edit XML documents and files using a simple set of shell commands in a way similar to how it is done with UNIX grep, sed, awk, diff, patch, join, etc commands.

XPath is a query language for selecting nodes from an XML document. In addition, XPath may be used to compute values from the content of an XML document. XPath was defined by the World Wide Web Consortium (W3C).

XQuery is a query and functional programming language that queries and transforms collections of structured and unstructured data, usually in the form of XML, text and with vendor-specific extensions for other data formats. The language is developed by the XML Query working group of the W3C. The work is closely coordinated with the development of XSLT by the XSL Working Group; the two groups share responsibility for XPath, which is a subset of XQuery.

A Processing Instruction (PI) is an SGML and XML node type, which may occur anywhere in the document, intended to carry instructions to the application.

XML transformation language

An XML transformation language is a programming language designed specifically to transform an input XML document into an output document which satisfies some specific goal.

IEEE STANDARD 1855-2016, IEEE Standard for Fuzzy Markup language (FML), is a technical standard developed by the IEEE Standards Association. FML allows modelling a fuzzy logic system in a human-readable and hardware independent way. FML is based on eXtensible Markup Language (XML). The designers of fuzzy systems with FML have a unified and high-level methodology for describing interoperable fuzzy systems. IEEE STANDARD 1855-2016 uses the W3C XML Schema definition language to define the syntax and semantics of the FML programs.

References

  1. "XSLT Reference".
  2. "XSLT 1.0: Function contains".
  3. "Example 16:1".