XSLT and XPath Reference

Please leave a remark at the bottom of each page with your useful suggestion.


XSLT and XPath Reference


Table of Contents


XPath Reference


XSLT Introduction

XSL stands for EXtensible Stylesheet Language, and is a style sheet language for XML documents.

  • CSS - stands for Cascading Style Sheets = Style Sheets for HTML
  • XSL - stands for EXtensible Stylesheet Language = Style Sheets for XML
  • XSLT - stands for XSL Transformations = transform an XML document into another XML document

XSLT is used to transform an XML document into another XML document, or another type of document that is recognized by a browser, like HTML and XHTML. Normally XSLT does this by transforming each XML element into an (X)HTML element. With XSLT you can add/remove elements and attributes to or from the output file. You can also rearrange and sort elements, perform tests and make decisions about which elements to hide and display, and a lot more.

A common way to describe the transformation process is to say that XSLT transforms an XML source-tree into an XML result-tree. HTML uses predefined tags. The meaning of, and how to display each tag is well understood. CSS is used to add styles to HTML elements. XML does not use predefined tags, and therefore the meaning of each tag is not well understood. XSL describes how the XML elements should be displayed.

XSL consists of four parts:

  • XSLT - a language for transforming XML documents
  • XPath - a language for navigating in XML documents
  • XSL-FO - a language for formatting XML documents (discontinued in 2013)
  • XQuery - a language for querying XML documents

Notation

| separator for alternative values
, separator for consecutive values
? zero-or-more repetitions
* zero-or-more repetitions
+ one-or-more repetitions
#PCDATA parsable character data
boolean-expression expression returning a Boolean
char represents a single character
expression XPath production expression
id XML name used as unique identifier within the document, special attribute type
ncname non-colon-name - XML Name without colon (see also qname)
nmtoken name token – mixture of XML name characters
node-set-expression expression returning a node-set
number represents a number
number-expression expression retuning a number
pattern XPath pattern
prefix XML namespace prefix
qname qualified name – XML name with local part and optional XML namespace prefix, separated by a colon
string represents a string
string-expression expression returning a string
token attribute type
uri-reference Universal Resource Identifier reference

Stylesheet Structure Elements


xsl:import

Imports the contents of one style sheet into another (lower precedence).


<xsl:import 
  href = uri-reference /> 

<xsl:import href="style.xsl"/>

<xsl:template match="message">
  <div style="padding:15px">
  <xsl:apply-imports/>
  </div>
</xsl:template>

xsl:include

Includes the contents of one style sheet into another (same precedence).


<xsl:include 
  href = uri-reference /> 

<xsl:include href="style.xsl"/>

xsl:stylesheet

Defines the root element of a style sheet.
'extension-element-prefixes' list is 'Optional'. A white space separated list of namespace prefixes used for extension elements.


<xsl:stylesheet 
  id = id  
  extension-element-prefixes = tokens  
  exclude-result-prefixes = tokens  
  version = number> 
  <!-- Content: (xsl:import*, top-level-elements)   --> 
</xsl:stylesheet>  

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

xmlns:str="http://exslt.org/strings"
xmlns:date="http://exslt.org/dates-and-times"
extension-element-prefixes="str date"

xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
...
...
</xsl:stylesheet>

xsl:transform

Defines the root element of a style sheet.

xsl:stylesheet and xsl:transform are 100% identical. Everything that applies to xsl:stylesheet is exactly identical to that applies to xsl:transform, whether it is primarily a transformation language or primarily a stylesheet language.


<xsl:transform 
  id = id  
  extension-element-prefixes = tokens  
  exclude-result-prefixes = tokens  
  version = number> 
  <!-- Content: (xsl:import*, top-level-elements) --> 
</xsl:transform> 

<xsl:transform version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

xmlns:str="http://exslt.org/strings"
xmlns:date="http://exslt.org/dates-and-times"
extension-element-prefixes="str date"

xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs">
...
...
</xsl:transform>

xsl:output

Defines the format of the output document

<xsl:output 
  method = "xml" | "html" | "text" | qname-but-not-ncname  => Defines the output format
  version = nmtoken  => Sets the W3C version number for the output format 
                       [only used with method="html" (4.0) or method="xml" (1.0)]
  encoding = string  => Sets the value of the encoding attribute in the output (ISO, UTF-8, UTF-16) 
  omit-xml-declaration = "yes" | "no" => "yes" specifies that the XML declaration (<?xml...?>)
  standalone = "yes" | "no" => standalone=”yes” means that a DTD will be used for validation only.
  doctype-public = string  => Sets the value of the PUBLIC attribute of the 
                              DOCTYPE declaration in the output
  doctype-system = string  => Sets the value of the SYSTEM attribute of the 
                              DOCTYPE declaration in the output
  cdata-section-elements = qnames  => A white-space separated list of elements whose text
                                     contents should be written as CDATA sections
                                    <root><tex-math><![CDATA[$\frac12\beta$ ]]</tex-math></root>
  indent = "yes" | "no"  => "yes" indicates that the output should be indented 
                             according to its hierarchic structure.
  media-type = string => Defines the MIME type of the output. The default is "text/xml"
  /> 

