XML and XSLT
XML and XSLT
1
Example code of XSLT
2
XSL = Style Sheets for XML
XML does not use predefined tags, and therefore the meaning of each
tag is not well understood.
LANGUAGES
XSLT is a language for transforming XML documents.
XPath is a language for navigating in XML documents.
XQuery is a language for querying XML documents.
3
Correct Style Sheet Declaration
• The root element that declares the document to be an XSL
style sheet is <xsl:stylesheet> or <xsl:transform>.
Note: <xsl:stylesheet> and <xsl:transform> are completely
synonymous and either can be used!
The correct way to declare an XSL style sheet according to the W3C XSLT
Recommendation is:
4
XSLT <xsl:template> Element
The <xsl:template> element is used to build templates.
The match attribute is used to associate a template with an XML element. The
match attribute can also be used to define a template for the entire XML
document. The value of the match attribute is an XPath expression (i.e.
match="/" defines the whole document).
<xsl:template match="/">
5
XSLT <xsl:value-of> Element
The <xsl:value-of> element can be used to extract the value of an
XML element and add it to the output stream of the transformation:
<xsl:value-of select=“root/parent/child"/>
6
XSLT <xsl:for-each> Element
The XSL <xsl:for-each> element can be used to select every XML element
of a specified node-set
<xsl:for-each select=“root/parent">
7
Filtering the Output
We can also filter the output from the XML file by adding a criterion to
the select attribute in the <xsl:for-each> element.
<xsl:for-each select=“root/parent[child_element=‘text']">
Legal filter operators are:
•= (equal)
•!= (not equal)
•< less than
•> greater than
8
XSLT <xsl:sort> Element
To sort the output, simply add an <xsl:sort> element inside the <xsl:for-each>
element in the XSL file
<xsl:sort select=“element"/>