![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
General Tech |
MapInfo / GIS |
Oracle / Database |
Misc / Useless |
XPath Operator | Explanation |
/ | This is a child operator. Using this operator with another node references the direct children of the specified node. By itself, / represents the children of the root node. Don't confuse this with the / used to terminate empty tags, which is still a required rule in XSL. Examples: '/cities' will select the top-level element <cities>, '/' selects the document root, not the top-level element. |
// | This is an arbitrary descendant operator. It functions in the same manner as the single / operator except that the // refers to any descendant and not only a direct child. Examples: '//city' will select all <city> elements in a document, '//*' will select all elements. |
. | This refers to the current node. |
.. | This refers to the parent node. |
* | An asterisk is a wildcard referencing any nodes it can find. |
@ | The @ refers to an element's attribute. |
[n] | Selects the nth occurence of the selected criteria. |
Math Function | Description |
* | multiplication |
+ | addition |
- | subtraction |
div | division |
mod | remainder of division |
XPath Function | Example |
String Functions | |
concat(string1,string2,...) | <xsl:value-of select='concat(/CustomerRec/FName, " ", /CustomerRec/LName)'/> |
contains(string,stringtofind) | <xsl:if test='contains("FfNn0", /CustomerRecord/IsBillable)'> |
normalize-space(string) | <xsl:if test='normalize-space(@attribute) = 'test'/> |
starts-with(findchars,string) | <xsl:if test='starts-with('k',@attribute)'> |
string(string) | <xsl:if test='string(date)'> |
string-length(string) | <xsl:if test='string-length(/CustomerRecord/CustomerId)=10'> |
substring(string,start,length) | <xsl:value-of select='substring(/CustomerRecord/CustomerId, 6, 5)'/> |
substring-after(string,findchars) | <xsl:value-of select="substring-after('Mahoney, Kevin', ',')"/> |
substring-before(string,findchars) | <xsl:value-of select="substring-before('2002/05/01', '/')"/> |
translate(string,findchars,replacewith) | <xsl:value-of select='translate(/CustomerRecord/RecordType, " ", "_")'/> (like ASP replace function) |
Number Functions | |
ceiling(number) | <xsl:value-of select='ceiling(100 div 70)'/> - returns 2 |
floor(number) | <xsl:value-of select='floor(100 div 70)'/> - returns 1 |
format-number(number,format) | <xsl:value-of select="format-number($cnt, '###,##0')"/> ... |
number(string) | <xsl:if test='not(number(@attribute))'> |
sum(node-set) | <xsl:value-of select='sum(/OrderRecord/Items/Subtotal)'/> |
round(number) | <xsl:value-of select='round(sum(/OrderRecord/LineItems/Item/Subtotal) * 100) div 100'/> |
Node Set Functions | |
count(node-set) | <xsl:value-of select='count(/OrderRecord/Items/LineItem)'/> |
last() | <xsl:value-of select='//orders/order[last()] '/> |
position() | <xsl:value-of select='orders/order[position()] '/> |
Boolean Functions | |
false() | |
not(boolean) | <xsl:if test="not(count(/CustomerRecord/RecordData) = 0)"> ... |
true() |
Instruction |
Syntax |
Description |
<xsl:stylesheet> or <xsl:transform> |
<xsl:stylesheet ...> . . . templates go in here . . . </xsl:stylesheet> |
These tags are an XSL document's root element declaring the document to be an XSL style sheet. xsl:stylesheet and xsl:transform are synonymous and either can be used. |
xsl:copy-of |
<xsl:copy-of select = expression /> example: <xsl:copy> <xsl:apply-templates select='@*|node()'/> </xsl:copy> |
Emits the node-set corresponding to the select expression. |
xsl:value-of |
<xsl:value-of select = string-expression disable-output-escaping = "yes" | "no" /> example: <xsl:value-of select='@cnt'/> |
Emits the string corresponding to the select expression. |
xsl:if |
<xsl:if test = boolean-expression> <!- - Content: template - -> </xsl:if> example: <xsl:if test='count(//author) > 4'> <cacophony/> </xsl:if> |
Evaluates the template if and only if the test expression evaluates to true. |
xsl:choose xsl:when xsl:otherwise |
<xsl:choose> <!- - Content: (xsl:when+, xsl:otherwise?) - -> </xsl:choose> example: <xsl:choose> <xsl:when test='count(//author) = 1'> <soloist/> </xsl:when> <xsl:when test='count(//author) < 5'> <ensemble/> </xsl:when> <xsl:otherwise> <cacophony/> </xsl:otherwise> </xsl:choose> |
These three elements are used together for conditional case testing. xsl:choose sets up the template. Different case conditions are specified by xsl:when, which if tested true, will have those XSL rules applied. And xsl:otherwise sets up the rules to apply when none of the xsl:when cases hold true. |
xsl:for-each |
<xsl:for-each select = node-set-expression> <!- - Content: (xsl:sort*, template) - -> </xsl:for-each> examples: <xsl:for-each select='/book/author'> <by id='{position()}'> <xsl:value-of select='@name' /> </by> </xsl:for-each> <xsl:for-each select="ord/@OrderID[not(.=preceding::ord/@OrderID)]"> <Order> <OrderID><xsl:value-of select="."/></OrderID> </Order> </xsl:for-each> |
Evaluates the template against each node in node-set returned by the select expression. The order of evaluation can be influenced using one or more xsl:sorts. |
xsl:template |
<xsl:template name = qname> <!- - Content: template - -> </xsl:template> example: <xsl:template name='emitSignature'> <sig><xsl:value-of select='/book/@title'/></sig> </xsl:template> |
An XSL style sheet contains xsl:template elements that enclose rules to apply when a specified pattern is matched. |
xsl:call-template |
<xsl:call-template name = qname> <!- - Content: xsl:with-param* - -> </xsl:call-template> example: <xsl:call-template name='emitSignature' /> |
Invokes the template rule named by name. |
xsl:text |
<xsl:text disable-output-escaping = "yes" | "no"> <!- - Content: #PCDATA - -> </xsl:text> example: <xsl:text>Wo>rld</xsl:text> generates - wo>rld <xsl:text disable-output-escaping='yes'>Wo>rld</xsl:text> generates - wo>rld |
Emits the text found in #PCDATA. Escaping of the five built-in entities is controlled using disable-output-escaping. |
xsl:number |
<xsl:number level = "single" | "multiple" | "any" count = pattern from = pattern value = number-expression format = { string } lang = { nmtoken } letter-value = { "alphabetic" | "traditional" } grouping-separator = { char } grouping-size = { number } /> example: <xsl>number value="5000000 div 3" grouping-separator="," grouping-size="3" /> - outputs 1,666,667 |
Emits a number based on the XPath number expression found in value or can autonumber based on node (use different level attribute to determine who nodes should be numbered). |
xsl:copy |
<xsl:copy use-attribute-sets = qnames> <!- - Content: template - -> </xsl:copy> |
Copies the current context node (and associated namespace nodes) to the result tree fragment. |
xsl:apply-templates |
<xsl:apply-templates select = node-set-expression mode = qname> <!- - Content: (xsl:sort | xsl:with-param)* - -> </xsl:apply-templates> |
Invokes the best-match template rules against the node-set returned by the select expression. |
xsl:apply-imports |
<xsl:apply-imports /> |
Promotes the current stylesheet in import precedence. |
xsl:message |
<xsl:message terminate = "yes" | "no"> <!- - Content: template - -> </xsl:message> |
Emits a message in a processor-dependent manner. |
xsl:fallback |
<xsl:fallback> <!- - Content: template - -> </xsl:fallback> |
Evaluates the template when the parent instruction/directive is not supported by the current processor. |
xsl:comment |
<xsl:comment> <!- - Content: template - -> </xsl:comment> example: <xsl:comment>comment number two</xsl:comment> generates - <!--comment number two--> |
Emits an XML comment containing the template as its character data. |
xsl:processing-instruction |
<xsl:processing-instruction name = { ncname }> <!- - Content: template - -> </xsl:processing-instruction> example: <xsl:processing-instruction name='A'> PI</xsl:processing-instruction> generates - <?A PI?> |
Emits an XML processing instruction whose [target] is name and whose [children] are based on template. |
xsl:element |
<xsl:element name = { qname } namespace = { uri-reference } use-attribute-sets = qnames> <!- - Content: template - -> </xsl:element> example: <xsl:element name='bob' namespace='http://example.com'> <xsl:element name='steve' namespace='http://example.com'/> </xsl:element> |
Emits an XML element whose [local name] is name, whose [namespace URI] is namespace, and whose [children] are based on template. |
xsl:attribute |
<xsl:attribute name = { qname } namespace = { uri-reference }> <!- - Content: template - -> </xsl:attribute> example: <a> <xsl:attribute name="href">http://www.amazon.com/exec/obidos/ASIN/ <xsl:value-of select="isbn"/>/kevigmahoshomepa </xsl:attribute> <xsl:value-of select="title"/> </a> generates <a href="http://www.amazon.com/exec/obidos/ASIN/123/kevigmahoshomepa">Book Title</a> |
Emits an XML attribute whose [local name] is name, whose [namespace URI] is namespace, and whose [children] are based on template. |
xsl:sort |
<xsl:sort select = string-expression lang = { nmtoken } data-type = { "text" | "number" | qname-but-not-ncname } order = { "ascending" | "descending" } case-order = { "upper-first" | "lower-first" } /> example: <xsl:for-each select='/book/author'> <xsl:sort select='@canadian' order='descending'/> <xsl:sort select='@name' /> <by><xsl:value-of select='@name' /></by> </xsl:for-each> |
Sorts the output in a specified order. |
xsl:param |
<xsl:param name = qname select = expression> <!- - Content: template - -> </xsl:param> example: <xsl:template name='emitTop'> <xsl:param name='arg1' select='/book/author[2]/@name' /> <xsl:param name='arg2'>true</xsl:param> <top> <xsl:if test='$arg2'> <sometimes/> </xsl:if> <one><xsl:value-of select='$arg1' /></one> </top> </xsl:template> <xsl:call-template name='emitTop'> <xsl:with-param name='arg1'>Hello</xsl:with-param> <xsl:with-param name='arg2' select='false()' /> </xsl:call-template> |
Declare a named parameter and set its default value - parameters, unlike variables, can have their initial values overridden at template invocation time by using xsl:with-param - parameters instructions must appear at the top of the template in which they are included; xsl:variable instructions can appear anywhere an instruction is allowed. |
xsl:variable |
<xsl:variable name = qname select = expression> <!- - Content: template - -> </xsl:variable> |
Declares a variable named name and initializes it using the select expression or template. |
xsl:namespace-alias |
<xsl:namespace-alias stylesheet-prefix = prefix | "#default" result-prefix = prefix | "#default" /> example: <xslt:stylesheet version='1.0' xmlns:xslt='http://www.w3.org/1999/XSL/Transform' xmlns:xsl='urn:fake:uri'> <xslt:namespace-alias stylesheet-prefix='xsl' result-prefix='xslt' /> <xslt:template name='emitStylesheet'> <xsl:stylesheet version='1.0'> <xsl:template name='{//@procName}' /> </xsl:stylesheet> </xslt:template> </xslt:stylesheet> |
Alters the namespace URI of one namespace declaration by associating it with a second namespace declaration that provides the actual namespace URI to be used in the result document. |
xsl:include |
example: <xsl:include href='stylesheeta.xsl' /> |
Causes the contents of an external stylesheet to be merged with the importing stylesheet. |
xsl:import |
example: <xsl:import href='first.xsl' /> |
A collision-aware import mechanism - all xsl:import directives must appear as the initial [children] of an xsl:stylesheet element. |
xsl:output |
<xsl:output method = "xml" | "html" | "text" | qname-but-not-ncname version = nmtoken encoding = string omit-xml-declaration = "yes" | "no" standalone = "yes" | "no" doctype-public = string doctype-system = string cdata-section-elements = qnames indent = "yes" | "no" media-type = string /> examples: <xsl:output method='text' /> <xsl:output indent="no"/> - tell the XSLT processor not to add whitespace for readability, which typically results in a smaller document |
Controls what XSLT stylesheet produces. |
xsl:space xsl:strip-space xsl:preserve-space |
example: <b xml:space='preserve'></b> <xsl:preserve-space elements="*" /> <xsl:strip-space elements="author title" /> |
Controls whitespace stripping - xsl:text element is automatically in the whitespace preserving list and does not need to be explicitly listed in the xsl:preserve-space directive. |
xsl:decimal-format |
<xsl:decimal-format name = qname decimal-separator = char grouping-separator = char infinity = string minus-sign = char NaN = string percent = char per-mille = char zero-digit = char digit = char pattern-separator = char /> |
Formats a number. |