<xsl:output 
  method = "html"
  version = "4.0" 
  encoding = "UTF-8"  
  omit-xml-declaration = "yes"
  standalone = "yes"
  doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
  doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"  
  cdata-section-elements = "tex-math"  
  indent = "yes" | "no" 
  media-type = "text/html" /> 

Template Rule Elements


xsl:apply-imports

Applies a template rule from an imported style sheet


<xsl:apply-imports /> 

<xsl:import href="style.xsl"/>

<xsl:template match="message">
  <div style="padding:15px">
  <xsl:apply-imports/>
  </div>
</xsl:template>

xsl:apply-templates

Applies a template rule to the current element or to the current element's child nodes


<xsl:apply-templates 
  select = node-set-expression  
  mode = qname> 
  <!-- Content: (xsl:sort | xsl:with-param)* --> 
</xsl:apply-templates> 

<xsl:template match="title">
  <h1><xsl:apply-templates/></h1>
  <h2><xsl:apply-templates select="title"/></h2>
  <h3><xsl:apply-templates select="*" mode="big"/></h3>
</xsl:template>

xsl:template


<xsl:template 
  match = pattern  
  name = qname  
  priority = number  
  mode = qname> 
  <!-- Content: (xsl:param*, template) --> 
</xsl:template> 

Variable / Parameter Elements


xsl:copy-of


<xsl:copy-of 
  select = expression /> 

xsl:param


<xsl:param 
  name = qname  
  select = expression> 
  <!-- Content: template --> 
</xsl:param> 

xsl:variable


<xsl:variable 
  name = qname  
  select = expression> 
  <!-- Content: template --> 
</xsl:variable> 

xsl:with-param


<xsl:with-param 
  name = qname  
  select = expression> 
  <!-- Content: template --> 
</xsl:with-param> 

xsl:call-template

Calls a named template


<xsl:call-template 
  name = qname> 
  <!-- Content: xsl:with-param* --> 
</xsl:call-template> 

Creating Result-Tree Elements


xsl:attribute

Adds an attribute


<xsl:attribute 
  name = { qname } 
  namespace = { uri-reference }> 
  <!-- Content: template --> 
</xsl:attribute> 

xsl:attribute-set

Defines a named set of attributes


<xsl:attribute-set 
  name = qname  
  use-attribute-sets = qnames> 
  <!-- Content: xsl:attribute* --> 
</xsl:attribute-set> 

xsl:comment


<xsl:comment> 
  <!-- Content: template --> 
</xsl:comment> 

xsl:copy


<xsl:copy 
  use-attribute-sets = qnames> 
  <!-- Content: template --> 
</xsl:copy> 

xsl:element


<xsl:element 
  name = { qname } 
  namespace = { uri-reference } 
  use-attribute-sets = qnames> 
  <!-- Content: template --> 
</xsl:element> 

xsl:namespace-alias


<xsl:namespace-alias 
  stylesheet-prefix = prefix | "#default" 
  result-prefix = prefix | "#default" /> 

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 } /> 

xsl:processing-instruction


<xsl:processing-instruction 
  name = { ncname }> 
  <!-- Content: template --> 
</xsl:processing-instruction> 

xsl:text


<xsl:text 
  disable-output-escaping = "yes" | "no"> 
  <!-- Content: #PCDATA --> 
</xsl:text> 

xsl:value-of


<xsl:value-of 
  select = string-expression  
  disable-output-escaping = "yes" | "no" /> 

Conditional Processing Elements


xsl:choose

Used in conjunction with <when> and <otherwise> to express multiple conditional tests


<xsl:choose> 
  <!-- Content: (xsl:when+, xsl:otherwise?) --> 
</xsl:choose> 

xsl:if


<xsl:if 
  test = boolean-expression> 
  <!-- Content: template --> 
</xsl:if> 

xsl:otherwise


<xsl:otherwise> 
  <!-- Content: template --> 
</xsl:otherwise> 

xsl:when


<xsl:when 
  test = boolean-expression> 
  <!-- Content: template --> 
</xsl:when>  

xsl:for-each


<xsl:for-each 
  select = node-set-expression> 
  <!-- Content: (xsl:sort*, template) --> 
</xsl:for-each> 

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" } />  

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 /> 

xsl:key


<xsl:key 
  name = qname  
  match = pattern  
  use = expression /> 

xsl:preserve-space


<xsl:preserve-space 
  elements = tokens />

xsl:strip-space


<xsl:strip-space 
  elements = tokens /> 

xsl:fallback

Specifies an alternate code to run if the processor does not support an XSLT element


<xsl:fallback> 
  <!-- Content: template --> 
</xsl:fallback> 

xsl:message

Writes a message to the output (used to report errors)


<xsl:message 
  terminate = "yes" | "no"> 
  <!-- Content: template --> 
</xsl:message> 



Write Your Comments or Suggestion...





Sitemap
▾ Multiple Choice Question (MCQs)